从 HTML 调用 PHP 表单


Call PHP form from HTML

contact-form.html

<form method="POST" name="contactform" action="contact-form-handler.php"> 
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>

联系人表单处理程序.php

<?php 
$errors = '';
$myemail = 'net.dev@spikyarc.net';
if(empty($_POST['name'])  || 
empty($_POST['email']) || 
empty($_POST['message']))
{
   $errors .= "'n Error: all fields are required";
}
$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 
if (!preg_match(
  "/^[_a-z0-9-]+('.[_a-z0-9-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*('.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "'n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail; 
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".

$headers = "From: $myemail'n"; 
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
  } 
?>

这两个文件我都放在 wwwroot 文件夹中,但是当我提交 html 表单时,它会给出错误The page cannot be displayed。我找不到问题。谢谢你帮助我。

HTML 表单中的操作设置为:

contact-form-handler.php

喜欢:

<form method="POST" name="contactform" action="contact-form-handler.php"> 

更新1:并检查这个:

改变

$email_subject = "Contact form submission: $name";

$email_subject = "Contact form submission:" . $name;

更新2:这也:

$headers = "From: $myemail'n"; 
$headers .= "Reply-To: $email_address";

自:

$headers = "From: " . $myemail . "'n"; 
$headers .= "Reply-To:" . $email_address;

更新 3:

验证电子邮件地址$email_address但从不使用它。

请务必在操作中检查文件名!!