PHP validation just doesn't want to work
Posted: Thu Sep 18, 2008 6:26 am
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"> in a sociable suburb <br>
<input name="q1" value="x" type="radio"> in between<br>
<input name="q1" value="-a" type="radio"> 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.
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"> in a sociable suburb <br>
<input name="q1" value="x" type="radio"> in between<br>
<input name="q1" value="-a" type="radio"> 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.