Loading ...
Sorry, an error occurred while loading the content.

Passing Javascript arrays via YUI Connection

Expand Messages
  • swamyveera
    Hi there friends, I am trying to use YUI connection object and send asynchronous requests to my server. Everything works fine, till i have a need to send an
    Message 1 of 5 , Jul 2, 2008
    View Source
    • 0 Attachment
      Hi there friends,
      I am trying to use YUI connection object and send asynchronous
      requests to my server. Everything works fine, till i have a need to
      send an array to the php script. A function in the script requires a
      parameter which is an array, is there a way to pass javascript arrays
      to the scrip via YUI Connection?

      var batchInfo=new Array();
      batchInfo['batchName']="testBatch";
      batchInfo['location']="Pantai Puteri";
      batchInfo['created_by']="Veeru";
      batchInfo['expected_units']=10;
      batchInfo['collection_date']="2008-07-10";
      batchInfo['organizer']='Testing';
      batchInfo['collection_centre']="Unknown";
      var sUrl =
      'testTallyManager.php?type=ajaxrequest&function=createNewBatch&batchInfo='+batchInfo;
      var obj1 = YAHOO.util.Connect.asyncRequest('GET',
      sUrl, callback);

      That does'nt work at all. Is there any other way or am i missing
      something?

      Thanks
      Vru
    • Satyam
      In JavaScript there is a difference in between arrays and associative arrays. That is not the case in PHP. Thus, you would initialize batchInfo to a new
      Message 2 of 5 , Jul 2, 2008
      View Source
      • 0 Attachment
        In JavaScript there is a difference in between arrays and associative
        arrays. That is not the case in PHP. Thus, you would initialize
        batchInfo to a new Object() not to a new Array(). Arrays inherit from
        Object so your code doesn't produce an error, but it may cause you
        trouble further down the line.

        sURL is a string which you can look at via an alert, console.log,
        debug.print or whatever you have. Is that what you want or PHP
        expects? We are just dealing with strings. JavaScript strings can't
        know the string you are putting together is meant to become an URL.

        The easiest would be to concatenate each of those values as individual
        URL arguments into sURL. That would be easier on the PHP side as well
        since it would read those straight from $_REQUEST. It would be
        advisable to have the arguments sent in a POST request instead of GET
        since they might be truncated if they get too long.

        The other alternative is to use JSON on both sides so you can send
        arbitrary complex data structures. I wouldn't think it would be
        justified in this case.

        Satyam



        swamyveera wrote:
        > Hi there friends,
        > I am trying to use YUI connection object and send asynchronous
        > requests to my server. Everything works fine, till i have a need to
        > send an array to the php script. A function in the script requires a
        > parameter which is an array, is there a way to pass javascript arrays
        > to the scrip via YUI Connection?
        >
        > var batchInfo=new Array();
        > batchInfo['batchName']="testBatch";
        > batchInfo['location']="Pantai Puteri";
        > batchInfo['created_by']="Veeru";
        > batchInfo['expected_units']=10;
        > batchInfo['collection_date']="2008-07-10";
        > batchInfo['organizer']='Testing';
        > batchInfo['collection_centre']="Unknown";
        > var sUrl =
        > 'testTallyManager.php?type=ajaxrequest&function=createNewBatch&batchInfo='+batchInfo;
        > var obj1 = YAHOO.util.Connect.asyncRequest('GET',
        > sUrl, callback);
        >
        > That does'nt work at all. Is there any other way or am i missing
        > something?
        >
        > Thanks
        > Vru
        >
        >
        > ------------------------------------
        >
        > Yahoo! Groups Links
        >
        >
        >
        > ------------------------------------------------------------------------
        >
        >
        > No virus found in this incoming message.
        > Checked by AVG.
        > Version: 8.0.101 / Virus Database: 270.4.3/1529 - Release Date: 01/07/2008 19:23
        >
      • swamyveera
        Hi Satyam Thanks for the quick response. I took your advice, managed to solve the issue. Used a small script called UED, http://www.openjs.com/scripts/data/
        Message 3 of 5 , Jul 2, 2008
        View Source
        • 0 Attachment
          Hi Satyam

          Thanks for the quick response. I took your advice, managed to solve
          the issue.

          Used a small script called UED, http://www.openjs.com/scripts/data/
          ued_url_encoded_data/ and passed json strings rather than javascript
          arrays and that worked for me.

          Thanks
          Vru
        • Eric Miraglia
          Vru, Note that YUI has this functionality as well, in Luke Smith s JSON Utility. http://developer.yahoo.com/yui/json/#stringify Regards, Eric
          Message 4 of 5 , Jul 3, 2008
          View Source
          • 0 Attachment
            Vru,

            Note that YUI has this functionality as well, in Luke Smith's JSON Utility.


            Regards,
            Eric


            On Jul 2, 2008, at 10:12 PM, swamyveera wrote:

            Hi Satyam

            Thanks for the quick response. I took your advice, managed to solve 
            the issue. 

            Used a small script called UED, http://www.openjs. com/scripts/ data/
            ued_url_encoded_ data/ and passed json strings rather than javascript 
            arrays and that worked for me. 

            Thanks
            Vru


          • swamyveera
            hi Eric, Thanks, did nt know that - will try it out Vru
            Message 5 of 5 , Jul 7, 2008
            View Source
            • 0 Attachment
              hi Eric,
              Thanks, did'nt know that - will try it out

              Vru
            Your message has been successfully submitted and would be delivered to recipients shortly.