使用AJAX和PHP更新表单字段

使用AJAX和PHP更新表单字段,php,jquery,ajax,Php,Jquery,Ajax,我有一个用户可以编辑的表单。我希望表单在不使用Ajax重新加载页面的情况下进行更新。新值将替换旧值。对于输入字段,我从php SQL查询中读取值。因此,用户第一眼就能看到这些细节。问题是当这个代码运行时,我没有得到任何错误和结果。我试过console.log 我从while循环中获取用户值 if(isset($_GET['edit_user'])){ $the_user_id = $_GET['edit_user']; $query = "SELECT * FROM users

我有一个用户可以编辑的表单。我希望表单在不使用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>

$(文档).ready(函数(){
$(“#更新”)。单击(函数(){
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\u id='+user\u id;
如果(名字=“”| |姓氏=“”| |联系人=“”){
警告(“请填写所有字段”);
}否则{
$.ajax({
类型:“POST”,
url:“update.php”,
数据:dataString,
cache:false,
成功:函数(html){
警报(html);
}
});
}
返回false;
});
}); 
我希望能够在不重新加载页面的情况下编辑表单

<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> 

标题:MrsMissMsDr先生


名字*:您不设置用户id。请确保在ajax请求中发送用户id或根据给定数据获取用户id

更新->您需要将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";
}

?>

这个函数在哪里
确认()
它做什么?表示了解的语句。即使是这样也不安全!嗨,弗雷德-我现在收到了阿贾克斯的回复。但是update.php查询不起作用。confirm()是我在functions.php中创建的函数。与此无关。别担心。弗雷德,我删除了确认功能。仍然无法工作。hans-你能修改我的代码以便我理解吗?不是从我的手机:)我建议将用户id作为一个元素添加到表单中。如果愿意,可以将其隐藏或禁用。更新javascript以将用户ID添加到数据字符串。更改update.php以从postrequest获取用户id。考虑如何验证USER输入,执行一些错误处理,避免SQL链接。汉斯,我已经更新了我的代码和你的建议。你能看一下吗?我已经用修改过的代码尝试了你建议的一切。现在,来自ajax代码的数据字符串被发布到firstname字段中。
<?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";
}

?>