我有一个可以由用户编辑的表单。我希望使用Ajax在不重新加载页面的情况下更新表单。新值将替换旧值。对于输入字段,我从php SQL查询中读取值。因此,用户将能够第一眼看到细节。问题是这个代码运行时没有错误,也没有结果。我试过console.log。
我从while循环中获取用户值。
if(isset($_GET['edit_user'])){
$the_user_id = $_GET['edit_user'];
$query = "SELECT * FROM users WHERE user_id = $the_user_id ";
$select_users_query = mysqli_query($connection,$query);
while($row = mysqli_fetch_assoc($select_users_query)) {
$user_id = $row['user_id'];
$user_firstname = $row['first_name'];
$user_lastname = $row['last_name'];
$user_contact = $row['mobile'];
$_SESSION["id"] = $user_id;
}
}
Ajax代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#update').click(function(){
var user_id = $("#user_id").val();
var firstname = $("#firstname").val();
var lastname = $("#lastname").val();
var contact = $("#mob").val();
var dataString = 'firstname='+firstname + 'lastname='+lastname + 'contact='+contact+'user_id='+user_id;
if(firstname=='' || lastname=='' || contact=='') {
alert("Please fill all fields");
} else {
$.ajax({
type: "POST",
url: "update.php",
data: dataString,
cache: false,
success: function(html){
alert(html);
}
});
}
return false;
});
});
</script>
我希望能够在不重页加载的情况下编辑表单。
<form method="post" name="form">
<div style="position:relative; left:120px;">
<p>Title: <select><option value="Mr">Mr</option> <option value="Mrs">Mrs</option><option value="Miss">Miss</option><option value="Ms">Ms</option><option value="Dr">Dr</option></select></p>
<p>First name *: <input type="text" id="firstname" name="firstname" value="<?php echo $user_firstname; ?>" style="width:50%;"></p>
<p>Last name *: <input type="text" id="lastname" name="lastname" value="<?php echo $user_lastname; ?>" style="width:50%;"></p>
<p>Contact telephone number *: <input type="text" id="mob" name="contact" value="<?php echo $user_contact; ?>" style="width:50%;"></p>
<p><input type="hidden" id="user_id" name="user_id" value="<?php echo $_SESSION['user_id']; ?>" style="width:50%;"></p>
<button class="btn btn-primary btn-lg" id="update" name="update" role="navigation" type="submit" style="border-radius:0px;">Save & continue</button>
</div>
</form>
update.php
<?php require_once("includes/db.php"); ?>
if(isset($_POST['update'])){
$firstname = mysqli_real_escape_string($_POST['firstname']);
$lastname = mysqli_real_escape_string($_POST['lastname']);
$contact = mysqli_real_escape_string($_POST['contact']);
$user_id = mysqli_real_escape_string($_POST['user_id']);
$query = "UPDATE users
SET first_name ='". $_POST['firstname'] . "',
last_name ='". $_POST['lastname'] . "',
mob ='". $_POST['contact'] . "'
WHERE
user_id = '". $_POST['user_id'] . "'";
$edit_user_query = mysqli_query($connection,$query);
}
答案 0 :(得分:-1)
您没有设置用户ID。确保在您的ajax请求中发送userid或根据给定的数据获取它
最新情况:&GT;你需要将你的jquery改为
var dataString = 'firstname='+firstname + '&lastname='+lastname + '&contact='+contact;
我会将update.php更改为以下代码
<?php
require_once("includes/db.php");
//get user_id from session data or from call to db to combine session id with user_id
//this is depending on where you store this data for a currently logged in user
$userid = $_SESSION['user_id'];
if (isset($_POST['firstname'], $_POST['lastname'], $_POST['contact'])) {
$firstname = mysqli_real_escape_string($_POST['firstname']);
$lastname = mysqli_real_escape_string($_POST['lastname']);
$contact = mysqli_real_escape_string($_POST['contact']);
$query = "UPDATE users
SET first_name ='" . $_POST['firstname'] . "',
last_name ='" . $_POST['lastname'] . "',
mob ='" . $_POST['contact'] . "'
WHERE
user_id = '" . $userid . "'";
$edit_user_query = mysqli_query($connection, $query);
}
else {
echo "invalid response";
}
?>