jQuery使用PHP脚本更新Div

Joh*_*nny 4 php ajax jquery request

我完全不知道怎么做,所以我要继续问问.

我想要做的是使用我在外部文件中的PHP脚本更新div的内容,名为send.php.

所以我有这样的div:

<div class="classname">

</div>
Run Code Online (Sandbox Code Playgroud)

我想将数据发布到此send.php文件,然后使用PHP脚本的结果更新该div.可以这样做吗?

kar*_*m79 5

对于简单的ajax调用,我通常更喜欢使用$ .load,因为它的语法非常简洁.将参数作为对象(键/值对)传递将使其使用POST请求:

<a href="no-script.php" class="something">Click!</a>

$(document).ready(function() {
    $('a.something').click(function(e) {
        //prevent the href from being followed
        e.preventDefault();

        //inject div.classname with the output of send.php
        $('div.classname').load('send.php', {param1: 'foo', param2: 'blah'});
    });
});
Run Code Online (Sandbox Code Playgroud)

如果您不需要它作为POST,您只需将参数添加为查询字符串:

$('div.classname').load('send.php?param1=' + param1 + '&param2=' + param2);
Run Code Online (Sandbox Code Playgroud)