2016-08-03 76 views
-2

我根据回答更新了下面的代码PHP 500错误联系表格?

完全披露我不知道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("[email protected]", $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("[email protected]", $subject, $sendmessage, $headers); 
echo "I will be in touch soon."; 
} 
?> 
+0

有你提到的代码没有动作。 '

' –

回答

1

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

形式的方法,并提交申请到PHP没有设置,它必须是这样

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

提交链接添加结束</form>后,所以这一块必须是在表单结束之前

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

在您的php文件中双}}在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 = "[email protected]"; 
$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."; 
?> 

检查证明

enter image description here

+0

嘿,我试过用这个,但我没有运气。我再也看不到这个错误,现在看起来有一个“提交”按钮,但是这条消息从来没有经过,我也没有看到错误日志中的任何内容。我完全是无知的PHP。 – user5854648

+0

另外是的,有一个双}}之前,其他 - 这是正确的? – user5854648

+0

更新,我不再有错误,但它回应了我的代码中设置的“无效电子邮件”,即使电子邮件有效。 – user5854648

0

我可以看到几个问题:

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

2.包含提交按钮,表单,并确保它实际上是在表单内(您<a>Send</a>是你的表格外)

3。更改您的形式,因为你的PHP脚本在$_POST

4.由于所有的输入都需要发送邮件寻求值使用的POST代替GET法,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> 

您也将有一个action属性添加到<form>如果脚本HANDL提交的文件与表单不同。

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的详细信息。

+0

添加如果是关键谢谢你,我不再有错误。但是现在,即使输入了有效的电子邮件,它也会回应“无效电子邮件”的标签,因此它不会发送邮件。 – user5854648

0

请尝试下面的代码。

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