gpt4 book ai didi

javascript - 将 javascript 数组中的值发送到表单值 html,将 html 值中的值发送到 php

转载 作者:行者123 更新时间:2023-11-28 19:46:53 25 4
gpt4 key购买 nike

标题有点难,我来解释一下

我在 JavaScript 数组中有值,我想将这些值放入 PHP 中读取的表单中。

我在想这样的事情:

Result.title = 用户填写的标题的数组。Result.price = 从数据库填充价格的数组

*the html*
<textarea id="products" type="hidden" name="products"/>
<textarea id="price_products" type="hidden" name="price_products"/>



*the javascripts*
$("#products").val(result.title);
$("#price_products").val(result.price);


*the PHP*
$products = Trim(stripslashes($_POST['products']));
$price_products = Trim(stripslashes($_POST['price_products']));

$Body .= $products;
$Body .= "\n";
$Body .= $price_products;
$Body .= "\n";

问题是我只将第一个值发送到文本区域。

最佳答案

好吧,由于您在服务器端使用 PHP,因此您可以使用字段名称中带有 [] 后缀的隐藏字段。

<form method="post" action="post.php">
<script>
var items = ["Hello","World"];
for (var i = 0; i < items.length; i++) {
document.write('<input type="hidden" name="items[]" value="'+escape(items[i])+'" />')
}
</script>
<input type="submit">
</form>

然后,在 post.php 中,只需读取 $_POST['items'],它将是一个数组。

关于javascript - 将 javascript 数组中的值发送到表单值 html,将 html 值中的值发送到 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24121572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com