Solved

php cannot get form to keep values if error in posting

Posted on 2010-08-31
7
457 Views
Last Modified: 2012-05-10
I am using this format in all my input box's
<input name="datepicker" value="<?php echo $datepicker;?>"/>
when I post to my page it checks for validation.

if it is not valid it calls die() and is suppose to display the error and redirect back to the page with the users values still in the input box's.
 
but instead it hangs in the error page the redirect doesn't work and when I press the back button all data entered is gone...

any solutions??
if (preg_match('/^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/', $_POST['datepicker'])){
$datepicker = $_POST["datepicker"];
} else {
die('Invalid date');
header("refresh: 4; mywebsite");
}

Open in new window

0
Comment
Question by:Solutionabc
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 27

Expert Comment

by:yodercm
Comment Utility
You have to save the inputs as session variables or form variables when they are first processed, then use them as initial values in the input areas if there was an error and you go back to the form.
0
 

Author Comment

by:Solutionabc
Comment Utility
can you please give an example of how to do that?
0
 
LVL 6

Expert Comment

by:Fahdmurtaza
Comment Utility
In the attached code snipped I have assigned $datepicker value to a session variable of the name 'datepicker'. If you want to use this session variable anywhere in the php application, just use

$_SESSION['datepicker'] to get its value. For example in your form there is a field with name 'datepicker' which might have a input element like

<input name="datepicker" type="text" value="<?=$_SESSION['datepicker']?>">

This attched code snippet simply store the value submitted in form on redirect and will show in the form.

Regards,
Fahd Murtaza
if (preg_match('/^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/', $_POST['datepicker'])){
$datepicker = $_POST["datepicker"];
 $_SESSION['datepicker']=$datepicker;
} else {
die('Invalid date');
header("refresh: 4; mywebsite");
}

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Expert Comment

by:shinuq
Comment Utility
if (preg_match('/^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/', $_POST['datepicker'])){
$datepicker = $_POST["datepicker"];
} else {
die('Invalid date');
header("refresh: 4; mywebsite");
}

Remove this condition, :: die('Invalid date'); Since Die causes the script to halt and stops it from redirecting.

The better option to redrecting is to check the validation in the same page itself.

Put the <form tag to post to the same page.
Then in the top you have your conditions as you had pasted
And then in the input text you have the value

<input name="datepicker" type="text" value="<?php echo $datepicker; ?>" />



Hope this helps.
0
 
LVL 27

Accepted Solution

by:
yodercm earned 500 total points
Comment Utility
<?php

$datepicker = $_POST['datepicker'];

if (preg_match('/^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/', $_POST['datepicker']))
{
   $datepicker = $_POST["datepicker"];
   include "correctprocessing.php";
}
else
{
   $datepicker = NULL;
 ?>

<form type=post action='PHP_SELF'>
<input name="datepicker" type="text" value="<?php echo $datepicker; ?>" />
</form>

<?php
}

This method allows you to use a single script instead of redirecting, and just include the script that processes a correctly entered date.
0
 

Author Comment

by:Solutionabc
Comment Utility
so if I remove die() and submit to the same page how do I ensure that if the input is invalid to kill the process so that it does not go any further and is not entered into the database?

thanks.
0
 
LVL 17

Expert Comment

by:shinuq
Comment Utility
<?php
$validdate=false;
if (preg_match('/^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/', $_POST['datepicker'])){
   $datepicker = $_POST["datepicker"];
   
   $validdate=true;
}

if($validdate){

    //code to update the database
}

?>
<input name="datepicker" type="text" value="<?php echo $datepicker; ?>" />


Hope this helps
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now