2012-03-29 89 views
1

我打电话使用ajax调用php函数,但它不工作。当我点击某行的某一列时,它的值可以编辑,但是当我编辑该值并单击另一列或输入更改的值时,将不会再显示,因此我有一个包含某些行的表。其实我正在做一个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'; 
} 

任何想法?

在此先感谢

+0

你得到什么错误? – safarov 2012-03-29 09:44:12

+0

我不知道什么发生:(我是新来的ajax我怎么能检查ajax调用? – 2012-03-29 09:44:59

+0

alert(html)里面成功并告诉我们结果 – 2012-03-29 09:48:35

回答

1

下面使用后你不能调用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(); 
+0

让我试试 – 2012-03-29 09:58:08

+0

为什么你在函数后面写了edit_ajax()? – 2012-03-29 09:59:46

+0

你可以在函数之前写它, t复制你的代码并添加了呼叫。 – erdeszt 2012-03-29 10:04:59

2

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

因此,请将您的网址更改为worker_app.php,使用(例如)获取变量(如?[函数])来侦听请求。

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

没有得到你的观点你能解释一下吗? – 2012-03-29 09:46:56

+0

看起来你试图通过请求url /worker_app::edit_ajax.php来调用edit_ajax()。这是不可能的。你不能单独使用请求url调用函数。 – Daniel 2012-03-29 09:48:08

+0

那么我能做些什么来调用函数? – 2012-03-29 09:48:57

0

网址: “worker_app :: edit_ajax.php”
这哪里是URL referrs来。

检查这个参考 jquery .ajax

如果你想调用函数使用尝试任何具体的职位后场到该文件。
在该文件中

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