0

Hi
I need better way to my checkboxes in form.php here is code

<h3>Which programming languages should you learn first?</h3>
HTML
<input type="checkbox" name="cb_html" value="HTML"><br>
PHP
<input type="checkbox" name="cb_php" value="PHP"><br>
Perl
<input type="checkbox" name="cb_perl" value="Perl"><br>
Python
<input type="checkbox" name="cb_python" value="Python"><br>

And my form.php code is

#checkboxes
if (isset($_POST["cb_html"])) {
    echo "You should learn HTML!";
}
echo "<br>";
if (isset($_POST["cb_php"])) {
    echo "You should learn PHP first!";
}
echo "<br>";


if (isset($_POST["cb_perl"])) {
    echo "You should learn Perl first!";
}
echo "<br>";
if (isset($_POST["cb_python"])) {
    echo "You should learn Python first!";
}

echo "<br />";

Edited by tony75

2
Contributors
4
Replies
45
Views
3 Years
Discussion Span
Last Post by tony75
0

Generally, when you have more than one checkbox, you give them the same value for the name attribute, then in your PHP code, use a loop to retrieve the values.

0

Thanks Sir
Any idea to loop through them?

<h3>Which programming languages should you learn first?</h3>
HTML
<input checked type="checkbox" name="programming" value="HTML"><br>
PHP
<input checked type="checkbox" name="programming" value="PHP"><br>
Perl
<input checked type="checkbox" name="programming" value="Perl"><br>
Python
<input checked type="checkbox" name="programming" value="Python"><br>
0

Use this as the name attribute/value: name="programming[]" for you input elements (checkboxes), then some PHP code to help you loop through and retrieve the results...

<?php
if(isset($_POST['programming'])){
echo "<h3>You Selected:</h3>";
echo "<ul>";
foreach ($_POST['programming'] as $value) {
        echo "<li>$value</li>";
    }
echo "</ul>";
}
?>

Demo ->> http://itg.somee.com/dw/dw-468203/

Edited by JorgeM

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.