代码在上次刷新之前返回最新值而不是插入的最新值?

时间:2015-02-06 12:29:55

标签: javascript php jquery postgresql

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

<!-- 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)存储在数据库中: (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)

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

        $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行的数据。

唯一不能正常工作的是: 当我点击按钮x时,它会打开一个对话框,但会显示一个值,但不会显示行x的值。但是,当我看到数据库时,行x存储在那里。复选框中的值是在页面上的最新刷新之前单击的按钮的值。它似乎在我的电话链中有一些错误或某些东西(我无法弄清楚,这就是我要问的原因)。

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

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

2 个答案:

答案 0 :(得分:1)

我建议您通过JSON从POST返回数据。请注意,AJAX调用是异步的。所以你不知道答复何时到来。 因此,您需要使用ajax Success回调函数处理结果。

</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 
         do_post_and_show_info(names, surname);
        });
    });

function do_post_and_show_info(names, surname){
  request= $.ajax({
    type:      "post",
    cache:     false, 
    url:       "show_lecturer.php", 
    data: { x: names, y: surname}  ,
    dataType: "json",
    });

    request.done(function(json){
        if (json.status =="ok"){
            // DO YOUR THING!   
            Alert(json.data.names + " " + json.data.surnames);
        }
        else {
            alert("Error! " + json.error + " : " + json.remarks);
        }
    });


    request.fail(function(jqXHR, textStatus) {
        alert( "Request failed: " + textStatus  + ":" + jqXHR.responseJSON); 
    });  
}//do_post_and_show_info
</script>

我通常在PHP中返回这样的数据结构(所以在你的show_lecturer.php中)

<?
  // get your data before this in the variable $data
  // put your status "OK" or "ERROR" in $status
  // put some error info in $extraInfo
// of course some processing is involved, but here's a simple example

require_once('connect_db.php'); 

$name    = $_POST['x']; 
$surname = $_POST['y'];         
$result  = pg_query(connect(), "INSERT INTO keep_track VALUES ('$name', '$surname')");   
// obviously you need to do some error checking, but here's the happy flow
$status = "OK";
$error  = "";

$data['names']    = $name;
$data['surnames'] = $surname;


echo json_encode(array(
    "status" => $status,
    "error"  => $error,
    "remark" => $extraInfo,
    "data"   => $data
  ));

&GT;

请注意,这是我在编辑器中创建的示例,而不是实际的工作设置。所以请尝试理解它,而不是复制粘贴它并给它一个运行。

答案 1 :(得分:0)

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

$("#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页面。由于我在请求内容时传递了搜索参数,因此我获得了我想要的数据。

之前的问题是,当页面加载时,div是用数据创建的(即使我没有点击任何按钮)。因此,当我点击一个按钮时,它会显示div,其中包含上一页加载(上次刷新)的内容。

我还需要做一些其他的小改动才能让它发挥作用,但这是主要的想法。