2012-03-29 20 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 
     } 
}