doupafu6980 2013-05-20 01:43
浏览 55
已采纳

将数组从AJAX传递到HTML到PHP - 这可能吗?

I have an AJAX call returning an array to a PHP-HTML page.


Here are some blocks of codes:

Javascript:

"Note: get_data.php returns an HTML string, and I want to store it in an array."

var param_array = new Array();
$.post('get_data.php', { id: id }, function(data))
{
    param_array.push(data);
    $('#hidden_input').val(param_array);
}

HTML:

<form action='my_function' method='post'>
    <input type='hidden' id='hidden_input' name='array_from_ajax'>
    <input type='submit' name='btn_submit'>
</form>

PHP - CodeIgniter:

my_function()
{
    $param_array = $this->input->post('array_from_ajax');
    print_r($param_array); // Will this work? will $param_array contain the value?
}

Questions:

  1. Is it possible to return an array value and insert it into the HTML page?
  2. Is a JavaScript array the same as a PHP array?
  3. In the end, will my $param_array in PHP contain the value passed from the AJAX call?
  • 写回答

1条回答 默认 最新

  • doufei2007 2013-05-20 01:50
    关注

    JavaScript and PHP can only communicate over HTTP and only via strings (unless you could SOAP I guess, if that's even possible). That is to say there is no compatibility in data structures between them, so a JavaScript array <> a PHP array. Similarly, you can't set the input value to a JavaScript array.

    The best way to handle this would probably be to JSON-encode the array before setting it to the value of the input. PHP can easily decode the JSON into data structures it can use.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突