【发布时间】:2016-02-16 16:44:17
【问题描述】:
我正在创建一个搜索界面,让用户从包含 1 到 500 行的表格中选择一行,并找到与所选条目相似的条目。
现在,我对每一行都有一个复选框形式,并直接在 php 中获取值。
index.php 的一部分
<table>
<tr><!-- several tds -->
<td><form action="pfile.php" name="findsimilar1" method="post">
<input type="checkbox" name="color" value="red">
<!-- several more checkboxes with attributes -->
<input type="checkbox" name="darkness" value="8">
<input type="submit" name="submit_b">
</form></td></tr>
<tr><!-- several tds-->
<td><form action="pfile.php" name="findsimilar2" method="post">
<input type="checkbox" name="color" value="blue">
<!-- several more checkboxes with attributes -->
<input type="checkbox" name="darkness" value="2">
<input type="submit" name="submit_b">
</form></td></tr>
<tr><!-- several tds-->
<td><form action="pfile.php" name="findsimilar3" method="post">
<input type="checkbox" name="color" value="green">
<!-- several more checkboxes with attributes -->
<input type="checkbox" name="darkness" value="5">
<input type="submit" name="submit_b">
</form></td></tr></table>
pfile.php 的一部分:
if(isset($_POST['submit_b'])){
if(isset($_POST['color'])){
$c = $_POST['color'];
}
if(isset($_POST['darkness'])){
$d = $_POST['darkness'];
}
}
但只有第一个/最后一个条目的值被发送。我已经 - 在阅读了类似问题的答案之后 - 通过将行号附加到名称来将表单的名称更改为唯一,但这不会改变行为。
用户可以点击所有、一个或几个复选框。
我只希望发送单击其按钮的表单的值。 php 版本:5.3.3 浏览器:Firefox ESR 38.6.0
任何指向正确方向或我可能错过的指针?
Jquery 可能是更好的选择吗?
编辑:
- 添加了 'method="post"'
- 澄清了许多复选框并且没有强制性
【问题讨论】:
-
@JonStirling 是对的,我认为您必须告诉我们,如果您的 php 中只需要一种 color 和一种 darkness 或多个值。
-
他不是说“我只想要发送按钮被点击的表单的值”吗?因此,只应发送 1 个 TR 的形式:2 项“颜色”和“黑暗”。
-
@hherger 你是对的。显然没有引起足够的重视。评论已删除。
-
所以...他应该使用一个带有单选框的表单来表示颜色并禁用复选框来表示黑暗,只有选择了正确的颜色才会启用。
-
但是如何向服务器发送数据呢?如果只是通过单击按钮提交表单,而不涉及任何 javascript,那么您所说的不应该发生。实际上,您发送的是 GET 请求,而不是 POST,但这与您所说的相矛盾,所以很难知道您的问题是什么......