【问题标题】:Convert jQuery object into PHP array using HTML form使用 HTML 表单将 jQuery 对象转换为 PHP 数组
【发布时间】:2019-06-24 13:32:07
【问题描述】:

我有一个附加到表单字段的 jQuery 对象,例如,

var params = {
    'product_id': productId,
    'width': width,
    'drop': drop
};

$("#sample-form input[name='params']").val(params);

表格的样子,

<form id="sample-form" action="url" method="post">
    <input type="hidden" name="params"/>
    <button type="submit">Submit</button>
</form>

在 PHP 中使用 POST 数据接收此“参数”时,我得到值为“[object Object]”的字符串

如何将其转换为数组?

【问题讨论】:

  • 你可以使用 JSON。 ...val(JSON.stringify(params))。然后在发送到您的 PHP 时反序列化该值
  • 将其存储为字符串化 json val(JSON.stringify(params));。然后在 PHP 中,您可以:$params = json_decode($_POST['params'], true); 将其作为 PHP 数组获取。

标签: php jquery html arrays


【解决方案1】:

我将 jQuery 对象转换为字符串化的 JSON,

$("#sample-form input[name='params']").val(JSON.stringify(params));

在 PHP 中,将接收到的 JSON 转换为 PHP 数组,

$params = json_decode($_POST['params'], true);

【讨论】:

    猜你喜欢
    • 2013-01-31
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2017-04-30
    • 2021-04-05
    • 2016-05-06
    相关资源
    最近更新 更多