dsfsdf7852
2010-07-26 09:54
浏览 86

使用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 = "";
    }

图片转代码服务由CSDN问答提供 功能建议

我有一个变量$ visitRecord-&gt; getReferallist(),它获取数组中的医生列表。 我需要将数组发送到php文件并在php文件中执行foreach操作..如何在jquery中发送此数组变量。 此代码不起作用。

  function seekOpinion()
 {
 var queryUrl =“&lt;?php echo $ this-&gt; url(array('controller')  =&gt;'consultant','action'=&gt;'opiniondoclist'));?&gt;“; 
 $ .post(queryUrl,{referal:'&lt;?php echo $ gotreferal;?&gt;',visitId  :'&lt;?php echo $ gotvisitId;?&gt;',referalList:'&lt;?php echo $ visitRecord-&gt; getReferallist();?&gt;'},function(data)
 {
 $('  .opiniondoclistData')。html(data); 
}); 
 document.getElementById('opiniondoclistDiv')。style.display =“”; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • duanjue6584 2010-07-27 08:47
    已采纳

    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

    点赞 评论
  • doumei1772 2010-07-26 10:00
    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

    点赞 评论
  • douzhan8303 2010-07-26 11:00

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

    Serialize()

    点赞 评论
  • dqjcb132285 2010-07-26 11:40

    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.

    点赞 评论
  • duanchao5258 2010-07-26 13:34

    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.

    点赞 评论
  • dortmundbvb0624 2010-07-27 08:34

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

    点赞 评论

相关推荐 更多相似问题