How to get information from the checkbox?

My problem gets a little complicated. (I am using PHP)

I have two arrays (a simple array of arrays [0] = string, array [1] = string ...) OK, now I will show the contents of the two arrays on the web page. The first array contains the names and the second URL of the images.

Images and names are already displayed (my problem is not here).

But now I want to do something else, add a flag next to each image, by default this flag r is active. So now the user can uncheck the inbox

The ultimate goal is to get a new array containing only the values โ€‹โ€‹of the names and images that have been verified.

I thought of something simple, scanning the keys (number) of unverified flags and canceling them from my array. But the problem is that I did not know how to deal with checkboxes

+3
source share
3 answers

First of all, I recommend having only one array:

$array = array (0 => array('name' => '....', 'url' => '....'))

I think it will make your life a lot easier. Also in HTML you can also send an array key

foreach ($yourArray as $key=>$value) {
    ...
    <INPUT type="checkbox" name="chkArr[<?php echo $key ?>]" value="1" checked/>          

then in the form action you repeat the intial array and delete the unverified ones.

foreach ($yourArray as $key=>$value) {   
    if (!isset($_POST['chkArr'][$key]) OR $_POST['chkArr'][$key]!='1') {
        unset($yourArray[$key]);  
    }
}
+2
source

To receive input as arrays in PHP, you must set their name using brackets in HTML:

<label><input type="checkbox" name="thename[]" value="" /> The text</label>

Then, when you access $ _REQUEST ['thename'], you will get an array. Check it out to see its format and play with it :)

+5
source
<INPUT type="checkbox" name="chkArr[]" value="$num" checked/>

$_REQUEST ['chkArr'], , .

, , array_diff($listOfAllNums, $chkArr)

0

Source: https://habr.com/ru/post/1705153/


All Articles