-5

我有一个简单的 HTML 表单,其中包含多个字段(如 id、姓名、联系人和电子邮件)。用户填写表单并提交后,数据将存储在数据库中,同时(甚至在同一页面上),表格将显示已存储的数据。

现在,我想在表格底部放置一个更新按钮。当用户点击它时,他们可以查看表单,该表单将预先填写他们之前输入的数据,因此他们不需要输入两次数据。

我怎样才能做到这一点?如何根据 id 字段捕获其他字段?

4

5 回答 5

3

您将不得不使用如下所述的 javascript 和 ajax 代码。试着理解它。

<script type="text/javascript">  
$(document).ready(function(e) {
    $.ajax({
        //Your ajax here...
        success: function(response) {
            //Your code of showing inserted data in table....
            //get the inserted userid from response and set update button's ONCLICK attribute as mentioned below
            var userid = getuseridfromresponse;
            $("#updatebuttonid").attr("onclick","updaterecord("+userid+")");
        }
    });
});
function updaterecord(userid){
    $.ajax({
        //your ajax here you
        //send all the details from php code using JSON or whatever you are using.
        success: function(response) {
            //store all the data into js variables.
            var userid = response.userid;
            var fullname = response.fullname;
            // and so on..
            $("#fullname").val(fullname); //this will set the textbox value to fullname
            //and so on..
            $("#userid").val(userid); //this is a hidden input in inser form. by default it will be zero. you will need it while updating.
            $("#actionname").val("update"); //this is also hidden input in insert form. default value would be "insert"
            // you just need to check in php that which actionname you are getting.

            //if you will get actionname = insert Do insert code
            //if you will get actionname = update Do update code.
        }
    });
}
</script>
于 2012-12-03T07:16:19.563 回答
1

这些是简单的数据库操作。网上有很多教程,google一下。你可以参考这个

于 2012-12-03T05:42:06.083 回答
1

如果您想在不刷新页面的情况下在页面上显示新条目,请使用AJAX..
否则刷新页面并用于Select query显示新条目。
对于第二个问题.. 使用Select query更新按钮按下然后你的echo每个字段这是一种简单的方法..valueHTML input fields

// php code
$result = mysql_query('Select * from table_name WHERE id = 1');
$row = mysql_fetch_assoc($result);
// use loop if query return multiple rows
// HTML code
<input type = "textbox" name="age" value="<?php echo $row['age'] ?>">
于 2012-12-03T05:43:14.807 回答
1

带有更新按钮的表单需要一个hiddenid 字段。然后 PHP 需要从数据库中选择对应 id 的记录,并以表格的形式显示出来。

如果每一页只有一条记录,您可以将预先填写的表格放在与div表格相同的页面上。然后,您可以使用 JavaScript 隐藏 div页面加载时间并显示div用户单击更新按钮的时间。

于 2012-12-03T06:56:29.017 回答
1

单击表数据后,将其 ID 传递给它。然后检查 ID 并从数据库中获取数据并以 html 形式设置值。如果传递了 ID,则应该进行更新,如果没有 ID,则应该是新的插入。

于 2012-12-03T05:37:15.327 回答