我有一个可以处理PHP表单的联系表单,但是我添加了Google recapture。重新捕获未通过验证。提交表单后,用户将收到以下错误:
此页面无法正常运行www.example.co.uk当前无法处理此请求。 HTTP错误500
我的代码如下:
HTML
<div class="contact">
<div class="container">
<h2>Contact US</h2>
<div class="contact-form">
<div class="col-md-8 contact-grid">
<form id="test" action="form-process.php" method="POST">
<input name="name" type="text" placeholder="Name" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='name';}" required>
<br>
<input name="email" type="text" placeholder="Email" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='email';}" required>
<br>
<select id="contact" name="dropdown" onchange="handleOption(this)" required>
<option value="" disabled selected>Crew/Gang</option>
<option value="NC Maphia">NC Maphia</option>
<option value="deadeye/index">Dead Eye</option>
</select><br>
<select id="contact" name="dropdown2" required>
<option value="" disabled selected>Gang</option>
<option value="Grand Theft Auto V">Grand Theft Auto V</option>
<option value="Red Dead Redemption II">Red Dead Redemption II</option>
</select><br>
<select id="contact" name="dropdown3" required>
<option value="" disabled selected>Platform</option>
<option value="Xbox One">Xbox One</option>
<option value="PS4">PS4</option>
</select><br>
<select id="contact" name="dropdown4" required>
<option value="" disabled selected>Reason For Contacting Us</option>
<option value="Joining NC Maphia">Joining NC Maphia</option>
<option value="Recruitment Requirments">Recruitment Requirments</option>
<option value="Events">Events</option>
<option value="Report A Member">Report A Member</option>
<option value="Webmaster">Webmaster</option>
<option value="General Inquiry">General Inquiry</option>
</select><br>
<input name="subject" type="text" placeholder="Subject" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='subject';}">
<br>
<textarea name="message" cols="77" rows="6" placeholder="Message" onfocus="this.value='';" onblur="if (this.value == '') {this.value = 'message';}" required></textarea>
<div class="g-recaptcha" data-sitekey="Site-Key-Here"></div>
<div class="send">
<input type="submit" name="submit" value="Send" >
</div>
</form>
</div>
PHP
<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
if(isset($_POST["submit"])) //If the form is submitted
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = 'My-Key-Here';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success):
{
$notification=""; //Used for catching all your messages
//Retrieve form data.
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$errors = [];
$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$dropdown2 = $_POST['dropdown2'];
$dropdown3 = $_POST['dropdown3'];
$dropdown4 = $_POST['dropdown4'];
$subject = $_POST['subject'];
$message = $_POST['message'];
//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course,
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$dropdown) $errors[count($errors)] = 'Please enter which crew/gang you are attempting to contact.';
if (!$dropdown2) $errors[count($errors)] = 'Please enter which game you are referring too.';
if (!$dropdown3) $errors[count($errors)] = 'Please enter which platform you are currentlyplaying on.';
if (!$dropdown4) $errors[count($errors)] = 'We need too know the reason for your message.';
if (!$message) $errors[count($errors)] = 'We need more detail on why you are contacting us.';
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = 'no_reply@ncmaphia.co.uk';
//sender
$from = $email;
//subject and the html message
$subject = 'Hello from ' . $name;
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<p>You have recieved a message from $name using your contact form at www.ncmaphia.co.uk
<table>
<tr><td>Name: </td><td>' . $name . '</td></tr>
<tr><td>Email: </td><td>' . $email . '</td></tr>
<tr><td>Crew/Gang: </td><td>' . $dropdown . '</td></tr>
<tr><td>Game: </td><td>' . $dropdown2 . '</td></tr>
<tr><td>Platform: </td><td>' . $dropdown3 . '</td></tr>
<tr><td>Reason For Contacting Us: </td><td>' . $dropdown4 . '</td></tr>
<tr><td>Subject: </td><td>' . $subject . '</td></tr>
<tr><td>Message: </td><td>' . $message . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = mail($to, $subject, $message, $from);
//if POST was used, display the message straight away
if ($_POST) {
if ($result) header('location: thank-you.html', true, 303);
else:
$errMsg = 'Robot verification failed, please try again.';
endif;
else:
$errMsg = 'Please click on the reCAPTCHA box.';
endif;
else:
$errMsg = '';
$succMsg = '';
endif;
//else if GET was used, return the boolean value so that
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
$notification.= $result;
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
$notification.= '<a href="contact.html">Back</a>';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
} //First If loop
?>
预先感谢