Click to See Complete Forum and Search --> : Validating Froms in PHP


arpit_vavadia
09-28-2008, 09:24 PM
Below is the PHP script , and I want to develope the script such a way that , if data is not entered in some field, then it would redisplay the form and keeping the rest of the entered data as it is .

BELOW IS THE EXACT CODE THAT I HAVE ENTERED .


<html>
<head>
<title> Personal Detail Form </title>
<style type = "text/css">
label {font-family:Arial, sans-serif; font-weight:bold;}
.Text {font-style:italic; }

</style>

</head>
<Body>

<?php
if (!$_POST['submit'])
{
//Write the HTML Code to Display the form
?>

<Form name = "form1" id = "myForm" method= "post" action = "<?=$_SERVER['PHP_SELF']?>">
<Table>
<TR>
<TD> <Label for "TITL"> Title </Label></TD>
<TD><SELECT name = "TITL" id = "TITL" size = "1" Title = "Select Title">
<option> Mr </option>
<option> Mrs </option>
<option> Miss </option>
</SELECT> </TD>
</TR>

<TR>
<TD> <Label for= "FNAME">First Name </Label></TD>
<TD><INPUT TYPE = "text" name = "FNAME" id = "FNAME" SIZE = "25" TITLE = "First Name" CLASS = "Text"></TD>
</TR>

<TR>
<TD><LABEL for = "LNAME">Last Name </Label></TD>
<TD><INPUT TYPE = "text" name = "LNAME" id = "LNAME" SIZE = 20 TITLE= "Last Name" CLASS = "Text"></TD>
</TR>

<TR>
<TD><LABEL for = "UNAME">User Name </Label></TD>
<TD><INPUT TYPE = "text" name = "UNAME" id = "LNAME" SIZE = 20 TITLE= "User Name" CLASS = "Text"></TD>
</TR>

<TR>
<TD><LABEL for = "PASS1">Password </Label></TD>
<TD><INPUT TYPE = "password" name = "PASS1" id = "PASS" SIZE = 20 TITLE= "Password" CLASS = "Text"></TD>
</TR>

<TR>
<TD><LABEL for = "PASS2">Password Again </Label></TD>
<TD><INPUT TYPE= "password" name = "PASS2" id= "PASS2" SIZE = 20 TITLE = "Password Again" CLASS = "Text"></TD>
</TR>
<tr>
<td></td>
<td> <INPUT TYPE = "submit" name = "submit" VALUE = "Send Form ">

</table>
</Form>

<?php
}
else
{
//Retriving the fields

$TITLE = $_POST['TITL'];
$FNAME = $_POST ['FNAME'];
$LNAME = $_POST ['LNAME'];
$UNAME = $_POST ['UNAME'];
$PASS1 = $_POST ['PASS1'];
$PASS2 = $_POST['PASS2'];

//Verfiying that that all the fields are entered
// Second Part of the Exercise
if (empty($FNAME))
{

echo "<br> <b> $FNAME Enter Your First Name <b> <br>";
exit (0);


}
if (empty($LNAME))
{
echo "<br><b> Enter Your Last Name <b> <br>";
exit (-1);
}
if(empty($UNAME))
{
echo "<br> <b>Enter Your User Name <b> <br>";
exit (-1);
}
if (empty($PASS1))
{
echo "<br> <b> Enter Your Password <b><br>";
exit (-1);
}
if(empty ($PASS2))
{
echo "<br><b> Enter Your CONFRIM Password <b><br>";
exit (-1);
}


//Displaying the details

echo "<b> First Name </b> :- $TITLE . $FNAME <BR>";
echo "<B> Last Name </B> :- $LNAME <BR>";
echo "<B> User Name </B> :- $UNAME <BR>";
echo "<B> Password </B> :- $PASS1 <BR>";
echo "<B> Confrim Password </B>:- $PASS2 <BR>";
//exit(0);
}
?>
</Body>
</HTML>

ElvisLives
09-29-2008, 08:20 AM
The problem with hybrid validation (php + html) like this is that you display the html amid php logic. Hard to read and hard to deal with. Check out MVC. However, for sake of he problem, check validation on top if your submit flag exists. Validate and print out messages prior to the HTML. use inline php

<input type="text" name="FIRST_NAME" value="<?= $_REQUEST['FIRST_NAME']; ?>" /> to copy the variable in the request.

After if the form succeeds, either use a location header to redirect to a result page or mark a flag to print out the result HTML and not the form HTML.