How do I process data from checkboxes?

Dear all,

I am not an expert and I need your help. In a form, I have multiple checkboxes, which corresponds to categories. My code is the following:

<form name="input" action="nextpage.php" method="get">

<input type="checkbox" name="region1" value="France" /> France <br>

<input type="checkbox" name="region2" value="Belgique" /> Belgique <br>

<input type="checkbox" name="region3" value="Canada" /> Canada <br>

<input type="checkbox" name="region4" value="Suisse" /> Suisse <br>

<input type="checkbox" name="region5" value="Afrique" /> Afrique <br>

Now, for the next page, I need to know how to retrieve this information. Then, let's say that the user checked regions 1, 3 and 5, I need my PHP code to retrieve the categories that are associated in the database with the regions. In other words, I would like something that says "If region1 is checked, retrieve categories from database and list them". Then, it would do the same from regions 3 and 5, in a loop. I know that I will need an array but I am not very comfortable with these codes (I am learning PHP). Can anyone help me or explain me how it works please?

Thanks!

Comments

  • PHP provides a really cool way to process forms like this. Since your checkboxes are all related you can name them in such a way that PHP will put all the checked values in an array for you to handle in your code. To enable this, your HTML must be rewritten like this:

    <form name="input" action="nextpage.php" method="get">

    <input type="checkbox" name="regions[]" value="France" /> France <br>

    <input type="checkbox" name="regions[]" value="Belgique" /> Belgique <br>

    <input type="checkbox" name="regions[]" value="Canada" /> Canada <br>

    <input type="checkbox" name="regions[]" value="Suisse" /> Suisse <br>

    <input type="checkbox" name="regions[]" value="Afrique" /> Afrique <br>

    Notice that all the checkboxes are renamed to "regions[]". To access which checkboxes are checked in your PHP form handler you can do this:

    <h2>Selected Regions</h2>

    <?php

    if (!empty($_GET['regions'])) {

    echo "<ul>\n";

    foreach ($_GET['regions'] as $region) {

    echo "<li>$region</li>\n";

    }

    echo "</ul>\n";

    } else {

    echo "<p>No regions selected.</p>\n";

    }

    ?>

    In the code above, all the selected regions are contained in $_GET['regions'] which PHP defines as an array because of the naming convention used for the checkboxes. Appending [] at the end of the name in the form element tells PHP to make this a list or array in the $_GET, $_POST or $_REQUEST arrays. If no checkboxes are selected, $_GET['regions'] is not set at all.

  • Below is the code you need, If you want them to only be able to make one choice out of the 5 regions, give each checkbox the same name... like

    <input type="checkbox" name="region" ...>

    <?

    if (isset($_REQUEST['region1']))

    { $r1 = $_REQUEST['region1'];}

    if (isset($_REQUEST['region2']))

    { $r2 = $_REQUEST['region2'];}

    if (isset($_REQUEST['region3']))

    { $r3 = $_REQUEST['region3'];}

    if (isset($_REQUEST['region4']))

    { $r4 = $_REQUEST['region4'];}

    if (isset($_REQUEST['region5']))

    { $r5 = $_REQUEST['region5'];}

    echo"

    r1 = $r1<br />

    r2 = $r2<br />

    r3 = $r3<br />

    r4 = $r4<br />

    r5 = $r5<br />";

    ?>

Sign In or Register to comment.