?
Solved

php cannot get form to keep values if error in posting

Posted on 2010-08-31
7
Medium Priority
?
481 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 2000 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this article, we’ll look at how to deploy ProxySQL.
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 count occurrences of each item in an array.

719 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