Page 1 of 1

PHP validation just doesn't want to work

Posted: Thu Sep 18, 2008 6:26 am
by djdante
Hi all, I have been building a complex personality test for my website. I have learned all the php from scratch and it is running smoothly except for one problem... If someone forgets to select a radio box, I get an error returned on the page that the answers were posted to as non-checked radio buttons return 'null' values (or so I believe).

I have tried to use isset

if(isset($_POST["q1"]){
$q1 = ($_POST["q1"]);
}
else {
$q1 = 0;
}


A 0 value for an unchecked button would be good enough. But it doesn't seem to work.

I have also read that I could verify my form server-side using java, but despite hours on end of cutting/pasting manipulating code from the forums and websites i've been searching through, I can't seem to get the code to respond to javascript at all.

So I put out the question.. If I create a simple example of test.php

<form name="form1" method="post" action="test2.php">
1) I would rather have a house:<br>



<input name="q1" value="a" type="radio">&nbsp; &nbsp; &nbsp; &nbsp;in a sociable suburb <br>

<input name="q1" value="x" type="radio">&nbsp; &nbsp; &nbsp; &nbsp;in between<br>

<input name="q1" value="-a" type="radio">&nbsp; &nbsp; &nbsp; &nbsp;alone in the deep woods<br>

<br>

<br>


<input name="Submit1" value="Continue with test" type="submit"></form>


And I have a test2.php

$q1 = $_POST['q1'];


echo $q1;


What can I do to verify this simple form, or at least set it to a 0 value rather than 'null'?

I should also mention that I am running a session in the full quiz, but I doubt that should have any impact.

Re: PHP validation just doesn't want to work

Posted: Thu Sep 18, 2008 6:33 am
by Maugrim_The_Reaper
Maybe it's just empty...

Code: Select all

if(isset($_POST["q1"]) && empty($_POST["q1"])){
Being Null, and being Empty are two distinct states with separate checks. Empty in this case is defined as the value being 0, null, false, an empty string, etc. isset() merely check if the variable even exists (null or not) so it's usually not sufficient by itself, but is still required since empty() will error out if the variable passed to it doesn't exist.

Re: PHP validation just doesn't want to work

Posted: Thu Sep 18, 2008 7:02 am
by djdante
REAPER:

Interesting concept, I tried punching the info in as follows..


if(isset($_POST["q1"]) && empty($_POST["q1"])){
$q1 = $_POST['q1'];}
else{
$q1 = 0;
}

echo $q1;


However, it just eternally returns 0, even if I HAVE checked one of the boxes. This was the trouble I had with isset.
Also, when I remove the else{$q1 =0;} section, it returns an unidentified value error, suggesting to me that the isset section simply gets skipped every time as it is written right now.

Re: PHP validation just doesn't want to work

Posted: Thu Sep 18, 2008 10:24 am
by Jade
Is the radio box named q1? That's the only reason I could see that it's not picking anything up. All your radio boxes should have the exact same name (if they're a part of a set) and different values. If you want, you could make one selected by default (so they can't leave the set blank) by putting <option value="radio" name="q1" value="1" checked />

Re: PHP validation just doesn't want to work

Posted: Thu Sep 18, 2008 5:53 pm
by djdante
Yes, they all have the same name, and different values. All three are named q1, the values are a, x, -a respectively (the letters are not arbitrary, thye have meaning further on down the line).

Re: PHP validation just doesn't want to work

Posted: Thu Sep 18, 2008 6:14 pm
by Cut
djdante wrote:REAPER:

Interesting concept, I tried punching the info in as follows..


if(isset($_POST["q1"]) && empty($_POST["q1"])){
$q1 = $_POST['q1'];}
else{
$q1 = 0;
}

echo $q1;


However, it just eternally returns 0, even if I HAVE checked one of the boxes. This was the trouble I had with isset.
Also, when I remove the else{$q1 =0;} section, it returns an unidentified value error, suggesting to me that the isset section simply gets skipped every time as it is written right now.
empty() returns true if the variable is empty, so you should be using !empty().

Re: PHP validation just doesn't want to work

Posted: Fri Sep 19, 2008 3:59 am
by djdante
HURRAH!

Thankyou ever so much, that seems to have done the trick!

I'm still not quite sure what's going on in the line
if(isset($_POST["q1"]) && empty($_POST["q1"])){

Is that saying.. "IF $_POST["q1"] OR it's and empty field" ?