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 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding