【问题标题】:Calling PHP function using ajax not working使用ajax调用PHP函数不起作用
【发布时间】:2012-04-12 22:44:17
【问题描述】:

我正在使用 ajax 调用调用 php 函数,但它不起作用。当我单击行的任何列时,我有一个包含一些行的表,它的值可以编辑,但是当我编辑值并单击另一列或输入更改的值时,不会再次显示。实际上我正在做一个 ajax 调用,我在其中更改表中列的数据,但它没有调用该 php 函数。

我的脚本如下

<script type="text/javascript" >
$(document).ready(function()
{
    $(".edit_tr").click(function()
    {
        var ID=$(this).attr('id');
        $("#span_"+ID).hide();
        $("#input_"+ID).show();
    }).change(function()
    {
        var ID=$(this).attr('id');
        var input=$("#input_"+ID).val();
        var dataString = 'id='+ ID +'&data='+input;
        $("#span_"+ID).html('<img src="load.gif" />'); // Loading image

        if(input.length>0)
        {

            $.ajax({
                type: "POST",
                url: "worker_app::edit_ajax()",
                data: dataString,
                cache: false,
                success: function(html)
                {
                    $("#span_"+ID).html(span);
                }
            });
        }
        else
        {
            alert('Enter something.');
        }

    });

    // Edit input box click action
    $(".editbox").mouseup(function()
    {
        return false
    });

    // Outside click action
    $(document).mouseup(function()
    {
        $(".editbox").hide();
        $(".text").show();
    });

});

HTML 表格如下所示

<tbody>
  <tr id="{IDWORKERS}" class="edit_tr">
    <td class="edit_td">
      <span id="span_{IDWORKERS}" class="text">{FIRM}</span>
      <input type="text" value="{FIRM}" class="editbox" id="input_{IDWORKERS}" />
    </td> 
  </tr>
</tbody>

而 php 函数位于 apps 文件夹中的一个名为 wroker_app.php 的文件中

public function edit_ajax(){
    ///echo "<pre>";
    ///print_r($_POST);
    //echo "</pre>";
   // sql to update the database goes here
    echo 'I am here';
}

有什么想法吗?

提前致谢

【问题讨论】:

  • 我不知道什么都没发生 :( 我是 ajax 新手,如何检查 ajax 调用?
  • alert(html) inside 成功并告诉我们结果

标签: php ajax jquery


【解决方案1】:

您不能单独使用请求来调用特定函数。你需要告诉你的脚本应该执行edit_ajax。

因此,将您的 url 更改为 worker_app.php,使用(例如)get 变量(例如 ?[function])监听请求。

if (isset($_GET['edit_ajax']) && function_exists($_GET['edit_ajax']))
     edit_ajax();

【讨论】:

  • 您似乎尝试通过请求 url /worker_app::edit_ajax.php 来调用 edit_ajax()。这不可能。不能单独使用请求 url 调用函数。
  • 那我该怎么做才能调用函数呢?
  • 调用网址:'/worker_app.php?edit_ajax'。在你的worker_app.php文件中,监听上面sn-p的调用。
【解决方案2】:

你不能像这样调用 php 函数。您只能调用一个 php 文件,您可以在其中决定应该执行哪个函数。 一个简单的例子:

$.ajax({
 type: "POST",
 url: "/apps/worker_app.php",
 data: dataString,
 cache: false,
 success: function(html) {
   $("#span_"+ID).html(span);
 }
});

在您的应用程序/worker_app.php 中:

<?php 

function edit_ajax(){
  echo 'I am here';
}

// You can put some logic before this line to decide which function to call based on the request
edit_ajax();

【讨论】:

  • 你可以在我刚刚复制你的代码并添加调用的函数之前编写它。
  • 我写edit_ajax();就在我的函数之前,它给了我一个语法错误:(
  • 把错误信息贴在这里,但调用的地方不会是问题
  • 我在 ajax 调用中设置了我的 url,如下 url:“/apps/worker_app.php”,在我的应用程序/worker_app 文件中,我有我的方法 edit_ajax(),就在这个函数开始之前我有 edit_ajax();它给出的错误是 Parse error: syntax error, unexpected T_STRING, Expecting T_FUNCTION in C:\wamp\www\personal\apps\worker_app.php 在第 549 行
  • 哦,你不能简单地调用 edit_ajax() 你必须在类的实例上调用它(或者如果它是静态函数,则调用静态方式)
【解决方案3】:

url: "worker_app::edit_ajax.php"
这个网址指向哪里。

检查此参考 jquery .ajax

如果您想调用函数,请尝试将任何特定的帖子字段发布到该文件。
在该文件中使用后

 if($_POST['field']!="") { 
      requredfunction () {
           // your code run here
       } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2015-02-13
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多