0

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 = "";
    }
2
  • What does the generated HTML look like? Commented Jul 26, 2010 at 9:56
  • @Milan Babuškov - That i am formatting . but i need to get the array properly at back end php. when i say to print echo $_POST['referalList'] in the opiniondoclist . it just prints Array. nothing else... Commented Jul 26, 2010 at 10:00

6 Answers 6

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Serialize()

Comments

1

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.

1 Comment

I think json_encode is the better option for an array, because jQuery will send it as an array and PHP will interpret it as an array for $_GET or $_POST. This will only work with primitive strings as values, though.
0
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

1 Comment

i found a similar kind of solution stackoverflow.com/questions/1219473/…
0

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.

Comments

0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.