简体   繁体   中英

Html PHP web form not sending email?

I have created a html form with a php script in order to ping an email when user hits 'send it'. However when hit 'send it' I am getting an error 'Please correct the following error:' Can anyone tell me whether I am connecting the forms correctly or whether my php script is wrong. Thanks!

contact.htm

<html>
<body>

<p>Required fields are <b>bold</b></p>

<form action="contact.php" method="post">
<p><b>Your Name:</b> <input type="text" name="yourname" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>

<p>Do you like this website?
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>

<p>How did you find us?
<select name="how">
<option value=""> -- Please select -- </option>
<option>Google</option>
<option>Yahoo</option>
<option>Link from a website</option>
<option>Word of mouth</option>
<option>Other</option>
</select>

<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>

<p><input type="submit" value="Send it!"></p>

<p> </p>

</form>

</body>
</html>

thanks.htm

<html>
<body>

<p><b>Your message was sent</b></p>

<p>Your message was successfully sent!
Thank you for contacting us, we will reply
to your inquiry as soon as possible!</p>

</body>
</html>

contact.php

<?php

$myemail  = "gregtwardochleb@hotmail.co.uk";

$yourname = check_input($_POST['yourname'], "Enter your name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$website  = check_input($_POST['website']);
$likeit   = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}

if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}

$message = "Hello!

Your contact form has been submitted by:

Name: $yourname
E-mail: $email
URL: $website

Like the website? $likeit
How did he/she find it? $how_find

Comments:
$comments

End of message
";

mail($myemail, $subject, $message);

header('Location: thanks.htm');
exit();

function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
    show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<b>Please correct the following error:</b><br />
<?php echo $myError; ?>

</body>
</html>
<?php
exit();
}
?>

Your HTML page is getting redirected to contact page. And in Contact Page, you have written the code indicates

<b>Please correct the following error:</b><br />

Do you have following items in your code?

$website  = check_input($_POST['website']);
$likeit   = check_input($_POST['likeit']);
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");

Please let me know what you exactly want to do.

You are missing with some of inputs as you have included in contact.php like website,how,likeit which you are not posting from your form inputs

<html>
<body>
 <form action="contact.php" method="post">
  <p><b>Your Name:</b> <input type="text" name="yourname" /><br />
  <b>Subject:</b> <input type="text" name="subject" /><br />
  <b>E-mail:</b> <input type="text" name="email" /><br />
  <b>Website:</b> <input type="text" name="website" /><br />
  <b>Like the website:</b> <input type="text" name="likeit" /><br />
  <b>How did he/she find it:</b> <input type="text" name="how" /><br />
  <p><b>Your comments:</b><br />
  <textarea name="comments" rows="10" cols="40"></textarea></p>
  <p><input type="submit" value="Send it!"></p>
 </form>
</body>
</html>

php mail function will not work on local host as it takes domain information to send the mail. So you have to put it on server first. Hope it will work for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM