Topic: Assigning values to multiple form fields is not working

Asking this here as well I am not sure if it's a bug or not.

Can you check the code below and answer why the value for id1 is not displayed?

<?php
//echo base64_encode(random_string());
require_once("./xajax/xajax_core/xajax.inc.php");
function addFields() {
   
    $objResponse=new xajaxResponse();
    $objResponse->append( 'myform', "innerHTML", '<div id="mydiv1"></div>' );   
    $objResponse->createInput("mydiv1", "text", "field[]", "id1" );
    $objResponse->assign( "id1", "value", "val1" );
$objResponse->append( 'myform', "innerHTML", '<div id="mydiv2"></div>' );
    $objResponse->createInput("mydiv2", "text", "field[]", "id2" );
    $objResponse->assign( "id2", "value", "val2" );
    return $objResponse;
}
$xajax = new xajax();
$xajax->configure('debug',true);
$xajax->configure('verboseDebug',true);
$xajax->configure('javascript URI','/kbdev/xajax/');
$xajax->configure( 'errorHandler', true );
$xajax->configure( 'logFile', './kb_error_log.log' );
$xajax->register(XAJAX_FUNCTION, "addFields");
$xajax->processRequest();
$xajax->printJavascript();
?>
<form id = "myform" onSubmit="return false;">
<button onclick="xajax_addFields(xajax.getFormValues('myform')); return false;">Add new Fields</button>
</form>

Re: Assigning values to multiple form fields is not working

Hi,

Not sure why your code won't work as is. I played with it and this works:

<?php
//echo base64_encode(random_string());
require_once("./xajax/xajax_core/xajax.inc.php");
function addFields() {
   $objResponse=new xajaxResponse();

    $objResponse->append( 'myform', "innerHTML", '<div id="mydiv1"></div>' );   
    $objResponse->createInput("mydiv1", "text", "field[]", "id1" );


    $objResponse->append( 'myform', "innerHTML", '<div id="mydiv2"></div>' );
    $objResponse->createInput("mydiv2", "text", "field[]", "id2" );
    
    $objResponse->assign( "id1", "value", "val1" );
    $objResponse->assign( "id2", "value", "val2" );

    return $objResponse;
}
$xajax = new xajax();
$xajax->configure('javascript URI','./xajax/');
$xajax->register(XAJAX_FUNCTION, "addFields");
$xajax->processRequest();
?>
<!DOCTYPE html>
<head>
<?php $xajax->printJavascript(); ?>
</head>
<body>
<form id = "myform">
<button onclick="xajax_addFields(xajax.getFormValues('form1'));return false;">Add new Fields</button>
</form>
</body>
</html>

Only when I moved both assigns AFTER the appends and createinputs would it work...

Go figure.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: Assigning values to multiple form fields is not working

Ed,

Yea that works but if I want to fully automate e.g. use looping control structures like for/foreach the sequence $objResponse->append, $objResponse->createInput, $objResponse->assign is not working.

You think this is a bug?

Regards,
-Iulian

Re: Assigning values to multiple form fields is not working

I don't know if it's a bug or not but this works...

<?php
//echo base64_encode(random_string());
require_once("./xajax/xajax_core/xajax.inc.php");
function addFields() {
   $objResponse=new xajaxResponse();
    $inputs = array();
    $objResponse->append( 'myform', "innerHTML", '<div id="mydiv1"></div>' );   
    $objResponse->createInput("mydiv1", "text", "field[]", "id1");
    $inputs['id1'] = 'val1';


    $objResponse->append( 'myform', "innerHTML", '<div id="mydiv2"></div>' );
    $objResponse->createInput("mydiv2", "text", "field[]", "id2" );
    $inputs['id2'] = 'val2';
    foreach($inputs as $key=>$val)
      $objResponse->assign($key,'value',$val);

    return $objResponse;
}
$xajax = new xajax();
$xajax->configure('javascript URI','./xajax/');
$xajax->register(XAJAX_FUNCTION, "addFields");
$xajax->processRequest();
?>
<!DOCTYPE html>
<head>
<?php $xajax->printJavascript(); ?>
</head>
<body>
<form id = "myform">
<button onclick="xajax_addFields(xajax.getFormValues('form1'));return false;">Add new Fields</button>
</form>

</body>
</html>

Should lend itself to automation...

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: Assigning values to multiple form fields is not working

This is due to the way the append command works:  it takes the existing html, appends the new html to that in string form, then re-assigns the html to the container innerHTML.

For whatever reason, the browser does not include the value attribute when generating the html text (when xajax reads the innerHTML property during the append).

// Joe

xajax Developer
Connect to me on LinkedIn:
http://www.linkedin.com/in/calledtoconstruct