开发者

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 po开发者_运维知识库sted, 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.


Could it be because you're not closing your input tags and it's treating them all as one?


You can also use different names for each checkbox, such as filter_1, filter_2, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜