Using prototypejs for Ajax, how do i sent an array in the parameters?

Discussion in 'JavaScript' started by 123GoToAndPlay, Aug 16, 2007.

  1. #1
    Hi all,

    Part of the code i am trying

    
    <script language="javascript" type="text/javascript">
    .......
    var pars = "countryname=<?php echo $countryname;?>";
    new Ajax.Updater('formfields', file, { method: 'post', parameters: pars});
    ...........
    </script>
    
    Code (markup):
    When i set $countryname = array('fr','uk','us') i can't get the arrayvalues from countryname.

    Any ideas?
     
    123GoToAndPlay, Aug 16, 2007 IP
  2. bibel

    bibel Active Member

    Messages:
    289
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #2
    You are trying to use an php array as an javascript array.

    var pars = "countryname=<?php echo $countryname;?>"; will be compiled as :
    "countryname=Array".

    You can either define a javascript array, or use serialise() on the php array to convert it to string, and then unserialise to change it back to an array.
     
    bibel, Aug 17, 2007 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    @bibel, that's exactly what i am trying to do.

    Stupid of me to not think of serialise(), but that's was because i thought it must be in JS to enable the AJAX request.

    My solution was the JSON way
    
    //make json object...
    $json = new Services_JSON(); 
    
    ........
    var pars = 'countryname=<?php echo $json->encode($country);?>';
    new Ajax.Updater('formfields', file, { method: 'post', parameters: pars});
    ......
    
    Code (markup):
     
    123GoToAndPlay, Aug 17, 2007 IP