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 />";

Recommended Answers

All 4 Replies

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.

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>

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/

Thanks so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.