mercredi 8 mars 2017

Generating information based on users choice from checkbox in php

I have a form with a list of checkboxes as shown below.

<form action="#" method="post">

<h2>What sport do you play?</h2>

<input type="checkbox" name="check_list[]" value="football"><label>Football</label><br/>
<input type="checkbox" name="check_list[]" value="Rugby"><label>Rugby</label><br/>
<input type="checkbox" name="check_list[]" value="Hockey"><label>Hockey</label><br/>
<input type="checkbox" name="check_list[]" value="Basketball"><label>Basketball</label><br/><br/>

<input type="submit" name="submit" value="Submit"/>

</form>

I would like the users to chose what post they have already played from the list (they are chose more than one) and when they click the submit button the system will display a sport the user could do based on what they have already done i.e chosen.

The code I have tried (that I found on a tutorial) but this code only displays what checkboxes the user has clicked.

<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
}

}
else{
echo "<b>Please Select At least One Option.</b>";
}
}
?>

Should I possibly print out an answer rather than $select and save the result as an array?




Aucun commentaire:

Enregistrer un commentaire