如果按钮处于已单击的模式中,如何执行操作?我在 Ajax 和 PHP 中使用 jQuery。
Javascript
$(document).on('click', '#edit', function(){
var id = $(this).attr("data-id");
var userName = $(this).attr("data-username");
var userType = $(this).attr("data-user_type");
var action = 'edit';
$.ajax({
url:"/auth/action",
method:"POST",
data:{id:id, action:action},
success:function(data)
{
$('#editModal').modal('show');
$('#userName').val(userName);
$('#userType').val(userType);
$('#id').val(id);
}
})
});
PHP
if($_POST["action"] == 'edit')
{
$statement = $connect->prepare(
"UPDATE users SET username = :username, user_type = :user_type WHERE id = :id"
);
$result = $statement->execute(
array(
':userName' => $_POST["username"],
':userType' => $_POST["user_type"],
':id' => $_POST["id"]
)
);
if(!empty($result))
{
echo 'Data Updated';
}
}
模态 HTML
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Edit User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="post" action="">
<div class="form-group">
<label for="userName" class="col-form-label">Username:</label>
<input type="text" class="form-control border-danger" id="userName" readonly style="background-color: #2A3038">
</div>
请求是编辑用户的信息,我遇到的问题是模式中的提交按钮没有引起操作,我还在开发人员工具的网络选项卡中检查了它。
临摹微笑
catspeake