阿贾克斯按一下按钮,触发一个PHP文件/链接按钮、链接、文件、阿贾克斯按

2023-09-10 20:34:13 作者:别说谁变了你拦得住时间吗

我想要一个简单的按钮,当我点击,它显示了一个文件中的内容为file.php (当它触发该文件显示了一个随机数),如果它点击数次刷新从内容为file.php

I want a simple button that when I click, it shows the content from a file file.php ( this file when triggered it shows a random number) and if it's clicked several times refreshes the content from file.php.

推荐答案

您可以尝试这样的:

PHP文件:为file.php

PHP File: file.php

<?php
    //your php code for random number...
?> 

HTML文件:

<!DOCTYPE html>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <input type="button" id="clkMe" value="Load File" />
        <div id="response"></div>
        <script>
            $(document).ready(function(){
                $("#clkMe").click(function(){
                    var dataString={};
                    $.ajax({                                      
                        url:"file.php",
                        type: 'POST',
                        cache:false,
                        data: dataString,
                        beforeSend: function() {},
                        timeout:10000,
                        error: function() { },     
                        success: function(response) {
                           $("#response").html(response);
                           alert(response);
                        } 
                    });
                });
            });
        </script>
    </body>
</html>

我希望这是你所问。

I hope this is what you are asking.

编码快乐!