2017-09-25 163 views
0

當我將值發送到處理頁面時出現問題 值被多次發送 我在jQuery函數中更改爲.on(),但是同一問題 有些運行沒有問題,但更新和刪除次重複jquery 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

+0

僅供參考,您可以用'VAR數據= that.serialize()'來獲取所有表單值。請參閱https://api.jquery.com/serialize/ – Phil

+0

此外,我強烈建議您使用[準備語句](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)以執行你的查詢。僅供參考,您在最後'$ phone'周圍缺少引號。當你使用參數綁定時,你不必擔心這樣的問題 – Phil

+0

你實際上是否看到兩個AJAX請求?您的控制檯輸出根本無法解釋問題。檢查你的*網絡*控制檯 – Phil

回答

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 
     } 
    }); 
}); 
+0

它是請求兩次的同樣的問題。 –