我会尽力解释这个好事。当我通过ajax btw提交数据时,我在action.php中的变量始终为null我试图使用模态编辑我的数据。我只是不知道我哪里出错了我希望你们明白我的观点。 这是我的代码......
// html表单
<div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title" id="exampleModalLongTitle">Edit Fields</h2>
</div>
<div class="modal-body">
<form id="form_data">
<form id="edit_data">
<input type="text" name="id" id="id2">
<input type="text" name="username" id="username2" class="form-control"><br>
<input type="text" name="subject" id="subject2" class="form-control"><br>
<input type="text" name="plan" id="plan2" class="form-control"><br>
<input type="submit" name="go_edit" value="Edit" id="edit2" class="btn btn-warning">
</form>
</form>
</div>
</div>
</div>
</div>
//这是ajax脚本
$('#edit2').on('click', function(event){
event.preventDefault();
var edit = $("#edit_data").serialize() + "&go_edit=1";
$.ajax({
url:"action.php",
method:"POST",
data:edit,
success:function(data2){
alert(data2);
$('#exampleModalLong').modal('hide');
$('#list').load(window.loccation = ' #list');
}
});
});
});
// php代码,用于编辑位于action.php
中的表单中的数据if(isset($_POST['go_edit'])){
$id = $_POST['id'];
$username = $_POST['username'];
$subject = $_POST['subject'];
$plan = $_POST['plan'];
var_dump($where = array("id" => $id));
var_dump($myArray = array("username" => $username,
"subject" => $subject,
"plan" => $plan));
if($data->EditData("new_users",$myArray,$where)){
echo "Data Edited Successfully";
}
}
//这是我在edit.php
中编辑的sqlpublic function EditData($table,$fields,$where){
$sql = "";
$condition = "";
foreach ($where as $key => $value) {
$condition .= $key ."='". $value. "' AND ";
}
$condition = substr($condition, 0, -5);
foreach ($fields as $key => $value) {
$sql .= $key . "='".$value."', ";
}
$sql = substr($sql, 0, -2);
$sql = "UPDATE ".$table." SET ".$sql." WHERE ".$condition;
$query = mysqli_query($this->con,$sql);
if($query){
return true;
}
}
答案 0 :(得分:0)
对jQuery代码进行一些修改: 获取所有值:
var id1 = $("#id2").val();
var un1 = $("#username2").val();
var s1 = $("#subject2").val();
var p1= $("#plan2").val();
var submit=$('#edit2').val();
使用数据异步调用PHP代码:
$.post("action.php", {
id: id1,
username: un1,
subject: s1,
plan: p1,
go_edit:submit
},function(data) {
$("#returnmessage").append(data); // Append returned message to message
if (data == "TRUE") {
$("#form")[1].reset(); // To reset form fields on success.
}