Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 10th, 2004, 11:56 PM
Authorized User
Join Date: Oct 2003
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default go back previous page

How am i going to code the php page to be able to go back a previous page.
For example, if i have a employee.php where the user have to enter the name, address, phone, date of birth and etc. After the user clicked the submit button, the newemployee.php will checked if the user entered all the data before a new employee details will be entered in the database.
If the user does not enter the date of birth, i want to go back to the employee.php page which display the previously uncompleted entered data.

Old February 11th, 2004, 10:51 AM
Friend of Wrox
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts

You could echo some javascript if the user fails:

if($success == "failure")
echo "Failure.<script language=javascript>setTimeout('window.history.go(-1)',2000);</script>";

This is the method I use on my pages.

Old February 11th, 2004, 01:50 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts

This is exactly why most PHP scripts that generate forms are also responsible for receiving and validating the input of those forms.

If all the input is valid, then you redirect to the page that will process (e.g. insert into database, etc) the data.

If any of the data is invalid, then you flag the fields that were invalid and regenerate the form with error messages.

What's nice about this approach is that you can pre-populate all the fields of the form that contained valid info -- this spares the user of having to fill in ALL the fields when only one or two of them might be bad.

Here's the flow of logic:


if (the user submitted data)
  for each field in the form
    is the user input valid?
      if not, add this field to your "error fields" list.

  if your "error fields" list is empty,
    process the data (or redirect to your processing script)
    and exit.

// if we got here, we need to generate the form.

generate the form. Output any error messages and highlight
any fields in your "error fields" list.

If any previously submitted data exists, populate the form
fields with the submitted value. Don't do this for fields
in your "error fields" list.


Hope this makes sense!

Take care,


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to expire previous page johnsonlim026 ASP.NET 1.0 and 1.1 Basics 6 May 22nd, 2009 12:05 PM
Find previous page gandaliter PHP How-To 2 June 7th, 2007 11:55 AM
Page 1-2-3-4 etc.... Next and Previous buttons crmpicco Classic ASP Basics 1 March 18th, 2005 12:00 PM
Checking Previous Page shs BOOK: Beginning ASP.NET 1.0 2 September 23rd, 2004 11:01 PM
next and previous page results mikeuk PHP Databases 1 July 19th, 2004 05:15 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.