How to pass a javascript array to php file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarega
    New Member
    • Oct 2008
    • 82

    How to pass a javascript array to php file

    Hi,
    Code:
    <?
    #cd.php
    echo "<input type='text' name='text1' id='text1' onchange=\"return_value()"\ /><br></br>";
    ?>
    Javascript file
    Code:
    var xmlHttp;
     function return_value() {
     var x=document.getElementById('text1').value;
     xmlHttp = GetXmlHttpObject();
     if(xmlHttp == null) {
     alert("Your browser does not support ajax");
     return;
     }
     var url="ab.php";
     url=url+"?x="+x;
     xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
     xmlHttp.onreadystatechange = StateChanged;
     function StateChanged() {
     if(xmlHttp.readyState == 4) {
     var y=new Array();
    y= xmlHttp.responseText;
     }
     }
    the file ab.php returns an array to the javascript file, how can i pass this array 'y' to the php cd.php file
    Last edited by Dormilich; Jan 20 '09, 08:58 AM. Reason: fixed [code] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I guess you have to use a serializer for that. JSON and WDDX are the ones I've heard of (both work in Javascript and PHP)

    in the javascript code line 16 is unnecessary, your array (y) will be replaced by the return string (responseText).

    Comment

    • sarega
      New Member
      • Oct 2008
      • 82

      #3
      ok...can you just give an example of how to serialize it??

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        so far I've only done WDDX. JSON might be more suitable for you. explanations and example you can find here:

        Comment

        • sarega
          New Member
          • Oct 2008
          • 82

          #5
          ok will try that now...thank you

          Comment

          • sarega
            New Member
            • Oct 2008
            • 82

            #6
            for serializing at the javascript and deserializing at the php side, can both the javascript n php be in different files??

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by sarega
              ... can both the javascript n php be in different files??
              yes...???

              PHP is run on the server, JS in the browser, that's two different locations. so why would you have them in the same file? (besides the fact that serialization originates from transfering complex data between system)

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Instead of trying to pass an array, pass something that the server-side script will recognise, e.g. "val=1&val=2&va l=3" (rather like how checked checkbox values are passed).

                Comment

                • sarega
                  New Member
                  • Oct 2008
                  • 82

                  #9
                  I cannot reload the page, if I did then the values that are entered in the text boxes will be lost...

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by sarega
                    if I did then the values that are entered in the text boxes will be lost...
                    that's just a matter of correctly setting the text boxes' default values.

                    Comment

                    • sarega
                      New Member
                      • Oct 2008
                      • 82

                      #11
                      But the value in the text box will not be the same every time the form is submitted for the user

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        I don't see the problem there.

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Yes, no problem there. Either use Ajax, or use the new submitted values to set the text box values.

                          Comment

                          Working...