简体   繁体   中英

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.

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 ):

<?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.

For more information have a look here: http://us2.php.net/manual/en/faq.html.php#faq.html.arrays

Another similar question, i think is: HTML input arrays

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. So you'll always only ever get the last ticked checkbox as your value for the $filter variable.

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
$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 :)

I think you have 3 input boxes named the same "filter" which one is the php expected to get?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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