473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to display Form Errors in same page

ak1dnar
1,584 Expert 1GB
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
4 9014
cassbiz
202 100+
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
4,258 Expert 4TB
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
1,584 Expert 1GB
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
4,258 Expert 4TB
Glad to be of help ajaxrand.

Ronald :cool:
Feb 15 '07 #5

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

Similar topics

12
by: CJ | last post by:
Why won't this work? I am passing the name of the form (I have two that use this validation script) but I keep getting an error. Error reads: "document.which_form.name is null or not an object" ...
1
by: genc_ymeri | last post by:
Hello over there, I would like to generate the textboxes on the fly depending on the number of coulmns in a returning dataset. I tried something like this : TextBox temp = new TextBox();...
3
by: Bob Sanderson | last post by:
I am trying to create a form for a MySQL database similar to a spreadsheet. The idea is to display a list of records, with the last line of the list being an input form. When the user enters data...
7
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form...
2
by: * Tong * | last post by:
Hi, I'm wondering if you can show me an example php page that allows me to retrieve and display 3rd party pages. I.e., when my php script is called as http://site/path/getit.php?file_key it...
18
by: fishwick | last post by:
I haven't really done any css in quite a while, and am banging my head against the wall trying get the rudimentary layout together of a church website home page to display correctly - I don't want...
1
by: William Gill | last post by:
I hope I can state this clearly enough. I am redoing some of my html(php) forms. I'm trying to break things into three functions on a self processing php page: 1) show the form (and populate...
2
by: Tim Streater | last post by:
The following test page is intended to allow the user to choose an image file, and then display it. It works as expected in Safari 3.1.1, FF 2.0.0.14 (Mac), and IE7 (XP). But, it fails in FF...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.