Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 12:07 PM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,469
    Thanks
    13
    Thanked 361 Times in 357 Posts
    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.
    Last edited by Dormilich; 03-31-2010 at 03:05 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •