I want to post values of check boxes on booking.php page.
There are many checkboxes on the page but I don't know how to post on booking.php
page.
<form name="booking.php" method="post"> <label for="tour" class="tour-label">Add to Tour List</label> <input type="checkbox" name="booking-check" value="Desert Safari" /> </form> <div class="details"><a href="booking.php">Book Selected Tours</a></div>
Only last values will be sent. This way if a box is checked then its name and value "on" is sent, whereas if it's unchecked then the name of the corresponding hidden input and whatever value you might like to give it will be sent.
When it comes to using checkboxes, you kind of do not have a choice but to use foreach , and that's why you only get one value returned from your array. Here is an example using $_GET . You can however use $_POST and would need to make both directives match in both files in order to work properly.
If you need to submit a form when a checkbox is checked or when it is unchecked like when you are using a switch, a good way is to create an hidden input. If you try to submit the checkbox argument if the checkbox is unchecked the form will not be submitted at all. Find below my solution.
There are many links that lets you know how to handle post values from checkboxes in php. Look at this link: http://www.html-form-guide.com/php-form/php-form-checkbox.html
Single check box
HTML code:
<form action="checkbox-form.php" method="post"> Do you need wheelchair access? <input type="checkbox" name="formWheelchair" value="Yes" /> <input type="submit" name="formSubmit" value="Submit" /> </form>
PHP Code:
<?php if (isset($_POST['formWheelchair']) && $_POST['formWheelchair'] == 'Yes') { echo "Need wheelchair access."; } else { echo "Do not Need wheelchair access."; } ?>
Check box group
<form action="checkbox-form.php" method="post"> Which buildings do you want access to?<br /> <input type="checkbox" name="formDoor[]" value="A" />Acorn Building<br /> <input type="checkbox" name="formDoor[]" value="B" />Brown Hall<br /> <input type="checkbox" name="formDoor[]" value="C" />Carnegie Complex<br /> <input type="checkbox" name="formDoor[]" value="D" />Drake Commons<br /> <input type="checkbox" name="formDoor[]" value="E" />Elliot House <input type="submit" name="formSubmit" value="Submit" /> /form> <?php $aDoor = $_POST['formDoor']; if(empty($aDoor)) { echo("You didn't select any buildings."); } else { $N = count($aDoor); echo("You selected $N door(s): "); for($i=0; $i < $N; $i++) { echo($aDoor[$i] . " "); } } ?>
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