我有以下表单,我正在使用 javascript 来验证表单字段onsubmit。我将如何在此表单中实施reCaptcha?我是否必须更改onsubmit值才能执行带有 recaptcha 验证和字段验证的 php 文件?这是当前的表格:
<form id="breeders" method="post" action="add-breeder?process" onsubmit="return validateForm()">
Name:<br />
<input type="text" id="name" name="name" /><br /><br />
County:<br />
<select id="address" name="address">
<option value="Antrim">Antrim</option>
<option value="Armagh">Armagh</option>
</select><br/>
<br />Please select a point on the map nearest to you.<br />
<input type="hidden" id="lat" name="lat" /><br />
<input type="hidden" id="lng" name="lng" />
Phone No:<br />
<input type="text" id="phone" name="phone" /><br /><br />
Description:<br />
<textarea cols="40" rows="5" name="breeds" id="breeds" /></textarea><br /><br />
<input type="submit" value="Add Breeder" class="btn btn-primary btn-large" />
</form>
</div>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["breeders"]["name"].value;
if (x==null || x=="")
{
alert("Name is required.");
return false;
}
var x=document.forms["breeders"]["address"].value;
if (x==null || x=="")
{
alert("Location is required.");
return false;
}
var x=document.forms["breeders"]["phone"].value;
if (x==null || x=="")
{
alert("Contact phone number is required.");
return false;
}
var x=document.forms["breeders"]["breeds"].value;
if (x==null || x=="")
{
alert("A description of Breeds is required.");
return false;
}
var x=document.forms["breeders"]["lat"].value;
if (x==null || x=="")
{
alert("You have to select a location on the map.");
return false;
}
var x=document.forms["breeders"]["lng"].value;
if (x==null || x=="")
{
alert("You have to select a location on the map.");
return false;
}
}
</script>
任何帮助表示赞赏,干杯!
编辑:
我已经在表单上实现了 reCaptcha,但是当我单击提交时,该操作似乎没有执行。文件“submit.php”在同一个根目录下。这是带有 reCaptcha 的表单代码:
<form id="breeders" method="post" action="submit.php" onsubmit="return validateForm()">
Name:<br />
<input type="text" id="name" name="name" /><br /><br />
County:<br />
<select id="address" name="address">
<option value="Antrim">Antrim</option>
<option value="Armagh">Armagh</option>
</select><br/>
<br />Please select a point on the map nearest to you.<br />
<input type="hidden" id="lat" name="lat" /><br />
<input type="hidden" id="lng" name="lng" />
Phone No:<br />
<input type="text" id="phone" name="phone" /><br /><br />
Description:<br />
<textarea cols="40" rows="5" name="breeds" id="breeds" /></textarea><br /><br /><br /><br /><br />
<script type="text/javascript"
src="https://www.google.com/recaptcha/api/challenge?k=MY_PUBLIC_KEY">
</script>
<noscript>
<iframe src="https://www.google.com/recaptcha/api/noscript?k=MY_PUBLIC_KEY"
height="300" width="500" frameborder="0"></iframe><br>
</noscript>
<br/><input type="submit" value="Add Breeder" class="btn btn-primary btn-large" />
</form>
</div>
这是“submit.php”文件的内容:
<?php if (!defined('APPLICATION')) exit(); ?>
<?php
require_once('recaptchalib.php');
$privatekey = "MY_PRIVATE_KEY";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>
<?php
//connection to my database details etc.
?>
<?php
if(isset($_GET['process']))
{
$query = "Insert INTO `markers` (name, address, lat, lng, phone, breeds) values('$_POST[name]', '$_POST[address]','$_POST[lat]','$_POST[lng]', '$_POST[phone]', '$_POST[breeds]')";
//echo $query; exit;
$result = mysql_query($query) or die(mysql_error());
if(!$result)
{
$msg = "not Inserted";
}
else
{
$msg = "Inserted";
header("location:breeders?m=".$msg);
}
}
?>
“recaptchalib.php 也在同一个文件夹中。您可以在此处查看表格:http: //poultry.ie/add-breeder