Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined Index If I Do Not Check Checkbox

I seem to have a bizarre happening here. If I check a checkbox on the form then the php script runs perfectly. If I don't check the box php reports an undefined index on another variable.

That's using IIS localhost, checking things.

On the web the published identical script works no matter what. Well, virtually identical. I have locally added a variable 'test' POST-ed to php and compared with a hard coded value. That'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's 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 my editor PSPad was adding spurious (and invisible) char codes or something so I upgraded to the latest version. No difference.

Can't think what could cause this.

Can anyone help?

like image 955
user577111 Avatar asked Sep 04 '13 01:09

user577111


2 Answers

I believe checkboxes and radio buttons don't send a get/post data if not selected/checked (you can verify this by doing a var_dump/print_r on $_GET/$_POST) so you should do something like:

if(isset($_POST['publishok'])){
    $publishok = $_POST['publishok'];
}else{
    $publishok = "";#default value
}
like image 112
Class Avatar answered Oct 19 '22 06:10

Class


checkbox won't send the data to the server when you did not check it.

You have to use isset($_POST['publishok']) to check it whether it is checked in the server side.

like image 27
xdazz Avatar answered Oct 19 '22 07:10

xdazz