Índice indefinido si no marco la casilla de verificación

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:

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

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

$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.

¿Alguien puede ayudarme?

preguntado el 04 de septiembre de 13 a las 02:09

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 Bytes 3 by not sending this information to them!" But you're far from the first or last person to get tripped up by this quirk. -

5 Respuestas

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

Tienes que usar isset($_POST['publishok']) to check it whether it is checked in the server side.

Respondido el 04 de Septiembre de 13 a las 02:09

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">

Respondido el 04 de Septiembre de 13 a las 02:09

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... - Retsam

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

@user577111 Yes it will be 0, but you can change it to anything you want. It can be NO too if you want that. - totymedli

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:

    $publishok = $_POST['publishok'];
    $publishok = "";#default value

Respondido el 04 de Septiembre de 13 a las 02:09

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? - user577111

Thank you, should be the accepted answer. Working as expected. - jodyshop

Thank you for the complete answer. This is the one that helped fix my code. - Hotelesinger

<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";
 } ?>>

Respondido el 08 de enero de 20 a las 07:01

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 - Guillaume Raimundo

Create 2 input fields and make one hidden... Now add the value to the input fields This will solve the problem.. - Kwed

$_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.

contestado el 19 de mayo de 20 a las 07:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.