JSW189
JSW189

Reputation: 6335

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";
?>

Upvotes: 0

Views: 5801

Answers (5)

Infromthecold
Infromthecold

Reputation: 43

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.

Upvotes: 1

npclaudiu
npclaudiu

Reputation: 2451

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

Upvotes: 0

no.
no.

Reputation: 2376

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

Upvotes: 1

Andreas
Andreas

Reputation: 5335

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: https://www.php.net/manual/en/faq.html.php#faq.html.arrays

Another similar question, i think is: HTML input arrays

Upvotes: 3

dougajmcdonald
dougajmcdonald

Reputation: 20067

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

Upvotes: 0

Related Questions