slaterino Posted December 23, 2008 Share Posted December 23, 2008 Hi, I have designed a contact form in php but for the life of me can't work out how I can set it so that if the user types in the wrong details and the page refreses it keeps the data that was originally typed in. I have simply setup the contact page so that if the data typed in is not valid it will add a message to the header. When this message is added however it always wipes all the previous data. Here is the code I am using: <?php // start PHP session session_start(); if(isset($_POST['docontact'])) { $to = "[email protected]"; $def_subject = "HELP!"; $min_name_len = 2; $min_message_len = 5; if ( strtoupper($_POST['code']) == $_SESSION['code'] ) { if( isset($_POST['name']) and strlen($_POST['name']) >= $min_name_len and isset($_POST['message']) and strlen($_POST['message']) >= $min_message_len and isset($_POST['email']) and preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $_POST['email']) ) { $subject = (isset($_POST['subject'])) ? $_POST['subject'] : $def_subject; $message = $_POST['message'] ."\n==================================================\n" .$_POST['name'] ." | " .$_POST['email']; $header = "From: " .$_POST['name'] ." <" .$_POST['email'] .">\r\n"; mail($to, $subject, $message, $headers); header("location: ?" .$_SERVER['QUERY_STRING'] ."&sent"); } else { header("location: ?" .$_SERVER['QUERY_STRING'] ."&fillall"); } } else { header("location: ?" .$_SERVER['QUERY_STRING'] ."&wrongcode"); } } ?> And these are the headers that appear if the criteria are not met: <?php if(isset($_GET['sent'])) { echo "<p class=\"success\">Thank you, your message was sent successfully.</p>"; } if(isset($_GET['wrongcode'])) { echo "<p class=\"wrongcode\">You have entered the wrong code. Please try again.</p>"; } if(isset($_GET['fillall'])) { echo "<p class=\"error\">Please fill out all mandatory fields. This error may also occur if your email address is invalid.</p>"; } ?> Does anyone have any suggestions how I can go about resolving this? Many thanks! Russ Link to comment https://forums.phpfreaks.com/topic/138173-retaining-information-in-php-contact-form/ Share on other sites More sharing options...
ILMV Posted December 23, 2008 Share Posted December 23, 2008 Well, just use the GET variables to populate the fields again... <input type='text' name='email' value='<?php echo($_GET['email']); ?>' /> Link to comment https://forums.phpfreaks.com/topic/138173-retaining-information-in-php-contact-form/#findComment-722258 Share on other sites More sharing options...
redarrow Posted December 23, 2008 Share Posted December 23, 2008 You need to use sessions in the form. once the form has been sent correctly destroy the sessions. sorry didn't see you was using get. side note get can cause security issues, maybe consider sessions. Link to comment https://forums.phpfreaks.com/topic/138173-retaining-information-in-php-contact-form/#findComment-722259 Share on other sites More sharing options...
slaterino Posted December 23, 2008 Author Share Posted December 23, 2008 Sweet. Jobs a good one. Think I've got fairly secure too! I have one more query regarding this though which I was wondering if you had an answer to. The way my current form works is if someone types in an incorrect e-mail they get an error message above the form and the url is something like: "form.php&fillall". If they then got the code wrong they would then have 2 error messages displayed and url would be: "form.php&fillall&wrongcode". This goes on ad infinitum. Is there any way to delete the last addition to the header so that the user does not end up with a mass of messages above the form and a huge url? Thanks Russ Link to comment https://forums.phpfreaks.com/topic/138173-retaining-information-in-php-contact-form/#findComment-722470 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.