【问题标题】:How to call a PHP function in an URL of ajax?如何在 ajax 的 URL 中调用 PHP 函数?
【发布时间】:2015-02-15 15:04:24
【问题描述】:

我已经编写了一个带有 echo 语句的单个函数的 PHP 代码,我希望在 ajax 代码中将该函数称为 url . 我厌倦了这样做。

<html>
    <head>
        <title>Writing PHP Function</title>

        <script>

        $.ajax(
        {
        url : localhost/practice.php/f=myFirst();
        type: "GET",
        data: dataString,

        success: function(result)
        {
        alert(result);
        }
        });

        </script>

    </head>
<body>

        <?php

            function myFirst()
            { 
             echo 'The First ran successfully.'; 
            }

        ?>

</body>
</html>

【问题讨论】:

  • 你想达到什么目的?如果您调用该函数...它将附加 The First ran successfully 这没有意义
  • 您需要进一步详细说明。提供 php 代码并告诉我们确切的问题是什么。
  • 1.您需要将您的 url 更改为 localhost/practice.php/?f=myFirst 和 2。您需要返回您的值,而不是打印它。 3. 您应该将请求映射到函数

标签: php html ajax


【解决方案1】:

我会尝试根据 URL 中的参数调用函数。 Ajax部分,

$.ajax({url:'localhost/practice.php?f=myFirst',type:'GET'}).done(function(response){
  alert(response); // or you can write to your document
});

和 PHP 文件

<?php
      if(isset($_GET) && $_GET['f'] == 'myFirst'){
        myFirst();
      }else{
        die('No GET params received');
      }

      function myFirst(){ 
           echo 'The First ran successfully.'; 
      }

?>

【讨论】:

    【解决方案2】:

    你所拥有的东西有很多问题。这只是一个适合您的简单示例。

    practice.php

    <?php
        if(isset($_GET['f']))
            echo strip_tags($_GET['f']);
    ?>
    

    index.php

    <?php function myFirst() {
        echo 'The First ran successfully.';
    } ?><html>
    <head>
    <title>Writing PHP Function</title>
    <!-- You need to add the jQuery library -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script>
    // You should activate it on an event like click (unless you want it to autoload)
    $("#content").click(function() {
        $.ajax({
                // Your url is funky, and cannot be terminated with a semicolon
                // but rather a comma
                url : 'localhost/practice.php?f=<?php myFirst(); ?>',
                type: "GET",
                // You can do an alert, but I just made it populate a div instead
                success: function(result) {
                    $("#place-to-load").html(result);
                }
            });
        });
    </script>
        </head>
    <body>
    <!-- Your php function will not work as you intend because one -->
    <!-- is a server-side function and the other is a client-side script -->
    <!-- The functions are not interchangeable. That being said, you can -->
    <!-- populate a javascript with a php variable but it would need to be -->
    <!-- echoed like <?php myFirst(); ?> -->
    <div id="place-to-load"></div>
    <div id="content">Load</div>
    </body>
    </html>
    

    【讨论】:

    • 先生,谢谢这个正在工作.. 但我的主要议程是有一个 PHP 文件具有多种功能,如插入,从数据库中检索。我想在单击插入按钮时使用 Ajax 调用一个特定函数并在我的页面上显示。
    • 你把所有这些功能放在你的practice.php页面上。
    • 您所有的工作 php 脚本都在 practice.php 页面上。 index.php 页面所做的就是激活这些脚本/函数并将它们调用到 index.php 页面上的容器中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多