簡體   English   中英

從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>

contact-form-handler.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但從不使用它。

一定要檢查動作中的文件名!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM