jaxon-php / jaxon-js

The Jaxon javascript library https://www.jaxon-php.org.
BSD 3-Clause "New" or "Revised" License
4 stars 2 forks source link

response->append clears form values when dinamically adding more form elements #7

Closed feuzeu closed 7 years ago

feuzeu commented 7 years ago

From @seba1rx on October 26, 2017 14:59

In my Smarty + Jaxon app I have the following issue: (not using jaxon-smarty)

jaxon.php = Jaxon 2.0-beta.25 jaxon.core.js = jaxon.core.js 327 2007-02-28 16:55:26Z

When I want to append a new row of form elements to my form, all previous form values get cleared,

this is my demo code:

$jxnr = new Jaxon\Response\Response(); $template = $smarty->fetch('file:[sub]newFormRow.html'); $jxnr->append("formBox","innerHTML",$template);

If I create a string with all the template html code and then I append it to the element using jquery, it works:

//$jxnr->append("formBox","innerHTML",$template); $jxnr->script(" $('#formBox').append( '".$str_template."' ); ");

Can this be a jaxon issue?

Copied from original issue: jaxon-php/jaxon-core#25

feuzeu commented 7 years ago

A call to $jxnr->append("formBox","innerHTML",$template); execute the following js code in the browser: jaxon.$('formBox').innerHTML += '<html code>'.

Try this code to see if the error still occurs. $jxnr->script("jaxon.$('formBox').innerHTML += '".$str_template."';");

feuzeu commented 7 years ago

I would like to suggest that you upgrade both your PHP and Js libraries to the latest versions. Many stable versions were released since the one you are using. I also suggest the use of the jQuery response plugin, so you can write this code instead. $jxnr->jq('#formBox')->append($str_template); It is more concise and readable.

feuzeu commented 7 years ago

From @seba1rx on October 26, 2017 18:3

I will do the upgrade and use the jqurery plugin, but for now, I checked the script:

// appending but still clearing previous form fields $jxnr->script("jaxon.$('formBox').innerHTML += '".$str_template."';");

// still using jquery workarround until I try lquery plugin (not clearing previous form fields) $jxnr->script(" $('#formBox').append( '".$str_template."' ); ");

feuzeu commented 7 years ago

It's a bug in the js function. The innerHTML attribute does not include the form values, since there are not part of the DOM. They have to be copied explicitly. I'll move the issue to the jaxon-php/jaxon-js package.

feuzeu commented 7 years ago

Hi, I've reimplemented the DOM append() and prepend() functions using insertAdjacentHTML(). So this issue is fixed for browsers that have this function available.