Undefined Index If I do not check the box

I think I have a strange incident. If I checked the box in the form, then the PHP script works fine. If I do not check the checkbox php reports an undefined index for another variable.

This is using the local IIS host, checking things.

On the Internet, published identical work scripts work no matter what. Well, almost the same. I locally added the variable "test" POST-ed to php and compared with a hard-coded value. It's all.

Here's the html for the checkbox:

<tr> <td>Publish Comment?</td> <td><input name="publishok" value="NO" type="checkbox"> Check Box For Do spanstyle="font-weight: bold;">Not</span> Publish</td> </tr> <tr> 

and here is the php for the variable, 'publishok':

 $IP = $_SERVER["REMOTE_ADDR"]; $publishok = $_POST['publishok']; $test = $_POST['test']; if ($test != 'park') die ("Wrong input. Sorry. Go back, try again"); 

I suspected that my PSPad editor was adding false (and invisible) char codes or something else, so I upgraded to the latest version. No difference.

I can’t think what might cause this.

Can anyone help?

+4
source share
3 answers
Check box

will not send data to the server when you did not check it.

You should use isset($_POST['publishok']) to check if it is checked on the server side.

+9
source

This is because the flag data is not sent to the server if it is not set. A small hack for this is to use a hidden input field with the same name in front of the flag, so if the flag is unchecked, this value will be sent instead.

 <input name="publishok" value="0" type="hidden"> <input name="publishok" value="NO" type="checkbox"> 
+6
source

I believe that checkboxes and radio buttons do not send get / post data if they are not selected / checked (you can check this by doing var_dump / print_r on $ _GET / $ _ POST), so you should do something like:

 if(isset($_POST['publishok'])){ $publishok = $_POST['publishok']; }else{ $publishok = "";#default value } 
0
source

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


All Articles