0

我有以下表单,我正在使用 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

4

2 回答 2

0

当您提交数据时,验证码必须在服务器端进行验证,以确保其安全。只要您知道它根本没有强制执行,并且没有什么可以阻止恶意用户向您的服务器端脚本发送他们想要的任何无意义的垃圾,您就可以将您的验证保留在 Javascript 中。在可能的情况下验证服务器端通常是一种很好的做法,但这取决于您正在为谁进行验证。

于 2013-03-23T01:16:43.350 回答
0

哎呀!阅读此http://code.google.com/p/recaptcha/wiki/HowToSetUpRecaptcha
主要在第 2 部分:安装然后您的选择!

走向你的错误

改变 action="submit.php?process=1"形式
了吗 defined('APPLICATION')

更新:

Insert INTO `markers`  to Insert INTO markers 
(in the Above Note: "`" )

尝试 Echoing 以了解执行停止的位置!

于 2013-03-23T01:38:52.427 回答