如何设置表单验证

时间:2018-12-06 02:08:11

标签: javascript php html

我有一个使用php验证的联系表,并且代码或某些内容不起作用,我不明白为什么。我已经研究了一个合适的答案,但是当用户使用联系表格时,我没有走运,然后他们收到“此页面无法正常工作”错误。

我的HTML代码如下:

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

我的PHP代码如下:

<?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();
}

?>

当用户填写联系表格并进行验证码验证并单击“发送”时,他们将收到“此页面无效”错误。我提供了屏幕截图。

Error Screenshot

任何帮助将不胜感激,谢谢您。

P.S我是表单验证的新手

0 个答案:

没有答案