473,387 Members | 1,798 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,387 software developers and data experts.

Repopulate form after validation error

jaymanson
Help - I'm a bit of a newbie when it comes to PHP

I have a form (using POST) which my PHP script validates then returns to either a success or error page. The error page contains the form fields again for the user to resubmit their details. How do I repopulate these fields with their previous submission to save them typing it all in again (as it's likely they won't bother!)

Thanks in advance :-)

Jay
Oct 8 '06 #1
2 5015
ronverdonk
4,258 Expert 4TB
Here is a sample of a form that requires 2 fields, employee name and emplyee number. When one or both are not filled in, display error(s) and re-display the form with the already filled values shown.
[php]
<?php
/*-----------------------------------------------------------------------------*
* This form has 2 parts:
* 1. the first time (_submit not in $_POST) the form is displayed
* to the user requesting input data
* 2. when $_POST['submit'] is set, the form has been filled in
* and the variables in $_POST must be validated.
* Fields filled: continue whatever processing you want to do
* Fields not exist or blank, redisplay form with previous values echoed
*-----------------------------------------------------------------------------*/
if (isset($_POST["_submit"]) ) {
$errors = array();
// validate the input
if (!isset($_POST['empl']) OR strlen(trim($_POST['empl'])) == 0) {
$errors[] = 'Employee number is invalid';
}
if (!isset($_POST['name']) OR strlen(trim($_POST['name'])) == 0) {
$errors[] = 'Employee name is invalid';
}
// further validation of inpuit
//
// ------------------------------------------------------
// Everything is fine, continue processing
// ------------------------------------------------------
if (!$errors) {
// when values valid process further
exit;
}
// ------------------------------------------------------
// Errors encounterd, display them and show form again
// ------------------------------------------------------
else {
print '<span style="color:red"><ul><li><b>';
print implode('</b></li><li><b>',$errors);
print '</b></li></ul></span>';
}
} // End isset submit
?>
<form name='form1' action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Employee number:&nbsp;&nbsp;
<input type ='text' name='empl' maxlength='10' value="<?php echo (isset($_POST['empl'])) ? $_POST['empl'] : ""; ?>" /> <br />
Employee Name:&nbsp;&nbsp;
<input type ='text' name='name' maxlength ='30' value="<?php echo (isset($_POST['name'])) ? $_POST['name'] : ""; ?>" /><br />
<input type ='hidden' name='_submit' value = '1' /> <br />
<input type ='submit' name='submit' value="Submit request"/>
</form>[/php]

Ronald :cool:
Oct 9 '06 #2
Cheers for that - I'm just off to bed but will try this out when I get home from work tomorrow!

Will let you know how I get on!

- Jay
Oct 10 '06 #3

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

Similar topics

1
by: msn | last post by:
Unable to find script library '/aspnet_client/system_web/1_1_4322/WebUIValidation.js Any ideas?
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
1
by: fedrok | last post by:
Hi! I've got a problem with HttpWebRequet class. I've to make a POST http sendind a data stream. This data stream contains some "<" characters and so I get an internal server error 500 described...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
7
by: prakashsurya | last post by:
helllo, d problem is wen d curser jumps frm a combo box 2 a text field in a form d validations r nt working fr d text field....tel me wat shud i consider 2 get out of dis problem....wat r d...
12
by: Gustaf | last post by:
I've been working on a membership form for a while, and find it very tedious to get an acceptable level of form validation. A web search for solutions revealed some home-brewed solutions, such as...
12
by: Vengo | last post by:
Hi, I have a doubt whether it is possible to give the form validation error msg to the tooltip which is used for field description. thanks in advance, cheers, vengo.
2
by: munkee | last post by:
Hi all, Basically I have been using form validation incorrectly. Partly because of laziness which I now feel is going to really bite me back if I dont get it sorted. On all of my forms I have a...
3
by: Taylor Smith | last post by:
I have a validation file and i do not know where to add the focus so that with each error it focuses on the field where the error occured. Maybe someone will have a better idea on how to do this....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.