Determine if php $ _GET is checked

I just want php to determine if the checkbox is checked, but I ran into the problem of getting the correct return. Help me please.

My html code

    <label>
    <input name="Language" type="checkbox" id="aa"  checked="checked"  />
    One</label>
    <label>
    <input name="Language" type="checkbox" id="bb" />Two</label>
    <label>
   <input type="checkbox" name="Language"  id="cc" />Three</label>

I pass php values ​​with the $ _GET method

my php code is:

$aa=$_GET["aa"];
$bb=$_GET["bb"];
$cc=$_GET["cc"];


echo $aa."<br>";
echo $bb."<br>";
echo $cc."<br>";

exit true false lie

Then I just want to determine if each cell is checked, and if so, do something.

if ($aa == true) { $abc="checked";}
else { $abc="not checked"; }

if ($bb == true) { $cde="checked";}
else { $cde="not checked"; }


if ($fgh == true) { $fgh="checked";}
else { $fgh="not checked"; }

But if statements always return true, even if the checkbox is unchecked. I tried the options "===" and "! =", But it does not work.

TIA

+3
source share
3 answers
if (isset($_GET['checkbox_name'])) { ... }

( ) . POST, .

- ( , PHP).

, , IIRC, on, , , . isset . , , , , .

, - PHP [], .

, - :

<label>
<input name="Language[]" type="checkbox" id="aa"  checked="checked" value="One" />
One</label>
<label>
<input name="Language[]" type="checkbox" id="bb" value="Two" />Two</label>
<label>
<input type="checkbox" name="Language[]"  id="cc" value="Three" />Three</label>

. .

PHP $_GET['Language'] , .

+4

isset()

, HTML-

<label>
  <input name="Language[]" type="checkbox" id="aa" checked="checked" value ="1" />One    
</label>
<label>
  <input name="Language[]" type="checkbox" id="bb" value ="2" />Two
</label>
<label>
  <input name="Language[]" type="checkbox"  id="cc" value ="3" />Three
</label>

, -

$count = count($_GET["Language"]); .

do

$arr = $_GET["Language"]; //$arr is an array that contains the values of the checked checkboxes

foreach

   foreach( $arr as $item)
   {
        echo $item . "</br>"; /* Will print 1,2 and 3 (mind newlines)*/
   }
+1

In my PHP, I got it from $_POST['checkbox_name'], and I found that the variable mattered onwhen the flag was set (i.e. it existed even if the flag was clear).

0
source

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


All Articles