【问题标题】:Take table cell value into PHP post将表格单元格值放入 PHP 帖子中
【发布时间】:2013-07-26 06:24:55
【问题描述】:

我在调整从我设置的表中传递到$_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。那可能吗?

【问题讨论】:

  • 提交该表单后,表单字段“价格”将被提交到 updateitem.php。有什么问题?

标签: php html mysql html-table cell


【解决方案1】:

contenteditable 属性在这里对您没有帮助。 您必须使用 javascript 来注意何时更改并对其进行处理。

简单的答案是,您需要使&lt;input&gt; 字段对用户可用。因此,将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 ??
    }

}

【讨论】:

  • 嗨 RiggsFolly,感谢您的回复。不幸的是,这似乎只给出一个结果,而不是整个表格。谢谢。
  • 真的很抱歉,我收回了。使用 foreach 循环带出每个项目。感谢您的帮助 RiggsFolly!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多