PDA

View Full Version : PHP form history not saved in IE


gentleone
11-04-2010, 08:46 AM
This is bugging me already for 2 days and I'm lost.

I have a PHP form which uses server side validation. The errors (missing fields) will be printed in a div. Due to the lack of space I had to be creative so I made a pure CSS based lightbox effect which opens that error div in a overlay box centered on the form. Underneath the errors I made a 'close' link to send the user back to the form to fill in the missing fields.

<a href="#" onclick="history.go(-1);return false;">close</a>
On all the form elements I have also this:

<input type="text" name="full-name" id="full-name" value="<?php if(isset($_POST['full-name'])) echo htmlentities($_POST['full-name']);?>" />
When closing the overlay box all the cool browsers refreshes the page with the form, but they remember the users data on the form fields so the user has only to fill in the missing fields.
Like with many things is IE the party pooper. All the fields are empty again after clicking 'close', so the user has to fill in everything again.

Does anyone know how to workaround this IE thing, or is there perhaps a better alternative to display the error div?

Sorry... I can't place a link tot he site :(

davidj
11-04-2010, 11:12 AM
I think its a client/browser setting which you shouldnt rely on

save the form data to a session and repopulate from that if its set

gentleone
11-04-2010, 11:44 AM
Thanks David!

To save the form data, do I need a database? There is no database behind the site. it's a form to email script. Or can I also use cookies?

davidj
11-04-2010, 12:20 PM
just use a $_SESSION

It stores the info server side


session_start();

$_SESSION['formdata'] = $_POST;

// Then access the info through...

$_SESSION['formdata']['formfield'];

gentleone
11-04-2010, 12:56 PM
Great! I will give it try.

gentleone
11-04-2010, 09:08 PM
I've tried the code David, but with no luck in IE. This is how I tested it with tthe first 2 fields fields in my formprocess PHP file. Where do I go wrong?

<?php
if ('dd/rates-booking.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('<h2>Direct File Access Prohibited</h2>');

session_start();

$_SESSION['formdata'] = $_POST;

$_SESSION['formdata']['full-name'];
$_SESSION['formdata']['nationality'];

if(isset($_POST['sent'])) {
$error = Array();
if(!trim($_POST['full-name'])) $error[] = "Full name";
if(!trim($_POST['nationality'])) $error[] = "Nationality";
if(!trim($_POST['age'])) $error[] = "Age";
if($_POST['booked-before'] == false) $error[] = "Have you booked with us before?";
if(!preg_match("/[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9]{2,4}/",$_POST['email'])) $error[] = "E-mail address";
if(!preg_match("/^\+(?:[0-9] ?){6,14}[0-9]$/",$_POST['phone'])) $error[] = "Telephone number";
if($_POST['escort'] == "Select One") $error[] = "Who would you like to see?";
if(!trim($_POST['date'])) $error[] = "Proposed date";
if($_POST['payment'] == false) $error[] = "How would you like to pay?";
if($_POST['location'] == false) $error[] = "Booking location";
if(!trim($_POST['address'])) $error[] = "Hotel name/Residents address";
if(!trim($_POST['city'])) $error[] = "City";
if(!trim($_POST['country'])) $error[] = "Country";
if(!count($error)) {
if ($_POST['cc'] == true) { $flag = 2; } //Checks to see if the EU "ticked" the carbon-copy box
$receiver = "info@domain.nl";
$subject = "Booking Form Submission"; //Subject EU selected will display as subject line of email
$carbonsubject = "Carbon-copy: Booking Form Submission"; //Subject for email of the carbon copy the EU will receive
$sender = "From: ".$_POST['email']."\r\n"; //Uses the users email address as the sender of the email


/* Enters the form data into the body of the email. */
$body = "The following booking information was submitted:\r\n\r\n";
$body .= "Name: ".stripslashes($_POST['full-name'])."\r\n";
$body .= "Nationality: ".stripslashes($_POST['nationality'])."\r\n";
$body .= "Age: ".stripslashes($_POST['age'])."\r\n";
$body .= "Have you booked with us before?: ".$_POST['booked-before']."\r\n";
$body .= "E-mail: ".$_POST['email']."\r\n";
$body .= "Phone number: ".stripslashes($_POST['phone'])."\r\n";
$body .= "How did you find us?: ".$_POST['referral']."\r\n";
$body .= "Who would you like to see?: ".$_POST['escort']."\r\n";
$body .= "Alternative choice: ".$_POST['alternative']."\r\n";
$body .= "Proposed date: ".stripslashes($_POST['date'])."\r\n";
$body .= "Proposed time: ".stripslashes($_POST['time'])."\r\n";
$body .= "Desired duration: ".stripslashes($_POST['duration'])."\r\n";
$body .= "How would you like to pay?: ".$_POST['payment']."\r\n";
$body .= "Booking location: ".$_POST['location']."\r\n";
$body .= "Hotel name/Residents address: ".stripslashes($_POST['address'])."\r\n";
$body .= "City: ".stripslashes($_POST['city'])."\r\n";
$body .= "Country: ".stripslashes($_POST['country'])."\r\n";
$body .= "Your special requests or remarks:\r\n".preg_replace("(\r\n|\r|\n)","\r\n",stripslashes($_POST['remarks']))."\r\n";

/* Builds the message for the carbon-copy response to the EU */
$carbonbody = "Dear ".$_POST['full-name'].",\r\n\r\n";
$carbonbody .= "Thank you for your booking request. Your information has been successfully sent to our database and you will be receiving a response from us as soon as possible. Below is a carbon-copy of your booking request. \r\n\r\n";
$carbonbody .= "------------------------------------------------ \r\n\r\n";
$carbonbody .= "Name: ".stripslashes($_POST['full-name'])."\r\n";
$carbonbody .= "Nationality: ".stripslashes($_POST['nationality'])."\r\n";
$carbonbody .= "Age: ".stripslashes($_POST['age'])."\r\n";
$carbonbody .= "Have you booked with us before?: ".$_POST['booked-before']."\r\n";
$carbonbody .= "E-mail: ".$_POST['email']."\r\n";
$carbonbody .= "Phone number: ".stripslashes($_POST['phone'])."\r\n";
$carbonbody .= "How did you find us?: ".$_POST['referral']."\r\n";
$carbonbody .= "Who would you like to see?: ".$_POST['escort']."\r\n";
$carbonbody .= "Alternative choice: ".$_POST['alternative']."\r\n";
$carbonbody .= "Proposed date: ".stripslashes($_POST['date'])."\r\n";
$carbonbody .= "Proposed time: ".stripslashes($_POST['time'])."\r\n";
$carbonbody .= "Desired duration: ".stripslashes($_POST['duration'])."\r\n";
$carbonbody .= "How would you like to pay?: ".$_POST['payment']."\r\n";
$carbonbody .= "Booking location: ".$_POST['location']."\r\n";
$carbonbody .= "Hotel name/Residents address: ".stripslashes($_POST['address'])."\r\n";
$carbonbody .= "City: ".stripslashes($_POST['city'])."\r\n";
$carbonbody .= "Country: ".stripslashes($_POST['country'])."\r\n";
$carbonbody .= "Your special requests or remarks:\r\n".preg_replace("(\r\n|\r|\n)","\r\n",stripslashes($_POST['remarks']))."\r\n";
$carbonbody .= "------------------------------------------------ \r\n\r\n";
$carbonbody .= "Sincerely,\r\n";
$carbonbody .= "Company\r\n\r\n";
$carbonbody .= "info@domain.nl\r\n";
$carbonbody .= "http://www.domain.nl\r\n\r\n";
$carbonbody .= "This e-mail is automated, so please DO NOT reply.\r\n";

/* Send an alert to your cell phone once someone submits your form! */
$cellreciever = "number@domain";
$cellsubject = "Alert";
$cellsender = "";
$cellbody = "Someone submitted your contact form!"; //For standard SMS, text message must be under 160 characters

/* Generates strings for the automated reply email once the user submits the form */
$reply = $_POST['email'];
$replysubject = "Auto-Reply: Booking Form Submission";
$replyfrom = "From: no.reply@domain.nl\r\n";
$replymessage = "Dear ".$_POST['full-name'].",\r\n\r\n";
$replymessage .= "Thank you for your booking request for. Your information has been successfully sent to our database and you will be receiving a response from us as soon as possible.\r\n\r\n\r\n\r\n";
$replymessage .= "Sincerely,\r\n";
$replymessage .= "Company\r\n\r\n";
$replymessage .= "info@domain.nl\r\n";
$replymessage .= "http://www.domain.nl\r\n\r\n";
$replymessage .= "This e-mail is automated, so please DO NOT reply.\r\n";

/* Mails the two emails and redirects the user to a redirect page */
$mailit = mail($receiver,$subject,$body,$sender);
mail($cellreciever,$cellsubject,$cellbody,$cellsen der);
if ($flag == 2) { mail($reply,$carbonsubject,$carbonbody,$replyfrom) ; }
else { mail($reply,$replysubject,$replymessage,$replyfrom ); }
header("Location: ../success.php");
exit;
}
}
?>

edbr
11-05-2010, 01:14 AM
try moving the session start as the first thing on the page

davidj
11-05-2010, 06:40 AM
you did set the values of the form fields to those of the $_SESSION?

gentleone
11-05-2010, 08:45 AM
try moving the session start as the first thing on the pageI did try it and no luck either and I moved those $_SESSION lines pretty much everywhere in the file.

you did set the values of the form fields to those of the $_SESSION?Yes the values are the same as the form fields.

value="<?php if(isset($_POST['full-name'])) echo htmlentities($_POST['full-name']);?>"

davidj
11-06-2010, 07:49 AM
It should be....

Value="<?php echo $_SESSION['ses name'] ?>"

Don't forget to use the session_start() at the top of the page

gentleone
11-06-2010, 10:21 AM
If I change the value doesn't this break the original form validation process? Can I not just add $_SESSION['full-name'] to the value how I have it now. Something like this:

value="<?php if(isset($_POST['full-name'])) echo htmlentities($_POST['full-name']($_SESSION['full-name']));?>"

davidj
11-06-2010, 03:16 PM
you dont have to validate it

if its not set it wont display anyway

gentleone
11-06-2010, 03:35 PM
Okay, let me try to understand the form first then :)

I thought that this part in the form field value if(isset($_POST['full-name'])) checks if the required field is filled in.

In the form processor file I have this

if(isset($_POST['sent'])) {
$error = Array();
if(!trim($_POST['full-name'])) $error[] = "Full name";
which I though that if the required field is not filled in it will print the error message in the error div.

Please correct me if I'm wrong with the above.

So if I use the value you suggest I thought I lose this checking for required fields and printing the errors.

davidj
11-06-2010, 06:34 PM
you got time for a TV

gentleone
11-06-2010, 08:39 PM
you got time for a TV
I know, but TV sucks

davidj
11-07-2010, 08:12 PM
Err...

I mean Teamviewer (http://www.teamviewer.com/index.aspx)

Where i remote onto your machine and we solve the problem

gentleone
11-07-2010, 09:21 PM
Err...

I mean Teamviewer (http://www.teamviewer.com/index.aspx)

Where i remote onto your machine and we solve the problem
Oooohkay... hahaha... I've heard about Teamviewer, but never used it before... Anyways, it would be great if we can solve it that way... I'll have to download and install it first. I'm on a Mac if that's on forehand something you should know in order to remote on my machine. I'll let you know when I'm all set.

davidj
11-08-2010, 05:32 AM
right

no problem

gentleone
11-08-2010, 10:38 AM
I'm all set with TV, David. I've send you an email through your site, so if you want to plan a connection one of these days just let me know. Thanks!

davidj
11-08-2010, 10:52 AM
You are 2 hours ahead of me

What about tonight at 6pm my time (8 your time)