473,769 Members | 6,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to display Form Errors in same page

ak1dnar
1,584 Recognized Expert Top Contributor
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"><di v align="left">Fi rst Name<span class="style1"> * </span></div></th>
<th width="144" scope="row"><di v align="left">
<input name="fn" type="text" id="fn" />
</div></th>
</tr>
<tr>
<th scope="row"><di v align="left">La st Name<span class="style1"> * </span></div></th>
<th scope="row"><di v align="left">
<input name="ln" type="text" id="ln" />
</div></th>
</tr>
<tr>
<th scope="row"><di v align="left">em ail<span class="style1"> *</span></div></th>
<th scope="row"><di v align="left">
<input name="email" type="text" id="email" />
</div></th>
</tr>
<tr>
<th scope="row"><di v align="left"></div></th>
<th scope="row"><di v align="left"></div></th>
</tr>
<tr>
<th scope="row"><di v align="left"></div></th>
<th scope="row"><di v align="left">
<input type="submit" name="Submit" value="Submit" />
</div></th>
</tr>
</table>
</form>
</body>
</html>


[/PHP]
Feb 14 '07 #1
4 9049
cassbiz
202 New Member
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 Recognized Expert Specialist
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:re d"><ul><li><b>' ;
print implode('</b></li><li><b>',$er rors);
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"><di v align="left">Fi rst Name<span class="style1"> * </span></div></th>
<th width="144" scope="row"><di v align="left">
<input name="fn" type="text" id="fn" value="<?php echo (isset($_POST['fn'])) ? $fn : ""; ?>" />
</div></th>
</tr>
<tr>
<th scope="row"><di v align="left">La st Name<span class="style1"> * </span></div></th>
<th scope="row"><di v align="left">
<input name="ln" type="text" id="ln" value="<?php echo (isset($_POST['ln'])) ? $ln : ""; ?>" />
</div></th>
</tr>
<tr>
<th scope="row"><di v align="left">em ail<span class="style1"> *</span></div></th>
<th scope="row"><di v align="left">
<input name="email" type="text" id="email" value="<?php echo (isset($_POST['email'])) ? $email : ""; ?>" />
</div></th>
</tr>
<tr>
<th scope="row"><di v align="left"></div></th>
<th scope="row"><di v align="left"></div></th>
</tr>
<tr>
<th scope="row"><di v align="left"></div></th>
<th scope="row"><di v 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 Recognized Expert Top Contributor
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 Recognized Expert Specialist
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
2471
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" HTML----------- Form is ----> <form action="thanks.php" method="post" name="contact_form" id="contact_form"> Name -------> <input type="text" name="name" id="name" size="25"> Button sends code -----> <input type="button" value="Submit Form"
1
1304
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(); MyPage.Controls.Add(temp); // <-- the above one failed saying I should specify when this is already a server comp
3
3128
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 in the form and hits the submit button, the data is entered and the form is reloaded with the new data displayed and another input form becomes the last line. Example --- Before entering new data
7
6998
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 is correct I need to submit to another page that uses the form data. I first tried making the form submit action= field point to the same file. When the form was correct, I tried loading the next page by using <META http-equiv refresh>. But...
2
1776
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 can retrieve from http://3rd.party/path2/name-with-file_key.htm and display the result back to the browser. I don't know php, and start to learning it. So I hope you can give me a full
18
2499
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 to continue work until I understand what I'm doing wrong. http://www.christchurchinfo.net The site displays nicely in Firefox/Mozilla and Safari, but the stained glass windows navigation portion is getting pushed down in IE, and the "news"...
1
3954
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 fields if there were errors) 2) validate form data 3) process form data 2 and 3 are no problem, but 1 is. The page with the form displayed should validate as html 4.01 strict (actually all iterations of the page
2
8338
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 2.0.0.14 (Win-XP) - the image doesn't appear. The error console shows no errors and Page Info, under the media tab, shows no loaded image (in contrast to FF Mac which shows the image). The images I select are all jpegs, btw.
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.