Menno Hes
Menno Hes

Reputation: 23

Checkbox only returns first value

I´m creating a form with checkboxes. But somehow I only get the first result back..

Hope you can help!

<strong>My form </strong>

<form action="historie.php" method="post">
<?php
// ophalen van bijbehorende producten
    $sql_product_opzoek = "SELECT * FROM product_bestellingen WHERE bestelnummer = $bestelnummer_opzoek";
    $sql_p_opzoek = mysqli_query($con, $sql_product_opzoek);
    $count = 0;
    while ($row = mysqli_fetch_array($sql_p_opzoek)) {

        $artikelnr_retour = $row['artikelnr'];
        $merk_retour = $row['merk'];
        $artikelnr_lev_retour = $row['artikelnr_lev'];
        $kleur_retour = $row['kleur'];
        $maat_retour = $row['maat'];
        $prijs_retour = $row['prijs'];


        if ($count === 0) {
            $sql_opzoek = "SELECT * FROM bestellingen WHERE bestelnummer = $bestelnummer_opzoek";
            $sql_b_opzoek = mysqli_query($con, $sql_opzoek);

            while ($row = mysqli_fetch_array($sql_b_opzoek)) {
                $tent = $row['tent'];
            }

            echo $tent;
        }

        echo "
            <input type=\"checkbox\" name=\"retour[" . $artikelnr_retour . "]\" value=\"" . $artikelnr_retour . "\">
            <input type=\"hidden\" name=\"merk\" value=\"" . $merk_retour . "\">
            <input type=\"hidden\" name=\"art_lev\" value=\"" . $artikelnr_lev_retour . "\">

            <input type=\"hidden\" name=\"kleur\" value=\"" . $kleur_retour . "\">
            <input type=\"hidden\" name=\"maat\" value=\"" . $maat_retour . "\">
            <input type=\"hidden\" name=\"prijs\" value=\"" . $prijs_retour . "\">
            <input type=\"hidden\" name=\"bestelnummer\" value=\"" . $bestelnummer_opzoek . "\">
        ";

        if ($count === 0) {
            echo "<input name=\"submit\" type=\"submit\">";
        }

    echo "</form>";
        $count++;
}

This results in the next HTML

<strong>HTML</strong>
<form action="historie.php" method="post">
    <tr>
        <td>654655</td>
        <td>Huggo Boss</td>
        <td>xwsmcdD</td>
        <td> 13</td>
        <td>45</td>
        <td>€ 99,95</td>
        <td>123456</td>
        <td>
            <input type="checkbox" name="retour[654655]" value="654655">
            <input type="hidden" name="merk" value="Huggo Boss">
            <input type="hidden" name="art_lev" value="xwsmcdD">

            <input type="hidden" name="kleur" value=" 13">
            <input type="hidden" name="maat" value="45">
            <input type="hidden" name="prijs" value="99,95">
            <input type="hidden" name="bestelnummer" value="987654">
        </td>
        <td><input name="submit" type="submit"></td>
    </tr>
</form>
    <tr>
        <td>100254</td>
        <td>Maripe</td>
        <td>Stun</td>
        <td> 66</td>
        <td>33</td>
        <td>€ 295,95</td>
        <td></td>
        <td>
            <input type="checkbox" name="retour[100254]" value="100254">
            <input type="hidden" name="merk" value="Maripe">
            <input type="hidden" name="art_lev" value="Stun">

            <input type="hidden" name="kleur" value=" 66">
            <input type="hidden" name="maat" value="33">
            <input type="hidden" name="prijs" value="295,95">
            <input type="hidden" name="bestelnummer" value="987654">
        </td>
        <td></td>
    </tr>
</form>

To get the results I use the next PHP

PHP to get results

 foreach ($_POST['retour'] as $value) {
     echo $value; 
 }

This results only in the first result: 654655

Hope you can help!

Upvotes: 2

Views: 1576

Answers (3)

Manish J
Manish J

Reputation: 309

Your form is close befor the second check box. Please try this code:-

 <strong>HTML</strong>
<form action="historie.php" method="post">
    <tr>
        <td>654655</td>
        <td>Huggo Boss</td>
        <td>xwsmcdD</td>
        <td> 13</td>
        <td>45</td>
        <td>€ 99,95</td>
        <td>123456</td>
        <td>
            <input type="checkbox" name="retour[654655]" value="654655">
            <input type="hidden" name="merk" value="Huggo Boss">
            <input type="hidden" name="art_lev" value="xwsmcdD">

            <input type="hidden" name="kleur" value=" 13">
            <input type="hidden" name="maat" value="45">
            <input type="hidden" name="prijs" value="99,95">
            <input type="hidden" name="bestelnummer" value="987654">
        </td>
        <td></td>
    </tr>

    <tr>
        <td>100254</td>
        <td>Maripe</td>
        <td>Stun</td>
        <td> 66</td>
        <td>33</td>
        <td>€ 295,95</td>
        <td></td>
        <td>
            <input type="checkbox" name="retour[100254]" value="100254">
            <input type="hidden" name="merk" value="Maripe">
            <input type="hidden" name="art_lev" value="Stun">

            <input type="hidden" name="kleur" value=" 66">
            <input type="hidden" name="maat" value="33">
            <input type="hidden" name="prijs" value="295,95">
            <input type="hidden" name="bestelnummer" value="987654">
        </td>
        <td></td>
    </tr>
    <input name="submit" type="submit">
</form>

Upvotes: 1

halfpastfour.am
halfpastfour.am

Reputation: 5933

If you check both of the boxes, then both values should come through $_POST. If you only check one of the boxes, it only sends that value through $_POST.

Example:

One checkbox checked:

Only one checked

Both checkboxes checked:

enter image description here

Solution:

Try the following to make sure there is something in the $_POST at all times:

<input type="hidden" name="retour[654655]" value="0">
<input type="checkbox" name="retour[654655]" value="1">

If the value of $_POST['654655'] equals 1, it is present. If it equals 0 it is not present. The value of a checkbox will not be submitted as long as it is not checked.With the solution presented above the value of the hidden field will be overridden when the checkbox is checked.

Upvotes: 0

arunrc
arunrc

Reputation: 628

There is no need to use <input type="checkbox" name="retour[654655]" value="654655">

Just use array. i.e.

 <input type="checkbox" name="retour[]" value="654655">
<input type="checkbox" name="retour[]" value="100254">

This will give you $retour[0]=654655 and $retour[1]=100254

Upvotes: 1

Related Questions