groupkits Posted October 27, 2012 Share Posted October 27, 2012 hi! I am using following coding as a PHP + HTML contact form:- <html> <head> </head> <body> <?php if(isset($_POST['email'])){ $email = $_POST['email']; $recipient = "myemail@myemail.com,"; $subject = "Contact Form Email Subject"; $mailheader = "From: $email \r\n"; mail($recipient, $subject, $mailheader) or die("Error!"); $msg = "<font color='green' size='3'>Thank You for your message.</font>"; echo $msg; } ?> <form method="POST"> <p align="center"> Enter your valid email id: <br> <input type="text" name="email"> <br> <input type="submit" value="Subscribe now!"> </form> <?php echo $msg; ?> </body> </html> I am getting a problem: The text: [ Thank You for your message."; echo $msg; } ?> ] is showing up at the top of form even before submission. The form is sending the email but it is not showing the text [ Thank You for your message. ] blow the form after sending email. Quote Link to comment https://forums.phpfreaks.com/topic/269965-php-contact-form-thanks-msg-error/ Share on other sites More sharing options...
MDCode Posted October 27, 2012 Share Posted October 27, 2012 (edited) Well seeing as you're echoing the message at the top of the page that would explain why you're getting it at the top although you're checking if it's set which is odd. Trying changing EDIT: Tried this on my site and it works fine other than you're echoing in two places which leads me to believe you are not showing everything Edited October 27, 2012 by ExtremeGaming Quote Link to comment https://forums.phpfreaks.com/topic/269965-php-contact-form-thanks-msg-error/#findComment-1388076 Share on other sites More sharing options...
OOP Posted October 30, 2012 Share Posted October 30, 2012 I tried you code on my PC and it works fine except that it shows the thanks message twice. Once before the form, and the other after the form upon successful form submission. I have modified the code a bit, you may try it. <html> <head> </head> <body> <?php if (isset($_POST['submit'])) { if ( empty($_POST['email']) || !preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$_POST['email'])) { $msg = 'please provide a valid email'; } else { $email = $_POST['email']; $recipient = "myemail@myemail.com"; $subject = "Contact Form Email Subject"; $message = "your email message"; $mailheader = "From: $email \n"; mail($recipient, $subject, $message, $mailheader) or die("Error!"); $msg = "<font color='green' size='3'>Thank You for your message.</font>"; } } ?> <form method="POST"> <p align="center"> Enter your valid email id: <br> <input type="text" name="email"> <br> <input type="submit" name="submit" value="Subscribe now!"> </form> <?php echo $msg; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/269965-php-contact-form-thanks-msg-error/#findComment-1388654 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.