如何对另一个文件执行 POST 并将该文件加载到主文件中?

How to do a POST to another file AND load that file inside the main file?

好的,我有 3 个文件。
- index.php
- verTicket.php(表示 getTicket.php)
- VerTickets.php

在 index.php 上,我有一个 header、一个滑块和一个 div 保存内容。 <div id="bigBody"></div>

同上index.php,我有一个link到verTickets.php。
<li><a href="#" onClick="$('#bigBody').load('verTickets.php')" id="getAllTickets">All Tickets</a></li>

单击此 links,将 verTickets.php 加载到 bigBody id 中。 他们,我用 SQL 内容填充 table,包括可点击的 link:
ECHO "<td><a class='ticketLink' id='" . $row['id'] . "'>". $row['title'] . "</a></td>";

然后,JavaScript:

   $(document).ready(
        function() {
            $(".ticketLink").click(function() {
                window.alert(this.id); //Test to see if id is being passed.
                $.post("verTicket.php", {
                operation: "createTicket",
                ticketId: "Test id"
                });        
            });
        }
    );

我已经测试了 $.post 在 MySQL 服务器上插入数据并且它有效。但我想 POST 不刷新并将页面加载到我的 bigBody div。

@EDIT - 解决方案! 谢谢,@timgavin

在我的 verTicket.php 中有一个 table 使用 MySQL_Fetch。不知道我是否理解你的回答,蒂姆。因此,我尝试在我的 verTicket.php 中创建一个变量,并将 sql 查询生成的所有内容放入其中,这样我就可以 return 并使用 html.

但是,在这样做的过程中,我遇到了这个问题:
$("#bigBody").load("verTicket.php", {ticketId: (this.id), title: (this.innerHTML)}); 使用此代码,我可以将我的 post 变量传递给 verTicket.php,并在处理后加载整个页面。

谢谢! :) `

简短回答:如果查询在 verTicket.php 中失败,return 一条 'fail' 消息,或者,如果查询成功 return 您要加载的数据。

verTicket.php

if(database query is successful) {
    return/echo the data here
} else {
    die('fail');
}

然后,在您的 .post javascript 函数中,检查 returned 数据是否为 ​​'fail',如果不是,则加载 return将数据输入div,否则打印错误信息。

$.post('verTicket.php',{operation: "createTicket",ticketId: "Test id"}, function(response) {
    if(response != 'fail') {
        $('#bigBody').html(response);
    } else {
        // error code goes here
    }
});