PHP 500 联系表错误?

PHP 500 Error Contact Form?

我根据答案更新了下面的代码

完全公开我不知道PHP。我需要一个联系表,并且我对下面的代码有足够的了解,可以放心使用它。但是当我尝试提交联系表时出现错误: POST http://(myurlhere)/contact_form.php 500(内部服务器错误)

我很感激任何见解。下面是代码:

HTML

        <form id="form">
          <p id="returnmessage"></p>
          <input type="text" id="name" placeholder="Name"/>
          <input type="text" id="email" placeholder="Email"/>
          <textarea id="message" placeholder="Your Message Here"></textarea>
       </form>
       <a id="submit">Send</a>

PHP

<?php
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// if (!preg_match("/^[0-9]{10}$/", $contact)) {
// echo "<span>* Please Fill Valid Contact No. *</span>";
} else {
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>I will be in touch soon.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" .    $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail("myemailaddresshere@gmail.com", $subject, $sendmessage, $headers);
echo "I will be in touch soon.";
}
} else {
echo "<span>* invalid email *</span>";
}
?>

新代码 - 它发送消息但没有像电子邮件中应该包含的姓名、电子邮件或消息。

新建HTML

<form id="form" method="post" action="">
 <p id="returnmessage"></p>
 <input type="text" id="name" name="name" placeholder="Name"/>
 <input type="text" id="email" name="email" placeholder="Email"/>
 <textarea id="message" name="message" placeholder="Message"></textarea>
 <button id="submit" id="submit">Send</button>
</form> 

新建PHP

<?php
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// if (!preg_match("/^[0-9]{10}$/", $contact)) {
// echo "<span>* Please Fill Valid Contact No. *</span>";
} else {
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>I will be in touch soon.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" .    $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail("myemailhere@gmail.com", $subject, $sendmessage, $headers);
echo "I will be in touch soon.";
}
?>

您提交的代码似乎有问题。我注意到的某些部分有问题

表单方法和提交请求到php没有设置,必须这样

<form id="form" method="post" action="php-url">

提交 link 是在结束 </form> 之后添加的,所以这个必须在表单结束之前

 <button type"submit" id="submit">Send</a>

在你的 php 文件中 double } } 在 else 语句之前添加?

}
} else {
echo "<span>* invalid email *</span>";

最后的 html 会像

 <form id="form" method="post" action="submit.php">
      <p id="returnmessage"></p>
      <input type="text" id="name" name="name" placeholder="Name"/>
      <input type="email" id="email" name="email" placeholder="Email"/>
      <textarea id="message" name="message" placeholder="Your Message Here"></textarea>
  <button type="submit" id="submit">Send</a>
 </form>

PHP

<?php
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$recipient = "myemailaddresshere@gmail.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>I will be in touch soon.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" .    $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail($recipient, $subject, $sendmessage, $headers);
echo "I will be in touch soon.";
?>

检查证明

我可以看到一些问题:

1. 更改您的表单以添加 name 具有 PHP 期望的名称的属性。

2. 在您的表单中包含一个提交按钮,并确保它确实在表单内部(您的 <a>Send</a> 在您的表单外部)

3. 更改您的表单以使用 POST 而不是 GET 方法,因为您的 PHP 脚本正在寻找 $_POST

4. 由于发送邮件需要您的所有输入,因此请向它们添加 required 属性

下面的表格进行了全部 4 次调整

<form id="form" method="post">
  <p id="returnmessage"></p>
  <input type="text" id="name" name="name" placeholder="Name" required/>
  <input type="text" id="email" name="email" placeholder="Email" required/>
  <textarea id="message" name="message" placeholder="" required></textarea>
  <button type="submit" name="submit">Send</button>
</form>

如果处理提交的脚本与表单不是同一个文件,您还必须向 <form> 添加 action 属性。

5. 在 PHP 端,在尝试使用之前检查输入是否可用。现在,如果未提交任何内容,您的代码将处理不存在的值

<?php
if(isset($_POST['email'],$_POST['name'],$_POST['message'])){
    // proceed with your regular script here

}else{
    // no form values were submitted, so nothing to email
}

6. 找出服务器错误日志的位置并查看它。除了我的步骤之外,可能还需要进行更多更改。可以在错误日志中找到 500 error 的详细信息。

请尝试以下代码。

HTML代码

<form id="form" method="post" action="">
   <p id="returnmessage"></p>
   <input type="text" name="name" placeholder="Name"/>
   <input type="text" name="email" placeholder="Email"/>
   <textarea name="message" placeholder="Your Message Here"></textarea>
   <input type="submit" value="Submit"/>
</form>

PHP代码

if(!empty($_POST))
{
  // Fetching Values from URL.
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];

  $email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
  // After sanitization Validation is performed
  if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
   //check email validation and show error
  } else {
   //Email sending functionality
  }
}

你能从你的服务器发送任何网络邮件吗? 这是一个 php 脚本,我称之为万无一失,它非常简单,如果发送电子邮件失败,那么您可能有某种网络邮件限制。这适用于我的共享服务器。我还建议您检查您的服务器日志。

<?php
function sanitize_my_email($field) {
$field = filter_var($field, FILTER_SANITIZE_EMAIL);
if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}
$to_email = 'your-email@your-domain.com';
$subject = 'PHP Foolproof Send Mail script';
$message = 'This mail is sent using the foolproof PHP mail script';
$headers = 'From: gallienus@roman-empire.com';
//check if the email address is invalid $secure_check
$secure_check = sanitize_my_email($to_email);
if ($secure_check == false) {
echo "Invalid input";
}
else { //send email
mail($to_email, $subject, $message, $headers);
echo "echo: This email is sent using simple foolproof PHP mail";
}
?>