2009-10-28 42 views
0

我有一系列動態生成的輸入,我需要在php文件中爲我更新ajax,一旦用戶點擊輸入。使用ajax發送更改爲輸入後的PHP文件

我認爲我有正確的代碼,但它不工作的原因超出我的理解。我是一個完整的javascript/ajax noob,所以要溫柔。

的JS/AJAX:

<script type="text/javascript"> 
// Initialize the object: 
var ajax = false; 

// Create the object... 

// Choose object type based upon what's supported: 
if (window.XMLHttpRequest) { 

    // IE 7, Mozilla, Safari, Firefox, Opera, most browsers: 
    ajax = new XMLHttpRequest(); 

} else if (window.ActiveXObject) { // Older IE browsers 

    // Create type Msxml2.XMLHTTP, if possible: 
    try { 
     ajax = new ActiveXObject("Msxml2.XMLHTTP"); 
    } catch (e1) { // Create the older type instead: 
     try { 
      ajax = new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (e2) { } 
    } 

} 
function update_program(row, target, value) { 
    if (ajax) { // Confirm that the object is usable 
     ajax.open('get', 'update.php?row=' + encodeURIComponent(row) + '&target=' + encodeURIComponent(target) + '&nfv=' + encodeURIComponent(value)); // Call the PHP script using GET, pass the variable 
     //ajax.onreadystatechange = handle_programupdate; // Function that handles the response 
     ajax.send(null); // Send the request 
    } 
} 
</script> 

而對於動態輸入字段

echo "<td><input type=text value=\"$value\" name=\"date\" onblur=\"update_program($i, $j, this.input.date.value);\" class=med></td></td>"; 

行計數的PHP是$ i,而該場數爲附加$ J。所以我需要發送的數據是行,字段(目標)和輸入的修改值。

輸入被安排在一個表中有很多很多(變化的行,63場)。每個字段可以是文本輸入,也可以是文本區域。如何讓他們在用戶更改後提交給php文件?

+0

您是否安裝了Firefox擴展Firebug? (或者在您選擇的瀏覽器中使用一個等效的JS控制檯。)這可以幫助您瞭解正在發生的事情。 – 2009-10-28 12:43:26

回答

1

我認爲這部分在onclick輸入

update_program($i, $j, this.input.date.value) 

應該

update_program($i, $j, this.value) 

而且,考慮使用jQuery,它使阿賈克斯容易得多。和其他一切太:P

function update_program(row, target, value) { 
    $.get('update.php', {row:row, target:target, nfv:value}); 
} 

而且,你正在使用time和您的參數是功能value,對不對?

+0

作出了改變......似乎沒有任何效果。林甚至不知道什麼事情發生時,我做了 – mrpatg 2009-10-28 11:57:04

+0

編輯,仍然沒有影響 – mrpatg 2009-10-28 12:09:03

相關問題