Jump to content

Php Contact Form Thanks Msg Error


groupkits

Recommended Posts

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 = "[email protected],";

$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.

Link to comment
https://forums.phpfreaks.com/topic/269965-php-contact-form-thanks-msg-error/
Share on other sites

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

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 = "[email protected]";
$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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.