开发者

send array using jquery

i have a vari开发者_开发百科able $visitRecord->getReferallist() which get the list of doctors in an array . i need to send the array to a php file and do foreach action in php file.. how do i send this array variable in the jquery . This code did not work.

 function seekOpinion()
    {
    var queryUrl = "<?php echo $this->url(array('controller' => 'consultant', 'action' =>'opiniondoclist'));?>";
    $.post(queryUrl,{referal:'<?php echo $gotreferal; ?>',visitId:'<?php echo $gotvisitId; ?>',referalList:'<?php echo $visitRecord->getReferallist(); ?>'},function(data)
    {
            $('.opiniondoclistData').html(data);
    });
            document.getElementById('opiniondoclistDiv').style.display = "";
    }


Your problem is that you're working with an Array in PHP.

echo $visitRecord->getReferallist(); // Returns array of referrals.

When you cast the array to a string by echo'ing it (because echo outputs strings) then you get the text "Array".

In order to send this over the wire(from javascript via AJAX ($.post)) you will need to convert your referral list into a string. One method is serialization. You can convert your array into a "stringable format" using the serialize() function. www.php.net/serialize.

When this is received from PHP in the AJAX request you can convert your "stringable formatted" array back into a pure array using the unserialize() function. www.php.net/unserialize.

Your code should change from

$visitRecord->getReferallist();

to

serialize($visitRecord->getReferallist());

Then when it's received you should change your code from

$referrals = $_POST['referalList']; // Stringable version of the array

to

$referrals = unserialize($_POST['referalList']); // Pure PHP array


Use Serialize() to transform your array into a string, send it , then use unserialize() to get it back into an array in php;

Serialize()


I think you can use json_encode() to encode the array as a string that jquery will be able to read back as a javascript array

EDIT: sorry I didn't read your question properly, I see that you don't want to read the array in javascript, but in PHP so probably serialize() and unserialize() is better than json_encode() assuming that it escapes properly for use with javascript.


var sendData = 'referal=<?php echo $gotreferal; ?>&visitId=<?php echo $gotvisitId; ?>&referalList=<?php echo implode('-', $visitRecord->getReferallist()); ?>';    
$.post(queryUrl, sendData,function(data) { //etc

Then the receiving script can (in addition to any necessary sanitization):

$referalList = explode('-', $_POST['referalList']);

Don't forget to addslashes to all the variables you are echoing if necessary


PHP will automatically create an array variable from a query string, or posted data, which contains a "[]" at the end of the name. For example, you can do a jQuery load call as follows to pass an array to PHP.

$('#target').load('index.php?foo[]=a&foo[]=b');

On the PHP side, you will have an array called foo in $_GET

<?php
echo 'I have ' . $_GET['foo'][0] . ' and ' . $_GET['foo'][1];
?>

jQuery's AJAX functions, e.g. $.post, $.get and $.ajax can pass javascript arrays like this in their data sections - just be sure to name the variable with a "[]" at the end so that PHP knows how to handle it.


I found a solution on this problem...

Use this function in your javascript...

function js_array_to_php_array (a)
{
   var a_php = "";
   var total = 0;
   for (var key in a)
   {
      ++ total;
      a_php = a_php + "s:" +
            String(key).length + ":\"" + String(key) + "\";s:" +
            String(a[key]).length + ":\"" + String(a[key]) + "\";";
   }
   a_php = "a:" + total + ":{" + a_php + "}";

   return a_php;
}

a is the array you passed in this function then.. on your php where you get the return array of this function write this code...

$my_array = unserialize(urldecode(stripslashes($_COOKIE['php_array'])));
// print_r ($my_array); 

Hope it helps...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜