pass object from JS to PHP and back

fatpplown

New Member
This is something that I don't think can't be done, or can't be done easy.Think of this, You have an button inside a div in HTML, when you click it, you call a php function via AJAX, I would like to send the element that start the click event(or any element as a parameter) to PHP and BACK to JS again, in a way like serialize() in PHP, to be able to restore the element in JS.Let me give you a simple example:pHP:\[code\]function ajaxCall(element){ return element;}\[/code\]JS:\[code\]callbackFunction(el){ el.color='red';}\[/code\]HTML:\[code\]<div id="id_div"><input type="button" value="http://stackoverflow.com/questions/2493053/click Me" onClick="ajaxCall(this, callbackFunction);" /></div>\[/code\]So I thing at 3 methodsmethod 1. I can give each element in the page an ID.so the call to Ajax would look like this:\[code\]ajaxCall(this.id, callbackFunction);\[/code\]and the callback function would be:\[code\]document.getElementById(el).color='red';\[/code\]This method I think is hard, beacause in a big page is hard to keep track of all ID's.method 2. I think that using xPath could be done, If i can get the exact path of an element, and in the callback function evaluate that path to reach the element. This method needs some googling, it is just an ideea.method 3. Modify my AJAX functions, so it retain the element that started the event, and pass it to the callback function as argument when something returns from PHP, so in my AJAX would look like this:\[code\]eval(callbackFunction(argumentsFromPhp, element));\[/code\]and the callback function would be:\[code\]callbackFunction(someArgsFromPhp, el){ el.color='red';// parse someArgsFromPhp}\[/code\]I think that the third option is my choise to start this experiment.Any of you has a better idea how I can accomplish this ?Thank you.EDIT:The previous example is not complex enougth, I will try a better explanation this time.PHP:\[code\]function phpFunct($age){ $text=''; if(!is_numeric($age)){ $t['alert'] = 'You filled an invalid age !'; return $t; } // Can be lots of code ... $text.='Your age is '.$age; $t['#myDiv'] = $text; return $t;}\[/code\]JS: \[code\]function callback(x){ for(i in x){ if(i == 'alert'){ // Alert if something went wrong. alert(x); } elseif(){ // ... more options, promts, console.log's ...etc }else{ // Simple innerHTML $(i).val(x); } }}phpFunct(){ x_phpFunct($('#age').val(), callback); //call PHP function via AJAX}\[/code\]HTML:\[code\]<div id="myDiv"><input type="text" id="age" /><br /><input type="button" value="http://stackoverflow.com/questions/2493053/getSomethingFromPhp" onCLick="phpFunct();" /></div>\[/code\]I would like to be able to do something like this:HTML:\[code\]onClick="phpFunct(this.parentNode);"\[/code\]JS:\[code\]phpFunct(el){ x_phpFunct(el, $('#age').val(), callback);}\[/code\]PHP:\[code\]phpFunct($el, $age){ ... $t[$el] = 'Your age is '.$age; return $t;}\[/code\]This can be done if you have an ID for every element, and pass that ID to PHP and back. I know is not "legal" but now my ID are not unique. I use DIV's as wrapers and everithing inside that DIV have unique ID's (is like using DIV's as body, an have a lot of page opened in the same time, many bodyes). And if you reach an element you always know who the parent of that element is.
 
Top