Validating forms with an error message PHP?
Posted: Sun Nov 13, 2011 4:07 pm
Well I have two documents, one called "test.php" and the other "test2.php". I want to validate my First Name form so that when someone enters something that is not alphabetic, or they leave it black then you get redirected back to test.php with the values that you have entered still in the form but an error message has appeared beside the form.
"test.php"
"test.php"
Code: Select all
<?php
@$fname=$_GET["fname"];
@$fnameerror=$_GET["fnameerror"];
if (isset($fname)==true)
$fnameerror="*";
else
$fnameerror="Testing";
?>
<form action="test2.php" method="GET">
<p>
First Name: <br /> <input type="text" name="fname" value="<?php echo $fname; ?>"/> <br/></input><font color="red"><?php echo $fnameerror; ?></font></p>
</p>
<p><input type="submit" value="submit" /> </p>
</form>
Here's my other document "test2.php"
<html>
<h1>Academic Report Card</h1>
</html>
<?php
@$fname=$_GET["fname"];
@$fnameerror=$_GET["fnameerror"];
if ($fname==""
or ctype_alpha($fname)==False or $fnameerror=1)
echo ".$fnameerror."
; header ("location: test.php?fname=$fname&&fnameerror=$fnameerror");
if ($fnameerror=0)
echo "<table border=1>";
echo
"<th> <h2>".$fname." </h2> </th>
";
echo "</table>";
?>