MP145 Posted January 27, 2009 Share Posted January 27, 2009 Hi guys, I'm not really sure how to title this but here goes... My PHP code: <?php function validate_email($email){ $exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$"; if(eregi($exp,$email)){ if(checkdnsrr(array_pop(explode("@",$email)),"MX")){ return true; }else{ return false; } }else{ return false; } } if (isset($_POST['Submit'])) { if (empty($_POST['name'])) { $errors[] = 'Please enter your name.'; } else if (!ereg("^[a-zA-Z ]+$",$_POST['name'])) { $errors[] = 'Please enter your name with alphabets only.'; } if (empty($_POST['email'])) { $errors[] = 'Please enter your e-mail address'; } else if (!validate_email($_POST['email'])) { $errors[] = 'Please enter a valid e-mail address'; } if (empty($_POST['country'])) { $errors[] = 'Please enter your country name.'; } if (empty($_POST['telephone'])) { $errors[] = 'Please enter your telephone no. Please enter 0 if you do not wish to provide a telephone no.'; } else if (!is_numeric($_POST['telephone'])) { $errors[] = 'Please enter a valid telephone with a numeric value without any dash or brackets'; } if (empty($_POST['message'])) { $errors[] = 'Please enter your message'; } else if (strlen ($_POST['message']) > 255) { $errors[] = 'Your message is too long, please do not submit more than 255 characters'; } if (empty($_POST['spam'])) { $errors[] = 'Please answer the basic math question'; } else if ($_POST['spam'] != 9) { $errors[] = 'Please enter the correct answer'; } if (count($errors) == 0) { function check_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } $RepName = check_input($_POST["name"]); $RepMail = trim($_POST["email"]); $RepCom = check_input($_POST["message"]); require("./mail/class.phpmailer.php"); $mail = new PHPMailer(); //$mail->IsSMTP(); $mail->Host = "xxx"; //$mail->SMTPAuth = true; //$mail->Username = ""; //$mail->Password = ""; $mail->From = "$RepMail"; $mail->FromName = "$RepName"; $mail->AddAddress("xxx", "xxx"); $mail->AddReplyTo("$RepMail", "$RepName"); $mail->IsHTML(true); $mail->Subject = "xxx- $RepSubj"; $mail->Body = "xxx"; $mail->WordWrap = 50; if(!$mail->Send()) { echo "<div class=\"errorbox\">"; echo "Message could not be sent.<br />"; echo "Mailer Error: " . $mail->ErrorInfo; echo "</div>"; exit; } echo "<div class=\"noerrorbox\">"; echo "Thank You ".$RepName." . Your Message has been sent.<br />We will contact you ASAP."; echo "</div>"; } else { echo "<div class=\"errorbox\">"; echo $errors[0]; echo "</div>"; } } ?> // Part of the contact from <form action="" method="post" class="feedbackform"> <fieldset> <legend>CONTACT INFORMATION</legend> <div class="fieldwrapper"> <label for="name" class="styled">Your Name:</label> <div class="thefield"> <input type="text" id="user" name="name" value="<?php echo @$_POST['name']; ?>" size="30" /> </div> </div> Note: There are html codes above the php tags. I have a contact form with validation and use phpMailer to send the email. The form and the error messages kinda works but i would like to know, how do i display the input text value in the text box from the $POST value like name, email and etc before the actual form is emailed. At the moment, the form checks for any error in the name field and if there is no error it proceeds to the email field and the rest of error process goes on. I need to know the method to display the name field in the input text from the $POST value if there is no error. I tried <input type="text" id="user" name="name" value="<?php echo @$_POST['name']; ?>" size="30" /> but im kinda lost on how to erase the value once the email is sent. This method displays all the value even after the email is sent. Should session be used or is there a better way? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/142568-solved-displaying-input-text-variable-before-email-is-sent/ Share on other sites More sharing options...
haku Posted January 27, 2009 Share Posted January 27, 2009 When sending the mail, use this: $mail_sent = mail($to, $subject, $message, $headers); If the mail is sent, $mail_sent becomes TRUE, and if the mail wasn't sent, it is FALSE. Then, in your html output, you can use this: <input type="text" id="user" name="name"<?php if(!$mail_sent) {echo ' value="' . $_POST['name'] . '"';} ?> size="30" /> The value of $_POST['name'] will only be set if the mail hasn't been sent. If it has, then the field will be left blank. Side note: don't use @ marks in your code. It's sloppy programming. It hides errors, instead of fixing them. Quote Link to comment https://forums.phpfreaks.com/topic/142568-solved-displaying-input-text-variable-before-email-is-sent/#findComment-747151 Share on other sites More sharing options...
cooldude832 Posted January 27, 2009 Share Posted January 27, 2009 No $_POST doesn't care about the email adn you shouldn't be working on it (i.e empty($_POST)) what you need to do is do all your processing before displaying the form (Which you do) If the email is successful you do nothing if it is not you run <?php $inputs = array(); foreach($_POST as $key=>$value){ $inputs[$key] = $value; } ?> And then instead of saying <input type="text" id="user" name="name" value="<?php echo @$_POST['name']; ?>" size="30" /> say <input type="text" id="user" name="name" value="<?php echo $inputs['name']; ?>" size="30" /> Also don't use that @ sign its an error suppressor you are learning php you want to see errors, errors are helpful. Quote Link to comment https://forums.phpfreaks.com/topic/142568-solved-displaying-input-text-variable-before-email-is-sent/#findComment-747152 Share on other sites More sharing options...
MP145 Posted January 27, 2009 Author Share Posted January 27, 2009 Thank you very much haku & cooldude832, I kinda used both your advice. this is what i did: <?php if(!$mail->Send()) { echo "<div class=\"errorbox\">"; echo "Message could not be sent.<br />"; echo "Mailer Error: " . $mail->ErrorInfo; echo "</div>"; exit; } echo "<div class=\"noerrorbox\">"; echo "Thank You ".$RepName." . Your Message has been sent.<br />We will contact you ASAP."; echo "</div>"; } else { echo "<div class=\"errorbox\">"; echo $errors[0]; echo "</div>"; $inputs = array(); // cooldude832 method foreach($_POST as $key=>$value){ $inputs[$key] = $value; } } } ?> and at the form code: <input type="text" id="user" name="name" value="<?php if (isset($_POST['Submit'])) if (count($errors) != 0) {echo $inputs['name'];} // haku & cooldude832 method ?>" size="30" /> After testing it locally, it works fine for the first 2 error, because the 3rd error checks for checkdnsrr() , i need to upload it to a server. Will post the results soon, because i don't know what the outcome would be. Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/142568-solved-displaying-input-text-variable-before-email-is-sent/#findComment-747165 Share on other sites More sharing options...
cooldude832 Posted January 27, 2009 Share Posted January 27, 2009 The check for isset $_POST is trivial because you errors should be = 0 if you have not set any post data. Quote Link to comment https://forums.phpfreaks.com/topic/142568-solved-displaying-input-text-variable-before-email-is-sent/#findComment-747169 Share on other sites More sharing options...
MP145 Posted January 27, 2009 Author Share Posted January 27, 2009 ;D Works like a charm... cooldude832 : The check for isset $_POST is trivial because you errors should be = 0 if you have not set any post data. How should i do it then? Now, i got to crack my head, to make it work on the drop down list, checkbox and radio button Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/142568-solved-displaying-input-text-variable-before-email-is-sent/#findComment-747185 Share on other sites More sharing options...
cooldude832 Posted January 27, 2009 Share Posted January 27, 2009 for a drop down you need to build your options into an array and then run an if statement in a foreach and if it matches if statement set selected=selected For checks its the same as input just instead of setting the value you set checked=checked and radio its selected=selected same concept as check Quote Link to comment https://forums.phpfreaks.com/topic/142568-solved-displaying-input-text-variable-before-email-is-sent/#findComment-747220 Share on other sites More sharing options...
MP145 Posted January 27, 2009 Author Share Posted January 27, 2009 Thanks cooldude832. Everything works like a charm now regards, Quote Link to comment https://forums.phpfreaks.com/topic/142568-solved-displaying-input-text-variable-before-email-is-sent/#findComment-747338 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.