doubaisui2526 2013-07-25 05:44
浏览 35
已采纳

jQuery / php / mysql - 如何在不重新加载页面的情况下操作mysqli_query?

I have a table containing data read from a MySQL database via PHP. The first column holds all item names. Now, on clicking a td element in the first column of the table would link to a page with more detailed information about the item contained in the td.

Now I came up with the following idea:

$(document).ready(function() {
    $('#table td:first-child').click(function() {
        $('div.main').animate({
            height: "50px"
        }, 600);
        setTimeout(function() {
            $('div.data').fadeIn(1000);
        }, 600);
    });
});

div.main is the div-container that has the table included. What I want to do now is to slide that container up and fade a new div-container in, right below it, the new container include()s a PHP page which holds a dynamic query (pseudocode, no string escaping, simplified version):

SELECT detail FROM items WHERE items.name = $_GET['name'];

What I couldn't figure out is if and how I can tell the PHP file that is included in the in-fading div-container which item name it has to grab details for, off the database.

Right now I can read the item name via JavaScript/jQuery, but I couldn't figure a way out to pass that value to the PHP file without having to reload the page.

Any ideas or suggestions welcome!

  • 写回答

4条回答 默认 最新

  • douhuang4166 2013-07-25 05:55
    关注

    I think what you're looking for is asynchronous JavaScript and XML (AJAX). It sounds intimidating, but fortunately jQuery makes it very easy.

    You can call $.ajax() directly, but for most cases, you can use one of the convenience wrappers. In this case, I think $.load() will meet your needs.

    So, let's say your PHP file is called detail_ajax.php and it returns the HTML you wish to put in your div (with class data). All you would have to do then is this:

    $('div.data').load( '/detail_ajax.php', function(data){
        $(this).html(data);
    });
    

    If you want to pass data TO detail_ajax.php, you can pass it along this way:

    $('div.data').load( '/detail_ajax.php', { 'someField' : 'someValue' },
        function(data) {
            $(this).html(data);
        }
    });
    

    In detail_ajax.php, if you examine $_POST['someField'], you will see the value passed in.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败