从jQuery调用PHP函数?

Bri*_*nte 16 php ajax jquery

我的网站上有一个PHP函数,需要几秒钟才能完成.这保持了我不想要的整个页面.

使用jquery可以在页面加载后调用这个PHP函数并在div中显示结果吗?还要在PHP函数完成之前显示ajax加载程序图像?

我一直在看jQuery.post,但似乎无法让它工作.

有人能帮忙吗?

谢谢

Ota*_*tar 19

AJAX的神奇之处在于:

$(document).ready(function(

    $.ajax({ url: 'script.php?argument=value&foo=bar' });

));
Run Code Online (Sandbox Code Playgroud)


Bri*_*nte 10

谢谢大家.我把你的每一个解决方案都用了一些,然后自己制作了.

最终的工作解决方案是:

<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            url: '<?php bloginfo('template_url'); ?>/functions/twitter.php',
            data: "tweets=<?php echo $ct_tweets; ?>&account=<?php echo $ct_twitter; ?>",
            success: function(data) {
                $('#twitter-loader').remove();
                $('#twitter-container').html(data);
            }
        });
   });
</script>
Run Code Online (Sandbox Code Playgroud)


hoo*_*ter 9

是的,这绝对是可能的.你需要在一个单独的php文件中使用php函数.这是使用$ .post的示例:

$.post( 
    'yourphpscript.php', // location of your php script
    { name: "bob", user_id: 1234 }, // any data you want to send to the script
    function( data ){  // a function to deal with the returned information

        $( 'body ').append( data );

    });
Run Code Online (Sandbox Code Playgroud)

然后,在你的php脚本中,只需回显你想要的html.这是一个简单的例子,但是一个开始的好地方:

<?php
    echo '<div id="test">Hello, World!</div>';
?>
Run Code Online (Sandbox Code Playgroud)

  • 如果php函数在包含不同函数的php文件中,我们该怎么办? (2认同)