I have got a contact form with php validation and the code or something is not working and I don't understand why. I have researched for a suitable answer, but I have had no luck as when the user uses the contact form, then they receive "This page isn't working" error.
My HTML code is the following:
<div class="col-md-8 contact-grid">
<form action="form-process.php" method="POST">
<input name="name" type="text" value="Name" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='name';}">
<br>
<input name="email" type="text" value="Email" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='email';}">
<br>
<select id="contact" name="dropdown" onchange="handleOption(this)">
<option value="crew/gang">Crew/Gang</option>
<option value="nc maphia">NC Maphia</option>
<option value="deadeye/index">Dead Eye</option>
</select><br>
<select id="contact" name="dropdown2">
<option value="game">Game</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">
<option value="reason for contacting us">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" value="Subject" onfocus="this.value='';" onblur="if (this.value == '') {this.value ='subject';}">
<br>
<textarea name="message" cols="77" rows="6" value=" " onfocus="this.value='';" onblur="if (this.value == '') {this.value = 'message';}">Message</textarea>
<div class="g-recaptcha" data-sitekey="Site-key-here"></div>
<div class="send">
<input type="submit" value="Send" >
</div>
</form>
</div>
My PHP code is the following:
<?php
// error_reporting(E_WARNING);
function readURL($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$secret = "site-key-here";
$verificationResponse = $_POST["g-recaptcha-response"];
if( empty($verificationResponse) ) die("Google did not POST the required g-recaptha-response");
$response = readURL("https://www.google.com/recaptcha/api/siteverify" . $secret . "&response=" . $verificationResponse . "");
$responseArray = json_decode($response, true);
if( $responseArray["success"] !== true) die("Invalid reCaptcha <a href=\"javascript:history.go(-1);\">Try Again</a>");
/* Set e-mail recipient */
$myemail = "no_reply@ncmaphia.co.uk";
/* Check all form inputs using check_input function */
$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$dropdown2 = $_POST['dropdown2'];
$dropdown3 = $_POST['dropdown3'];
$subject = $_POST['subject'];
$message = $_POST['message'];
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Contact Message from www.ncmaphia.co.uk";
$message = "
$name has sent you a message using your contact form:
Name: $name
Email: $email
Crew/Gang: $dropdown
Game: $dropdown2
Reason For Contacting Us: $dropdown3
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location:thank-you.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
When the user fills out the contact form and does the re-captcha and clicks on send then they get the "This page isn't working" error. I have supplied a screenshot.
Any help would be appreciated and thank you in advance.
P.S I am new to form validation