...

View Full Version : How do I validate radio buttons in PHP?



TheDumbNerd
11-14-2011, 11:13 PM
Ok, so I have one document here called test.php and I want to validate my radio buttons so that when the user is filling out the form and clickes submit but didn't pick one of the radio buttons, then the data gets sent back to the original form and an error message appears beside the form.

Here's my first document "test.php"


<?php
@$semester=$_GET["semester"];
@$semestererror=$_GET["semestererror"];


?>
<html>

<h1>Entry Form</h1> <!--My title-->
</html>
<?php
if(isset($semester)==true)
$semestererror="*Please select a semester";
else
$semestererror="&nbsp";

?>
<!--My form-->
<form name="Stumarks" action="test2.php" method="GET">
<p><font color="red"><?php echo $semestererror; ?></font></p>Semester: <br />
<input type="radio" name="semester" value="fall" />Fall<br /> <?php if ($semester="fall") echo "checked="true""; ?><!--Radio Buttons-->
<input type="radio" name="semester" value="spring" />Spring<br /> <?php if ($semester="spring") echo "checked="true""; ?>
<input type="submit" value="submit"/>
</form>

And here's my second document "test2.php"


<?php
@$semester=$_GET["semester"];
@$semestererror=$_GET["semestererror"];

if ($semester="" or $semestererror=0)
{header ("location: test.php?semester=$semester&&semestererror=$semestererror");}

if ($semester="fall" or $semester="spring" or $semestererror=1)

echo "<table border=1>";
echo
"<th> <h2>".semester."</h2> </th>
";

echo "</table>";

?>

I just don't know what i did wrong?

mlseim
11-15-2011, 12:19 AM
First of all, in an "if" statement use two equal signs for compare, example:

if ($semester=="fall" or $semester=="spring" or $semestererror==1)

That's as far as I got ... gotta leave the house for a while.

TheDumbNerd
11-15-2011, 12:50 AM
First of all, in an "if" statement use two equal signs for compare, example:

if ($semester=="fall" or $semester=="spring" or $semestererror==1)

That's as far as I got ... gotta leave the house for a while.

I did that, and it didn't make a difference!

mlseim
11-15-2011, 02:24 AM
repost it again ... with your changes.

repost both scripts ... because there were errors in the other as well.

felgall
11-15-2011, 06:46 AM
You should be using isset($_GET["semester"]) to determine if a radio button was selected. If no button was selected then the variable will not exist.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum