Hello Readers,
If you select one or more then one checkbox value in php you can easily use the below code.
Here first you make a html (index.html)
index.html:
<form action="checkbox.php" method="post">
<label class="head">Select Your Hobbies:</label><br/>
<input type="checkbox" name="hobbies[]" value="Playing"><label>Playing</label><br/>
<input type="checkbox" name="hobbies[]" value="Coding"><label>Coding.</label><br/>
<input type="checkbox" name="hobbies[]" value="Reading"><label>Reading</label><br/>
<input type="checkbox" name="hobbies[]" value="Hacking"><label>Hacking</label><br/>
<input type="checkbox" name="hobbies[]" value="Sleeping"><label>Sleeping</label><br/><br/>
<input type="submit" name="submit" Value="Submit"/>
</form>
Then you make a checkbox.php file where the html action is going
checkbox.php:
<?php
if(isset($POST['submit'])){
if(!empty($POST['hobbies'])) {
echo " You have selected: ;";
foreach($POST['hobbies'] as $hobby) {
echo "<p>".$hobby ."</p>"; //Print all the hobbies
$show = "<p>".$hobby ."</p>";
echo "<script type='text/javascript'>alert(\"Your Hobbies are: '$show'\");</script>";
}
}
else{
echo "<b>Please Select at least One Hobby.</b>";
}
}
?>
In above code we use two file one is html and other one is php to select the multiple checkbox value and we have also used the server side validation. If we can not select any value it gives the error: please select at least one value or hobby
0 Comment(s)