Solved

php cannot get form to keep values if error in posting

Posted on 2010-08-31
7
475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 27

Expert Comment

by:Cornelia Yoder
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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:
Cornelia Yoder 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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

624 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