Php: Publish Checkbox List

After the SQL query, I get a form that contains a list of data, with checkboxes at the end of each row. The goal is the following. If the user validates some of them, the row will be deleted in the database when the form is submitted.

I call my checkboxes similar to my SQL query results, so my Php script can find the line to delete:

<input type="checkbox" name="chk[<?echo '/'.'$t[datetr]'.'/'.'$t[beneficiaire]'.'/'.'$t[objet]'.'/'.'$t[montant]';?>]">

      

My goal is to get all checkbox values ​​with $ _POST for my Php script. But even with that ...

foreach ($_POST as $key => $value) {
        echo "<tr>";
        echo "<td>";
        echo $key;
        echo "</td>";
        echo "<td>";
        echo $value;
        echo "</td>";
        echo "</tr>";
    }

      

... my php script doesn't seem to get the checkbox values ​​... did I do something wrong? Thanks for the help.

+3


source to share


6 answers


Apart from other (completely accurate) comments about unverified blocks not being passed with an HTTP request, you have a couple of issues with your actual PHP.

<?echo '/'.'$t[datetr]'.'/'.'$t[beneficiaire]'.'/'.'$t[objet]'.'/'.'$t[montant]';?>

      

First, your echo is wrong; it will probably be <?php echo

or <?=

, and not <?echo

(although this may work with short start tags).

Second, Apostrophes in PHP are non-interpolated string literals (i.e. '$t[objet]'

literally will be treated as a string '$t[objet]'

not a variable).

Finally, assuming it $t

is an array, your associative indices must be quoted, or they will be interpreted as constants that could throw an error.




I think you want to write:

<?= "/{$t['datetr']}/{$t['beneficiaire']}/{$t['objet']}/{$t['montant']}"; ?>

      

Once you have sorted it, the data $_POST['chk']

should be set correctly and this will be the associative array that you expect.

Then the loop foreach($_POST['chk'] as $key => $value) { ... }

should work ... although of course none of your inputs really matter at the moment.

+2


source


When used, the <input type="checkbox" name="foo" value="42"/>

variable foo = 42 is sent only if the checkbox is checked. When the box is not checked, nothing is sent at all.

If you need some 0/1 info, I suggest using <select>

a tag or couple instead <input type="radio">

:



<input type="radio" name="foo" value="1"/> Yes
<input type="radio" name="foo" value="0"/> No

      

+1


source


Checkboxes

Voids are not sent to the receive script, only checked ones. To work around this, you have an appropriate set of hidden fields and set their values ​​to something like "ON" or "OFF" depending on how you click those checkboxes. You probably want to use the onClick event, detect if the button is clicked or not, then set the corresponding hidden field, i.e. Checkbox_One Hidden_One Checkbox_Two Hidden_Two etc. When you post the form, have your script ignore the checkboxes and just handle the hidden fields.

+1


source


If you list all your checkboxes such as name="boxArray[]"

an array named will be created when you create the form $_POST["boxArray"]

.

Then you can make your foreach loop to display the values:

foreach ($_POST["boxArray"] as $item) {
        echo "<tr>";
        echo "<td>";
        echo $item;
        echo "</td>";
        echo "</tr>";
    }
      

Run codeHide result


To add to this, if you only want to remove the checked items, then for each checkbox, assign the post ID as the value:

<input type="checkbox" name="boxArray[]" value="RECORD ID">

      

Now when you run your foreach loop, only the checked boxes will send values ​​so that you change the code to remove each element in the array:

foreach ($_POST["boxArray"] as $item) {
       //SQL TO DELETE RECORD WHERE ID = $item;
    }

      

+1


source


Something to keep in mind when doing these checkboxes with multiple cells is that when submitted they will be interpreted in PHP as an array, in your case it $_POST['chk']

will be an array of checked checkboxes.

However, you also need to make sure you give the checkboxes a value, even if it's simple 1

.

When accessing POST, try using it first var_dump($_POST); die();

to see what the data looks like.

+1


source


use the following code:

foreach ($_POST['chk'] as $key => $value) {
        echo "<tr>";
        echo "<td>";
        echo $key;
        echo "</td>";
        echo "<td>";
        echo $value;
        echo "</td>";
        echo "</tr>";
    }

      

+1


source







All Articles