Solved

php cannot get form to keep values if error in posting

Posted on 2010-08-31
7
467 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I think I know what this error means, but I need some other eyes on it... 32 49
AJAX pass along a variable 3 46
php help 34 58
Wordpress Security 29 48
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.

856 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