代码返回上次刷新之前的最新值,而不是插入的最新值? [英] Code returns the latest value before last refresh instead of the latest value inserted?

查看:75
本文介绍了代码返回上次刷新之前的最新值,而不是插入的最新值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表中有一列按钮,声明如下: (文件index.php) 回声''; 然后,此脚本读取单击的按钮行中的数据,并将其发布到另一个php文件中:

I have a column of buttons in a table, declared like this: (file index.php) echo ''; Then this script reads the data in the row of the button clicked and posts it to another php file:

<!-- scripts that gets the lecturer chosen to SHOW functionality-->
    <script>
        $(document).ready(function(){
             $(".show-button").click(function() {
             var $row = $(this).closest("tr");    // Find the row
             var names = $row.find(".name").text(); // Find the name
             var surname = $row.find(".surname").text(); // Find the surname 
                 $.ajax({  type: "POST",  url: "show_lecturer.php", data: { x: names, y: surname}   }) 
            });
        });
</script>

该文件(show_lecturer.php)将读取的数据存储在数据库的表(keep_track)中: (文件show_lecturer.php)

That file (show_lecturer.php) stores the data read in a table (keep_track) in the database: (file show_lecturer.php)

<?php
ob_start(); //eliminates buffer collisions

    require_once('connect_db.php'); 
    $name = $_POST['x']; 
    $surname = $_POST['y'];         
    $result = pg_query(connect(), "INSERT INTO keep_track VALUES ('$name', '$surname')");   

?>

然后,我使用jquery创建一个空对话框,以使用从数据库中获取的数据填充该对话框: (文件index.php)

Then I create an empty dialogbox with jquery, to populate it with the data taken from the database: (file index.php)

<!-- The following script generates the empty dialog box -->
<script src="/js/jquery.min.js"></script>
<link rel="stylesheet" href="/css/jquery-ui.css">
<script src="/js/jquery-ui.min.js"></script>
<script>
    $(function() {
        //show lecturer dialog
        $("#show_dialog").dialog({autoOpen: false});
        $(".show-button").on("click", function() {$("#show_dialog").dialog("open");});
    });
</script>

然后这些数据取自表keep_track并在以上对话框中回显: (文件index.php)

Then these data are taken from the table keep_track and echoed in the above dialog: (file index.php)

        $name; $surname;
        require_once('connect_db.php'); 

                $firstname = pg_query(connect(), "SELECT name FROM keep_track");
                while($row = pg_fetch_array($firstname)){ $name = $row['path']." ".$row['name'];    }
                $lastname = pg_query(connect(), "SELECT surname FROM keep_track");
                while($row = pg_fetch_array($lastname)){ $surname = $row['path']." ".$row['name'];  }       
                echo '<div id="show_dialog" class="ui-dialog-content ui-widget-content">';      
                echo $name."".$surname; 
                echo '</div>';

?>

因此,当我单击行x的按钮时,将打开一个对话框,其中包含行x的数据.

So when I click the button of row x, a dialogbox opens with the data from the row x.

唯一无法正常工作的是: 当我单击按钮x时,它会打开一个对话框,但显示一个值,但不显示行x的值.但是,当我看到数据库时,x行存储在该数据库中.复选框中的值是页面上的最新刷新之前单击的按钮的值.好像我的通话链中有什么错误之类的东西(我无法弄清楚,这就是我问的原因).

The only thing that is not working correctly is this: The moment I click button x, it opens a dialog but displays a value, but not that of row x. However, when i see the database, the row x is stored there. The value in the checkbox is that of the button clicked before the latest refresh on the page. Its as if there is some mistake in my chain of calls or something (that I cant figure out, thats why Im asking).

为了说明我得到的数据: (表keep_track最初是空的)

To illustrate the data I get: (Initially the table keep_track is empty)

Press button 1 -> row 1 stored, dialogbox has no content
Press button 2 -> row 2 stored, dialogbox has no content
Press button 3 -> row 3 stored, dialogbox has no content
  Refresh page manually
Press button 4 -> row 4 stored, dialogbox has content from row 3
Press button 5 -> row 5 stored, dialogbox has content from row 3
  Refresh page manually
Press button 6 -> row 6 stored, dialogbox has content from row 6
Press button 7 -> row 7 stored, dialogbox has content from row 3

推荐答案

我将对话框(div)的内容写在另一个文件中并使用

I wrote the content of the dialog (div) in another file and used

$("#div").load("content.php", {x:parameter_1, y:parameter_2, ......});

代替

$.ajax({  type: "POST",  url: "show_lecturer.php", data: { x: names, y: surname}   }) 

这成功了.

现在div最初是不可见的并且为空,但是一旦单击该按钮,它将要求加载content.php页面.由于我在请求内容时传递搜索参数,因此我得到了想要的数据.

Now the div is initially invisible and empty, but once the button is clicked, it requests the content.php page to load. Since I'm passing the search parameters when I request the content, I get the data that I wanted.

以前的问题是,在加载页面时,div是用数据创建的(即使我没有单击任何按钮).因此,当我单击一个按钮时,它将向我显示具有上次页面加载(上次刷新)中的内容的div.

The problem from before was that when the page loaded, the div was created with the data (even though I hadn't clicked any button). Therefore, when I 'd click a button, it would show me the div with the content from the last page load (last refresh).

为了使它正常工作,我还需要做其他小的更改,但这是主要思想.

There were also other minor changes I had to do to make it work, but this is the main idea.

这篇关于代码返回上次刷新之前的最新值,而不是插入的最新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆