weixin_33725807 2015-11-23 06:42 采纳率: 0%
浏览 133

AJAX被调用两次

I am trying to add values to database using ajax & php. i have a modal popup which asks user for input, and in modal popup there are text fields and submit button. when user fills the fields and hit submit button, the ajax is called which adds values to database.

$('.dup-product').click(function(){    
    var owners = $(".total_product_owner").val();
        $(".getmailfields").empty();
        if(owners == 0){
            $(".getmailfields").empty();
        } else {
            for (i = 1; i <= owners; i++) {        
                $(".getmailfields").append("<div class='form-group email_forms'><label class='control-label col-sm-2' for=invite-"+i+">Member "+i+" email</label><div class='albox col-sm-10'><input type='email' class='form-control' name='invitefriends[]' placeholder='sam@unclejohn.com' required></div></div>");
            }
        }
});

below code is action from modal popup

$('.duplicate_product').click(function(){
    var prodid = $(this).attr('pid');
    var userid = $(this).attr('oid');  
    $.ajax({
        type : "post",
        url : "user.php",
        data: { 
                product_id: prodid,
                user_id: userid,
             },
        cache:false,
        success : function(html){
              location.reload();
            }
        });
});

when i hit submit from popup the data is getting added twice to database.how can i resolve it?

php:

<?php   
$pid = $_POST['product_id']; 
$uid = $_POST['user_id'];

if (isset($pid) && !empty($uid)){  
    duplicate($pid,$uid);
} else {   
    echo "Are you trying to do something nasty??";
}

duplicate($pid,$uid){
 echo "Duplicated";
}

?>
  • 写回答

1条回答 默认 最新

  • weixin_33712987 2015-11-23 06:51
    关注

    There could be any of below situations

    1. There are two elements with the class duplicate product. If this is the case use id to bind the click event

      $('#duplicate_product').click(function(){
      var prodid = $(this).attr('pid');
      var userid = $(this).attr('oid');  
      $.ajax({
          type : "post",
          url : "user.php",
          data: { 
                  product_id: prodid,
                  user_id: userid,
               },
          cache:false,
          success : function(html){
                location.reload();
              }
          });
      

      });

    Then put id="duplicate_product" to the button 2. The event is bound to the button again and again. in such case use below code

    $('.duplicate_product').unbind('click').bind('click', function(){
    var prodid = $(this).attr('pid');
    var userid = $(this).attr('oid');  
    $.ajax({
        type : "post",
        url : "user.php",
        data: { 
                product_id: prodid,
                user_id: userid,
             },
        cache:false,
        success : function(html){
              location.reload();
            }
        });
    

    });

    评论

报告相同问题?

悬赏问题

  • ¥15 三菱变频器模块怎么检测触发
  • ¥15 在C#方法中使用了.Refresh();刷新了控件,但是不是最新值
  • ¥30 求一下解题思路,完全不懂。
  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗