使用jQuery发送数组 [英] send array using jquery
问题描述
我有一个变量$ visitRecord-> getReferallist(),它获取数组中的医生列表.我需要将数组发送到php文件,并在php文件中执行foreach操作.我如何在jquery中发送此数组变量.该代码无效.
i have a variable $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 = "";
}
推荐答案
您的问题是您正在使用PHP中的数组.
Your problem is that you're working with an Array in PHP.
echo $ visitRecord-> getReferallist(); //返回引荐数组.
echo $visitRecord->getReferallist(); // Returns array of referrals.
当通过回显将数组转换为字符串时(因为回显输出字符串),则会得到文本"Array".
When you cast the array to a string by echo'ing it (because echo outputs strings) then you get the text "Array".
为了通过导线发送此消息(从javascript通过AJAX($ .post)发送),您需要将您的引荐列表转换为字符串.一种方法是序列化.您可以使用 serialize ()函数将数组转换为可字符串化的格式". www.php.net/serialize.
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.
在AJAX请求中从PHP收到此消息后,您可以使用 unserialize ()函数将可字符串格式的"数组转换回纯数组. www.php.net/unserialize.
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.
您的代码应更改为
$ visitRecord-> getReferallist();
到
序列化($ visitRecord-> getReferallist());
然后,当您收到它时,应从以下位置更改代码
Then when it's received you should change your code from
$ referrals = $ _POST ['referalList']; //数组的可字符串化版本
$referrals = $_POST['referalList']; // Stringable version of the array
到
$ referrals = unserialize($ _ POST ['referalList']); //纯PHP数组
这篇关于使用jQuery发送数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!