gpt4 book ai didi

php - 将表格单元格值放入 PHP post 中

转载 作者:行者123 更新时间:2023-11-30 01:27:06 25 4
gpt4 key购买 nike

我在调整从我设置的表传递到 $_POST 的值时遇到问题。

这是用于设置表的代码:

print "<form action=\"updateitem.php\" method=\"post\">\n";
print "<table border cellpadding = 3>\n";
print "<tr>\n";
print "<th>Menu item id</th>\n";
print "<th>Menu item name</th>\n";
print "<th>Menu</th>\n";
print "<th>Menu item price</th>\n";
print "</tr>\n";
while($info = mysql_fetch_array($results)){
$mi = explode("_",$info['meta_key']);
if ($mi[2] == "food")
$mi[2] = "takeout";
print "<tr>\n";
print "<td id=\"meta_id\">".$info['meta_id']."<input type=\"hidden\" name=\"meta_id".$i."\" value=\"".$info['meta_id']."\" /></td>\n";
print "<td>".$info['post_title']."</td>\n";
print "<td>".$mi[2]."</td>\n";
print "<td id=\"price\" contenteditable=\"true\">".$info['meta_value']."<input type=\"hidden\" name=\"meta_value".$i."\" value=\"".$info['meta_value']."\" /></td>\n";
print "</tr>\n";
$i++;
}
print "</table>\n";
print "<input type=\"Submit\" value=\"Update\" />\n";
print "</form>\n";

我想做的是,当有人更改价格单元格时,我希望将更新的价格传递到 PHP 的 $_POST 中。那可能吗?

最佳答案

contenteditable 属性在这里不会为您提供帮助。您必须使用 javascript 来注意到它何时发生更改并对其执行某些操作。

简单的答案是,您需要使 <input> 字段可供用户使用。因此,将 type="hidden" 更改为 type="text" 或添加另一个不隐藏供用户交互的字段,然后该值将在 $_POST 中传回脚本。

建议的更改

print "<form action=\"updateitem.php\" method=\"post\">\n";
print "<table border cellpadding = 3>\n";
print "<tr>\n";
print "<th>Menu item id</th>\n";
print "<th>Menu item name</th>\n";
print "<th>Menu</th>\n";
print "<th>Menu item price</th>\n";
print "</tr>\n";
while($info = mysql_fetch_array($results)){
$mi = explode("_",$info['meta_key']);
if ($mi[2] == "food")
$mi[2] = "takeout";
print "<tr>\n";
print '<td id="meta_id">' . $info['meta_id'] . '<input type="hidden" name="meta_id[]" value="' . $info['meta_id'] . '" /></td>';
print '<td>'.$info['post_title'].'</td>';
print '<td>'.$mi[2].'</td>';
print '<td id="price">';
print '<input type="hidden" name="original_price[]" value="' . $info['meta_value'] . '" />';
print '<input type="text" name="price[]" value="' . $info['meta_value'] . '" />';
print '</td>';
print "</tr>\n";
$i++;
}
print "</table>\n";
print "<input type=\"Submit\" value=\"Update\" />\n";
print "</form>\n";

我擅自更改了名称 (name="name[]") 以使用数组而不是 (name="name" . $i)

这会将字段作为 $_POST 数组中的数组返回所以 $_POST[price] will itself be an array 像这样:

$_POST['price][0]
$_POST['price][1] // and so on

因此您可以将更改后的价格处理为

foreach ( $_POST['price'] as $i => $price ) {
if ( $price != $_POST['original_price'][$i]) {
// We have a price change lets update the database
// using $_POST['meta_id'][$i] as the key to the database table ? I assume ??
}

}

关于php - 将表格单元格值放入 PHP post 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17873625/

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