By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

How to display Form Errors in same page

ak1dnar
Expert 100+
P: 1,584
Hi, I have created a PHP page that write down form data to mySQL table.
Before i submit data I want to check the Input field, whether it has filled up or Not.
the form should validate from the PHP itself Not from JS or any other client side scripting.

when i submit the form [page1.php] by using $PHP_SELF i can trap the errors, but it will delete previously entered values from the page and display empty input boxes.

So what i need to do I want display errors with the Incorrect values, we will say that i am checking the email id whether in the correct format or not.error should display in the header area of the page and incorrect value still should remain in the input box.

Can i do this by iframes. or is there any better way for this...

[PHP]
<?
//PHP Code Goes Here
?>

<html >
<head>
</head>

<body>
<p>Errors Should Display Here </p>
<form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
<table width="315" border="0" cellspacing="0" cellpadding="0">
<tr>
<th width="69" scope="row"><div align="left">First Name<span class="style1">* </span></div></th>
<th width="144" scope="row"><div align="left">
<input name="fn" type="text" id="fn" />
</div></th>
</tr>
<tr>
<th scope="row"><div align="left">Last Name<span class="style1">* </span></div></th>
<th scope="row"><div align="left">
<input name="ln" type="text" id="ln" />
</div></th>
</tr>
<tr>
<th scope="row"><div align="left">email<span class="style1">*</span></div></th>
<th scope="row"><div align="left">
<input name="email" type="text" id="email" />
</div></th>
</tr>
<tr>
<th scope="row"><div align="left"></div></th>
<th scope="row"><div align="left"></div></th>
</tr>
<tr>
<th scope="row"><div align="left"></div></th>
<th scope="row"><div align="left">
<input type="submit" name="Submit" value="Submit" />
</div></th>
</tr>
</table>
</form>
</body>
</html>


[/PHP]
Feb 14 '07 #1
Share this Question
Share on Google+
4 Replies


cassbiz
100+
P: 202
You can use a javascript for checker to make sure that the fields are not blank

Expand|Select|Wrap|Line Numbers
  1.         <script type="text/javascript">
  2.                 <!--
  3.                 function cForm()
  4.                 {
  5.                         if(document.formname.name.value == "")
  6.                         {
  7.                                 alert("Enter Name");
  8.                                 document.formname.name.focus();
  9.                                 return false;
  10.                         }
  11.  
  12.                 }
  13.                 //-->
  14.         </script>
  15.  
Then you just place an onSubmit in your form area

Expand|Select|Wrap|Line Numbers
  1. <form name="formname" onSubmit="return cForm()">
  2.  
Since PHP is a server side, to check a form before it is submitted you can use JavaScript which is client side.

Hope this helps

Good Luck
Feb 14 '07 #2

ronverdonk
Expert 2.5K+
P: 4,258
This is a PHP forum, so I'll give a PHP reply. Since it is a bit difficult to show exactly what should be coded at what place in the code, I have added the required function to your original code.

It only checks for existence and non-blank content of the submitted fields. You yourself have to add the checks for valid email address and the content of the other fields.

[php]<?php
//PHP Code Goes Here
$errors = array();
if (isset($_POST['Submit'])) {
if (!isset($_POST['fn']) OR trim(strip_tags($_POST['fn'])) == '' )
$errors[] = 'Invalid first name';
else
$fn = trim(strip_tags($_POST['fn']));
if (!isset($_POST['ln']) OR trim(strip_tags($_POST['ln'])) == '' )
$errors[] = 'Invalid last name';
else
$ln = trim(strip_tags($_POST['ln']));
if (!isset($_POST['email']) OR trim(strip_tags($_POST['email'])) == '' )
$errors[] = 'Invalid email';
else
$email = trim(strip_tags($_POST['email']));
// check if any errors set
if (!$errors) {
//.... continue processing your form
exit;
} // end IF errors

} // end IF (SUBMITted)

// (re)display the form
?>
<html>
<head>
</head>
<body>
<?php
// print out the errors
if ($errors) {
print '<span style="color:red"><ul><li><b>';
print implode('</b></li><li><b>',$errors);
print '</b></li></ul></span>';
}
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table width="315" border="0" cellspacing="0" cellpadding="0">
<tr>
<th width="69" scope="row"><div align="left">First Name<span class="style1">* </span></div></th>
<th width="144" scope="row"><div align="left">
<input name="fn" type="text" id="fn" value="<?php echo (isset($_POST['fn'])) ? $fn : ""; ?>" />
</div></th>
</tr>
<tr>
<th scope="row"><div align="left">Last Name<span class="style1">* </span></div></th>
<th scope="row"><div align="left">
<input name="ln" type="text" id="ln" value="<?php echo (isset($_POST['ln'])) ? $ln : ""; ?>" />
</div></th>
</tr>
<tr>
<th scope="row"><div align="left">email<span class="style1">*</span></div></th>
<th scope="row"><div align="left">
<input name="email" type="text" id="email" value="<?php echo (isset($_POST['email'])) ? $email : ""; ?>" />
</div></th>
</tr>
<tr>
<th scope="row"><div align="left"></div></th>
<th scope="row"><div align="left"></div></th>
</tr>
<tr>
<th scope="row"><div align="left"></div></th>
<th scope="row"><div align="left">
<input type="submit" name="Submit" value="Submit" />
</div></th>
</tr>
</table>
</form>
</body>
</html>[/php]

Good luck with it!

Ronald :cool:
Feb 14 '07 #3

ak1dnar
Expert 100+
P: 1,584
Thanks Ronald :)
its working fine as per my requirement.
Now I can show the Errors with out losing the invalid input that enter by the user.
Feb 15 '07 #4

ronverdonk
Expert 2.5K+
P: 4,258
Glad to be of help ajaxrand.

Ronald :cool:
Feb 15 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.