Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-31-2010, 10:37 AM   PM User | #1
syntax_error
New to the CF scene

 
Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
syntax_error is an unknown quantity at this point
AJAX: JS callback via PHP: what can I do with an [object NodeList]?

Greetings,

just dropped by here, looks quite nice with you guys here...

Well.
I wanted to send a form content to a jsPrep() ( = prepare) function in JS, then (silently) pass this to PHP.
The tricky thing is that this here is an ARRAY and can be implode()'d easily if and only if the PHP script is specified in the action=... attribute.

However, if I prepare some stuff in my JS caller script, and pass it to PHP as a second step, I get a [object NodeList] inside PHP which I do not know what to do with...

Looks like this:

[html]
Code:
<form name="suchform2" action="" onSubmit="jsPrepPhp('thephp',this.form['rsel[]']);return false;">

<input type="checkbox" name="rsel[]" value="101">
<input type="checkbox" name="rsel[]" value="102">
<input type="checkbox" name="rsel[]" value="103">
<input type="checkbox" name="rsel[]" value="104">

<input type="button" 
       value="send" onClick="jsPrepPhp('thephp',this.form['rsel[]']);return false;">
</form>
[js]
Code:
jsPrepPhp = function (ajaxPHP, prm1)
{
  var phpfile = ajaxPHP + ".php";    
  var enc=window.encodeURIComponent||window.escape;  
  var paramStr =  'p1[]=' + enc(prm1);
  send2PHP(phpfile,paramStr);

  // NOTE: send2PHP() is the common XMLHttpRequest stuff
}
{php} (via ajax)
PHP Code:
$prm_array1=$_POST['p1'];
$prm_array_text implode(', ',$prm_array1); 
This does not really work because as p1 I get a object NodeList from my JS caller script and I dunno why I get this nor can I do anything with it in my case.
Is there any way to rewrite it so that the implode() works as if I had directly specified the PHP in the action attribute in the <form>?
(Which does not work in my case because what you see is only a very compact part of the thing, in order to prevent you from having to read 500 LOC...)

Last edited by syntax_error; 03-31-2010 at 11:07 AM..
syntax_error is offline   Reply With Quote
Old 03-31-2010, 02:01 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by syntax_error View Post
This does not really work because as p1 I get a object NodeList from my JS caller script and I dunno why I get this nor can I do anything with it in my case.
upon "encoding" the NodeList (that’s the DOM-Interface (correlates to the JS-constructor) of the p1 object) you receive this object’s string representation, which in JavaScript is [object NodeList]. you would have to serialize (e.g. by JSON, WDDX) this object before sending it to PHP.
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 03-31-2010 at 02:05 PM..
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:34 AM.


Advertisement
Log in to turn off these ads.