简体   繁体   English

通过PHP的POST复选框形式

[英]POST checkbox form via PHP

My form has both a text area and check boxes. 我的表单同时具有文本区域和复选框。 When I POST the results, only one of the check box values gets posted, even if more than one is selected. 当我POST的结果,只有复选框值中的一个被贴出来,即使不止一个选择。

This is my form: 这是我的表格:

<form action="search.php">
<input type="text" name="term">

<input type="checkbox" name="filter" value="subject"> Subject
<input type="checkbox" name="filter" value="course"> Course
<input type="checkbox" name="filter" value="professor"> Professor

<input type="submit" name="submit" value="Go" />

And this is how I echo the form ( search.php ): 这就是我如何echo表单( search.php ):

<?php
$term = $_GET['term'];
$filter = $_GET['filter'];
echo "$term $filter";
?>

Try this instead: 尝试以下方法:

<input type="checkbox" name="filter[]" value="subject" /> Subject
<input type="checkbox" name="filter[]" value="course" /> Course
<input type="checkbox" name="filter[]" value="course" /> Professor

In your search.php script the submitted 'filter' var will be treated as an array. 在您的search.php脚本中,提交的“ filter”变量将被视为一个数组。

For more information have a look here: http://us2.php.net/manual/en/faq.html.php#faq.html.arrays 有关更多信息,请参见此处: http : //us2.php.net/manual/zh/faq.html.php#faq.html.arrays

Another similar question, i think is: HTML input arrays 我认为另一个类似的问题是: HTML输入数组

The trouble is the names you have assigned each checkbox, what is happening currently is the first checkbox is being read by the PHP and its value assigned to the variable, the next filter checkbox is being read but then overwriting the currently assigned value of the variable, and then again with the 3rd checkbox. 问题是您为每个复选框分配了名称,当前正在发生的情况是PHP读取了第一个复选框并将其值分配给变量,正在读取下一个过滤器复选框,但随后覆盖了变量的当前分配值,然后再次使用第三个复选框。 So you'll always only ever get the last ticked checkbox as your value for the $filter variable. 因此,您永远只会获得最后一个被选中的复选框作为$ filter变量的值。

You need to individually assigned each checkbox with a different name for example: 您需要为每个复选框分别分配不同的名称,例如:

<form action="search.php">
<input type="text" name="term" />

<input type="checkbox" name="filter1" value="subject" /> Subject
<input type="checkbox" name="filter2" value="course" /> Course
<input type="checkbox" name="filter3" value="professor" /> Professor

<input type="submit" name="submit" value="Go" />

Then the PHP: 然后是PHP:

<?php
$term = $_GET['term'];
$filter1 = $_GET['filter1'];
$filter2 = $_GET['filter2'];
$filter3 = $_GET['filter3'];
echo "$term $filter1 $filter2 $filter3";
?>

You also had your last checkbox's value the same as the second so I changed the value from course to professor, if this is in fact wrong, you can obviously change it back. 您还将最后一个复选框的值与第二个复选框的值相同,因此我将其值从课程更改为教授,如果这实际上是错误的,则显然可以将其更改回去。

Another note, it may also be a good idea to use the method POST instead of GET as it's more secure, however, you may have reasons to be using GET, but it's just a heads up :) 另一个注意事项,使用POST而不是GET方法可能也是一个好主意,因为它更安全,但是,您可能有理由使用GET,但这只是一个提示:)

I think you have 3 input boxes named the same "filter" which one is the php expected to get? 我认为您有3个输入框,它们都命名为相同的“过滤器”,而php有望获得其中的一个?

for radio buttons we use an array filter[] so if you really want check boxes and you really want they to all be the same name then try 对于单选按钮,我们使用数组过滤器[],因此,如果您确实想要复选框并且您真的希望它们都具有相同的名称,请尝试

<input type="checkbox" name="filter[]" value="subject"/> Subject
<input type="checkbox" name="filter[]" value="course"/> Course
<input type="checkbox" name="filter[]" value="course"/> Professor

and then in your php you will be getting filter as an array. 然后在您的PHP中,您将获得过滤器作为数组。

可能是因为您没有关闭输入标签,而是将它们全部视为一个?

您还可以为每个复选框使用不同的名称,例如filter_1,filter_2等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM