jQuery 使用 PHP 脚本更新 Div

发布于 2024-08-04 14:22:44 字数 235 浏览 7 评论 0原文

我完全不知道该怎么做,所以我只想继续问。

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

所以我有一个像这样的 div:

<div class="classname">

</div>

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

I have absolutely no idea how to do this, so I'm just gonna go ahead and ask.

What I want to do is update the contents of a div with a PHP script I have in an external file, called send.php.

So I have a div like this:

<div class="classname">

</div>

And I want to post data to this send.php file, and then update that div with whatever the outcome of the PHP script is. Can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

歌入人心 2024-08-11 14:22:44

对于简单的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'});
    });
});

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

$('div.classname').load('send.php?param1=' + param1 + '¶m2=' + param2);

For simple ajax calls, I normally prefer using $.load as its grammar is extremely concise. Passing parameters as an object (key/value pairs) will cause it to use a POST request:

<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'});
    });
});

If you don't need it to be a POST, you can just add your parameters as a query string:

$('div.classname').load('send.php?param1=' + param1 + '¶m2=' + param2);
筱果果 2024-08-11 14:22:44
<a href="#">click</a>

<script>
    $(function() {
      $("#refresh").click(function(evt) {
         $("#classname").load("send.php?data=someData")
         evt.preventDefault();
      })
    })
</script>


<div id="classname">

</div>

一些需要阅读的文档:

  1. http://docs.jquery.com/Ajax/jQuery。帖子

<a href="#">click</a>

<script>
    $(function() {
      $("#refresh").click(function(evt) {
         $("#classname").load("send.php?data=someData")
         evt.preventDefault();
      })
    })
</script>


<div id="classname">

</div>

Some documentation to read:

  1. http://docs.jquery.com/Ajax/jQuery.post

看轻我的陪伴 2024-08-11 14:22:44

绝对地!请查看这篇文章,了解有关如何使用 jQuery 的 ajax 功能的说明。然后你需要调用

$.(".classname").append("Whatever results you get");

To fill the div。

祝你好运!

Absolutely! Take a look at this post for instructions on how to use jQuery's ajax functionality. You are then going to want to call

$.(".classname").append("Whatever results you get");

To fill the div.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文