我正在尝试添加一个简单的联系表单和recaptcha到我的网站,并且还不是很成功。每当我尝试测试表单时,我都会收到错误500。该表单指向位于我的服务器上的PHP文件。
我是php的新手,我正在努力找出问题所在。这是表单的html:
<form method="post" action="form.php" id="contact-form" class="col s12">
<div class="row">
<div class="input-field col m6 s12">
<input id="first_name" type="text" class="validate">
<label for="first_name">First Name</label>
</div>
<div class="input-field col m6 s12">
<input id="last_name" type="text" class="validate">
<label for="last_name">Last Name</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="email" type="email" class="validate"required>
<label for="email">Email</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<textarea id="msg" class="materialize-textarea"></textarea>
<label for="msg">Message</label>
</div>
</div>
<div class="row">
<div class="col s12 m6">
<div class="g-recaptcha" data-sitekey="xxxx-Xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx" data-type="image"></div>
</div>
<div class="col s12 m6">
<button class="btn btn-large waves-effect waves-light" type="submit" name="action">Send Message</button>
</div>
</div>
</form>
这里是form.php
:
<?php
$secret='xxx-Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$sitekey='xxx-Xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx';
$first_name=$last_name=$email=$msg="";
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
$msg = test_input($_POST["msg"]);
if (isset($_POST['first_name'])) {
$first_name=$_POST['first_name'];
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (isset($_POST['last_name'])) {
$last_name=$_POST['last_name'];
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=".$captcha);
if ($response.success==false) {
echo '<p>Captcha failed.<p>'
exit;
}
$toEmail='contact@example.org';
$message = '<p><b>First Name: </b>'.$first_name.'</p>'.
'<p><b>Last Name: </b>'.$last_name.'</p>'.
'<p><b>Email: </b>'.$email.'</p>'.
'<p><b>Message: </b><br/>'.$msg.'</p>';
$headers = 'From: first last <contact@example.org> '."rn".
'Reply-To: '.$first_name.$last_name.' <'.$email.'> '. "rn".
'Content-type: text/html'. "rn".
'X-Mailer: PHP/'.phpversion();
$message2 = '<p><b>Here is a copy of your message: </b><br/><br/>'.$first_name.' '.$last_name.'wrote: <br/><br/></p>'.'<p>'.$msg'</p>'
$headers2 = 'From:'.$toEmail."rn".'Reply-To: '.$toEmail."rn".
'Content-type: text/html'. "rn".
'X-Mailer: PHP/'.phpversion();
mail($toEmail,'New Inquery from Website',$message,$headers);
mail($email,'Copy of your form submission',$message2,$headers2);
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
我的php / html有问题吗?或者是别的什么?我的网络服务器在Ubuntu 14.04上使用php5-fpm和nginx。谢谢你的帮助!
答案 0 :(得分:0)
行
echo '<p>Captcha failed.<p>'
关闭;
错过了
行
$message2 = '<p><b>Here is a copy of your message: </b><br/><br/>'.$first_name.' '.$last_name.'wrote: <br/><br/></p>'.'<p>'.$msg'</p>'
应该是
$message2 = '<p><b>Here is a copy of your message: </b><br/><br/>'.$first_name.' '.$last_name.'wrote: <br/><br/></p><p>'.$msg.'</p>';
另外,把
error_reporting(E_ALL);
ini_set('display_errors', 1);
在PHP文件的开头(afrer <?php
)查看&amp;调试错误。
答案 1 :(得分:0)
将您的HTML提交按钮更改为
<button class="btn btn-large waves-effect waves-light" type="submit" name="action" value="Send Message">Send Message</button>
修改你的php脚本。将您的代码放在
中if statement
e.g
<?php
if(isset($_POST['action']))
{
$secret='xxx-Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$sitekey='xxx-Xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx';
$first_name=$last_name=$email=$msg="";
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
$msg = test_input($_POST["msg"]);
if (isset($_POST['first_name'])) {
$first_name=$_POST['first_name'];
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (isset($_POST['last_name'])) {
$last_name=$_POST['last_name'];
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=".$captcha);
if ($response.success==false) {
echo '<p>Captcha failed.<p>';
exit;
}
$toEmail='contact@example.org';
$message = '<p><b>First Name: </b>'.$first_name.'</p>'.
'<p><b>Last Name: </b>'.$last_name.'</p>'.
'<p><b>Email: </b>'.$email.'</p>'.
'<p><b>Message: </b><br/>'.$msg.'</p>';
$headers = 'From: first last <contact@example.org> '."rn".
'Reply-To: '.$first_name.$last_name.' <'.$email.'> '. "rn".
'Content-type: text/html'. "rn".
'X-Mailer: PHP/'.phpversion();
$message2 = '<p><b>Here is a copy of your message: </b><br/><br/>'.$first_name.' '.$last_name.'wrote: <br/><br/></p>'.'<p>'.$msg'</p>'
$headers2 = 'From:'.$toEmail."rn".'Reply-To: '.$toEmail."rn".
'Content-type: text/html'. "rn".
'X-Mailer: PHP/'.phpversion();
mail($toEmail,'New Inquery from Website',$message,$headers);
mail($email,'Copy of your form submission',$message2,$headers2);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>