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 = "";
}
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. http://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. http://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