I'm creating a used cars website (written in PHP), and I'm stuck on sending advanced search options from form. I have more than 30 of them and I wonder if it's possible, and how, to send them in one variable (for example &options=1,2,3,5,6,10
or some other way..).
Also I've heard that this is possible with "bitwise" but I don't have a clue how to do that. Or if someone have a better idea, please let me know.
Thanks.
You can send them in an array.
<form method="post">
<input type="checkbox" name="param[]" value="blue" />
<input type="checkbox" name="param[]" value="red" />
<input type="checkbox" name="param[]" value="orange" />
<input type="checkbox" name="param[]" value="green" />
<input type="checkbox" name="param[]" value="black" />
<input type="submit" value="Submit" />
</form>
>> $_POST['param']
array('blue', 'orange')
You can even use multidimensional arrays:
<form method="post">
<input type="checkbox" name="param[color][]" value="blue" />
<input type="checkbox" name="param[color][]" value="red" />
<input type="checkbox" name="param[color][]" value="orange" />
<input type="checkbox" name="param[color][]" value="green" />
<input type="checkbox" name="param[color][]" value="black" />
<input type="checkbox" name="param[year][]" value="1999" />
<input type="checkbox" name="param[year][]" value="2000" />
<input type="checkbox" name="param[year][]" value="2001" />
<input type="checkbox" name="param[year][]" value="2002" />
<input type="checkbox" name="param[year][]" value="2003" />
<input type="submit" value="Submit" />
</form>
>> $_POST['param']['color']
array('blue', 'green')
>> $_POST['param']['year']
array('2001', '2004')
Place them in an array and loop through them in the script that processes the form.
<form action="yourscript.php" method="POST">
<label for="option1">Option 1</label>
<input id="option1" type="checkbox" name="option[]" value="option1" />
<label for="option2">Option 2</label>
<input id="option2" type="checkbox" name="option[]" value="option2" />
<label for="option3">Option 3</label>
<input id="option3" type="checkbox" name="option[]" value="option3" />
<input type="submit" name="submit" value=Submit />
</form>
The key is to place your checkbox values into in an array. option[]
Then in your script, you can access the array and loop through the submitted options.
if(!empty($_POST['submit']) //process the form
if(!empty($_POST['option']) //check to see if any checkboxes were selected
{
$options = $_POST['option'];
foreach($options as $option) //loop through the checkboxes
{
//do what you need to do with an option
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With