Solved

php cannot get form to keep values if error in posting

Posted on 2010-08-31
7
462 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
ID: 33572875
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
ID: 33572883
can you please give an example of how to do that?
0
 
LVL 6

Expert Comment

by:Fahdmurtaza
ID: 33573004
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 33574402
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
ID: 33575919
<?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
ID: 33576402
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:Shinesh Premrajan
ID: 33576478
<?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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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.

770 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