1

如果用户在几秒钟内点击太多链接,我想进行验证(点击垃圾邮件保护)。假设我想限制一个相同类型的 href 按钮来激活一个计数器,以防止允许在 10 秒内再次单击此 href 按钮。

我有一个活动列表,您可以作为用户加入,例如活动 Facebook 上的加入按钮。但是因为这些事件列表非常紧凑,所以有很多加入按钮。这就是为什么我想通过单击这些连接按钮来防止用户向数据库发送垃圾邮件。

我正在使用 jQuery,这是我的代码:

$(function() {
  $(".event_join").click(function() {

    var id = $(this).attr("id");
    var uid = <?php echo $_SESSION['uid']; ?>;
    var dataString = 'event_join=' + id + '&uid=' + uid;

    if (uid == '') {
      alert("not logged in");
    } else {
      $("#event_" + id).html('Joining the guestlist...');

      $.ajax({
        type: "POST",
        url: "ajax_insert.php",
        data: dataString,
        cache: false,
        success: function(html){
          $("#event_" + id).html('You're on the guestlist!');
        }
      });
    } return false;
  });
});

这是在 ajax_insert.php 文件中验证的常用方法吗?如果是,我如何告诉 jQuery 这不是“成功”并打印出错误?

4

2 回答 2

3

使用“标志”。声明 var flag = 0; 最初。然后在代码中,使用它如下:

if(flag==0) {
flag = 1;
$.ajax({
    type: "POST",
    url: "ajax_insert.php",
    data: dataString,
    cache: false,
    success: function(html){
        $("#event_" + id).html('You're on the guestlist!');
        flag=0;
    }
});

这样,可以防止重复。提交后,您可以使用 javascript 禁用表单,以便用户无法发送另一个请求。

编辑1:如果您使用链接,那么您可以禁用链接本身。有很多方法可以做到这一点。喜欢

$('.event_join').removeAttr('href');

第二种方法是更改​​链接地址。

$('.event_join').attr('href','#');

或者

$('.event_join').attr('href','javascript:void(0)');

第三种方法可能是您隐藏链接。

$('.event_join').css({'display':'none'});

第四种方法是完全删除链接。

$('.event_join').html('');

或者如果它的父母只有一个孩子,那就更好了,

$('.event_join').parent().html('');

可以有更多的方法,只是探索网络。

如果您有许多带有 event_join 类的链接,请使用 $('this') 而不是 $('.event_join') 以便并非所有链接都被禁用/隐藏/删除。完整的代码可能是:

if(flag==0) {
flag = 1;
$.ajax({
    type: "POST",
    url: "ajax_insert.php",
    data: dataString,
    context: $(this),
    cache: false,
    success: function(html){
        $("#event_" + id).html('You're on the guestlist!');
        $(this).removeAttr('href');
        flag=0;
    }
});

成功后会删除 href 属性,因为在第一次单击时可能没有完成 ajax 请求。并且使用标志物是为了防止在成功功能运行之前多次点击。

于 2013-05-07T20:56:35.727 回答
2

您可以按照您提供的方式进行操作。不清楚javascript代码,但它可以工作。有两种方法可以检查响应是否成功。

第一个是解析服务器响应。在这种情况下,成功的服务器结果应该返回如下内容:

  echo( json_encode( array( 'result' => true ) ) );
  exit();

对于错误:

  echo( json_encode( array( 'result' => false ) ) );
  exit();

并且在客户端服务器的 json 回复将由 JQuery 自动解析。所以,处理这样的响应:

success: function( response ){
  if( 'undefined' == typeof response.result ||
      false == response.result ){
    console.log( 'Server returned error. Handle it here' );
  } else {
    console.log( 'Server returned success!' );
  }
}

但我更喜欢使用 HTTP 响应代码,因为它就是为此而生的。接下来是与服务器握手的第二种方式。在服务器端成功:

http_response_code( 200 );
echo( 'Good, lets go' );
exit();

对于错误:

http_response_code( 401 );
echo( 'It looks like you are bad boy' );
exit();

在这种情况下,在客户端,您可以通过这种方式捕获服务器响应:

success: function( response ){
  console.log( 'Server returned http200 code. It means request was success and we do not need even to parse result' );
},
error: function( jqXHR, textStatus, errorThrown ){
  console.log( 'Server returned error. This could be not only an error you awaiting for, but any other. All error details are provided in incoming variables' );
}
于 2013-05-07T20:50:47.043 回答