jquery ajax发送请求两次

时间:2017-09-25 00:06:16

标签: javascript php jquery html ajax

将值发送到处理页面时出现问题 值不止一次发送 我在jQuery函数中更改为.on()但问题相同 有些页面运行没有问题,但重复更新和删除时间

ajax

    /* Start Update User From Moadl Bootstrap   */
$('#EditForm').submit(function(event){
    var that = $(this), 
    url = that.attr('action'), // action to function.php
    type = that.attr('method'), // Method POST default
    data  = {}; 
    event.preventDefault(); // for stop submit action forward 

    that.find('[name]').each(function(index,value){
        var that = $(this);
        var name = that.attr('name'); 
        var value = that.val(); // the vaule of ['name']="xxx"
        data[name] = value; // data
        console.log(data);  /* show the data */ 
    });

    $.ajax({
        url:url,
        type:type,
        data:data,
        success:function(data){
        //  console.log(data);
        //  $('div#EditModal button.btn-success').attr('data-dismiss','modal')
            jQuery.noConflict();
            $('div#EditModal').modal('hide'); //or  $('#IDModal').modal('hide');
            $('body').removeClass('modal-open'); // remove black screen
            $('.modal-backdrop').remove();//remove black screen
            $('#alert-success').fadeIn(1000).fadeOut(5000); //fadeIn alert
        }
    })
});
/*  End Update User     */

php代码

function update($db){
    if(isset($_POST['udid_update']) || isset($_POST['phone_update'])){
$name   =   mysqli_real_escape_string($db,$_POST['name_update']);
$udid   =   mysqli_real_escape_string($db,$_POST['udid_update']);
$phone  =   mysqli_real_escape_string($db,(int)$_POST['phone_update']);
$email  =   mysqli_real_escape_string($db,$_POST['email_update']);
$group  =   mysqli_real_escape_string($db,$_POST['group_update']);

$query = "UPDATE `$group` SET `num_phone` = '$phone', `udid` = '$udid', `name` = '$name', `email` = '$email' WHERE `$group`.`num_phone` = $phone";

$do = mysqli_query($db,$query);
    /*
if($do)
    echo 'Done'.$query;
else 
echo 'not'.$query;
    return true;*/

    }
}
update($db); 

问题图片

request post 2 times

1 个答案:

答案 0 :(得分:0)

试试这个

$('#EditForm').submit(function(e){ 
    e.preventDefault();
    $.ajax({ 
        url:$(this).attr('action'), 
        type:'post', // you can change this to $(this).attr('method')
        data:$(this).serialize(), //This line will serialize all form fields
        success:function(data){ 
            // Here you can do any code to show success message
            $(this)[0].reset(); // This line will reset the form after success
        } 
    });
});