PHP validation just doesn't want to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djdante
Forum Newbie
Posts: 4
Joined: Thu Sep 18, 2008 6:08 am

PHP validation just doesn't want to work

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Re: PHP validation just doesn't want to work

Post 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.
djdante
Forum Newbie
Posts: 4
Joined: Thu Sep 18, 2008 6:08 am

Re: PHP validation just doesn't want to work

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: PHP validation just doesn't want to work

Post 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 />
djdante
Forum Newbie
Posts: 4
Joined: Thu Sep 18, 2008 6:08 am

Re: PHP validation just doesn't want to work

Post 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).
Cut
Forum Commoner
Posts: 39
Joined: Sat Aug 23, 2008 8:01 pm

Re: PHP validation just doesn't want to work

Post 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().
djdante
Forum Newbie
Posts: 4
Joined: Thu Sep 18, 2008 6:08 am

Re: PHP validation just doesn't want to work

Post 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" ?
Post Reply