14

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?

1
  • 1
    But yeah, this is a quirk in the HTML/HTTP standard, from back when people cared about how long HTTP requests were. Someone thought "Hey, it's blank anyways, we don't need to tell them that, we can save a whole 3 bytes by not sending this information to them!" But you're far from the first or last person to get tripped up by this quirk. Commented Sep 4, 2013 at 1:32

5 Answers 5

23

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.

Sign up to request clarification or add additional context in comments.

Comments

10

This happens because the checkbox's data isn't sent to the server if it is unchecked. A little hack for this is to use a hidden input field with the same name before the checkbox, so if the checkbox is unchecked, that value will be sent instead.

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

3 Comments

Huh, this was the answer I was working on; but I didn't know you could just duplicate the name. I thought you had to set the hidden value using an onSubmit function or something like that...
Well thanks for this. It got it working. But leaves me uncertain about checkboxes. I give it a value in the form in html - used to be 'NO' but now the value will be the zero of the hidden var if it is not checked. Right? So that'd be a 'zero'.
@user577111 Yes it will be 0, but you can change it to anything you want. It can be NO too if you want that.
5

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
}

3 Comments

Isset doesn't tell me what the value is. Only that there is one. Is that the heart of the matter? I should test like a Boolean? Expect a null as a 'negative' and (any) value as a 'positive'? But that throws the error I had and have fixed with the 'hidden' var. Very untidy isn't it? How should I set the box value in html and how should i test in php? Or should I use some other method?
Thank you, should be the accepted answer. Working as expected.
Thank you for the complete answer. This is the one that helped fix my code.
1
<input class="form-check-input" type="hidden" id="" name="check_box" value="0"> 

<input class="form-check-input" type="checkbox" id="gridCheck" name="check_box" value="1" 
<?php if ($_POST['check_box'] == "1") {
  print "checked";
 } ?>>

2 Comments

Here we like you explain in plain text why your code could solve the problem. Pllease take a momnet to consult the guide "How to write a good answer? " here stackoverflow.com/help/how-to-answer
Create 2 input fields and make one hidden... Now add the value to the input fields This will solve the problem..
0
$_POST['wc_use_taxes'] = !isset($_POST['wc_use_taxes'])? "" : $_POST['wc_use_taxes'];

This code would set empty value in posted variable which isn't set and in case its set, that would make it equal to what posted. So i guess doing this would be good idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.