[英]Updating data in mysql database with bootstrap modal and AJAX in PHP
我正在嘗試使用引導模式形式在沒有頁面刷新的情況下更新mysql數據庫中的用戶詳細信息,但是當我按下編輯按鈕時,模式沒有打開並且什么也沒有發生。
這是我的編輯按鈕。 我的編碼或語法有問題嗎?
<button type="button" class="glyphicon glyphicon-edit btn btn-sm btn-default edit_user " id="'.$row["uID"].'" name="edit""></button>
這是我的添加用戶模式。
<!-- Add user modal -->
<div id="add_user_Modal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add New User/Admin</h4>
</div>
<div class="modal-body">
<form method="post" id="add_user_form">
<label>Enter Employee ID</label>
<input type="text" name="user_id" id="user_id" class="form-control" required/>
<br />
<label>Enter Employee Name</label>
<input type="text" name="username" id="username" class="form-control" required/>
<br />
<label>Enter Employee Email</label>
<input type="email" name="email" id="email" class="form-control" required/>
<br />
<label>Password</label>
<input type="password" name="password" id="password" class="form-control" required/>
<br />
<label>Confirm Password</label>
<input type="password" name="confirm_password" id="confirm_password" class="form-control" required/>
<br />
<label>Account Type</label>
<select name="account" id="account" class="form-control">
<option value="Guest">Guest</option>
<option value="Admin">Admin</option>
</select>
<br />
<input type="hidden" name="uid" id="uid" />
<input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
這是ajax的Java腳本,AJAX錯誤為“請求的JSON解析失敗”。
$(document).ready(function(){
//add
$('#add_user_form').on("submit", function(event){
event.preventDefault();
$.ajax({
url:"add_user.php",
method:"POST",
data:$('#add_user_form').serialize(),
beforeSend:function(){
$('#insert').val("Inserting");
},
success:function(data){
alert("succees")
$('#add_user_form')[0].reset();
$('#add_user_Modal').modal('hide');
$('#datatable-buttons').html(data);
},
});
});
$(document).on('click', '.edit_user', function(){
var uid = $(this).attr("id");
$.ajax({
url:"fetch_user_details.php",
method:"POST",
data: {uid:uid},
dataType:"json",
success:function(data){
console.log(data),
$('#username').val(data.username);
$('#email').val(data.email);
$('#password').val(data.password);
$('#account').val(data.account);
$('#user_id').val(data.user_id);
$('#insert').val("Update");
$('#add_user_Modal').modal('show');
},
error: function(jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
$('#post').html(msg);
}
});
});
我認為您引用的ID錯誤。 這可能是重復的13183630
你的按鈕應該是這樣的
<button type="button" class="glyphicon glyphicon-edit btn btn-sm btn-default
edit_user " id="'.$row["uID"].'" name="edit" data-toggle="modal" data- target="#myModal">Open Modal</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.