Getting arguments with AJAX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mikek12004
    New Member
    • Sep 2008
    • 200

    #1

    Getting arguments with AJAX

    Is it possible to put an array of values as an argument to a js function? I want through this function and with AJAX to pass this array to a PHP array inside the PHP script which will then do the rest
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You could pass a string with delimiters and split on the server-side.

    Comment

    • mikek12004
      New Member
      • Sep 2008
      • 200

      #3
      So it is impossible to use an array eh?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Well, you could use an array, but it'd be passed as a string, e.g.
        [1,2,3] would be passed as "1,2,3". You could serialize it on the client-side and deserialize it on the server-side.

        Comment

        • Canabeez
          New Member
          • Jul 2009
          • 126

          #5
          Try something like:

          [code=javascript]
          var myPostVars = "myArray[0]=AAA&myArray[1]=BBB";
          [/code]

          then in PHP the $_REQUEST["myArray"] should already be a compiled array.

          [code=php]
          var_dump($_REQU EST["myArray"]);
          [/code]
          Not sure it will work, well it did in PHP3, but things have changed since then, but still worth trying. ;)

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            that suggestion shows a 'kind of serializing' a javascript array ... since you cannot use that easyly in both languages ... a very seamless way to handle more complex datatypes is to use JSON ... so that you might use simple eval() on JS-side and json_encode() or json_decode() in php ... that will do the serializing and deserializing for you ...

            Comment

            • Canabeez
              New Member
              • Jul 2009
              • 126

              #7
              Originally posted by gits
              that suggestion shows a 'kind of serializing' a javascript array ... since you cannot use that easyly in both languages ... a very seamless way to handle more complex datatypes is to use JSON ... so that you might use simple eval() on JS-side and json_encode() or json_decode() in php ... that will do the serializing and deserializing for you ...
              You're right, I'd prefer building an XML in JS and then parse it in PHP, think that would be the right way to do it, or JSON as you mentioned. But the question was about passing an array through an AJAX request.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                JSON is more lightweight compared to XML ... so i would always prefer JSON ... and the other point is: what you have suggested is a serialized array that is passed as a string ... and no array ... to be correct :)

                Comment

                • Canabeez
                  New Member
                  • Jul 2009
                  • 126

                  #9
                  True....... True......... ;)

                  Comment

                  Working...