2010-01-31 47 views
2

我有以下的代碼工作:

<script type="text/javascript"> 
$(document).ready(function() { 
    // Initialise the table 
    $('#table_1').tableDnD({ 
    onDrop: function(table, row) { 
    $.tableDnD.serialize(); 

    $.ajax({ 
    type: "POST", 
    url: "test.php?"+$.tableDnD.serialize(), 
    data: "", 
    success: function(html){ 
     alert("Success"); 
    } 
    }); 
    } 
}); 
}); 
</script> 

將數據發送到test.php的:

<?php 
$table_1[] = $_GET['table_1']; 
$i = 0; 
if(!empty($table_1[0])){ 
    foreach($table_1 as $value) { 
     foreach($value as $row){ 
      $i++; 
      mysql_query("UPDATE mytable SET tableOrder='$i' WHERE id = '$row'"); 
     } 
    } 
} 
?> 

正如你可以看到TABLE_1陣列檢索使用$ _GET的數據,但Ajax代碼說我們用POST發送。如果我將$ _GET更改爲$ _POST,則不再有效。爲什麼是這樣?

回答

5

當您從$_POST讀取數據時,應將值傳遞給data而不是URL查詢字符串。

你的JavaScript代碼將不得不改變如下:

$.ajax({ 
type: "POST", 
url: "test.php", 
data: $.tableDnD.serialize(), 
success: function(html){ 
    alert("Success"); 
} 
}); 

那麼你就能夠做到:

<?php 
$table_1[] = $_POST['table_1']; 
?> 

您的一部開拓創新的代碼是因爲工作作爲Mike Sherov中指出無論用於提交數據的HTTP動詞如何,在URL查詢字符串中傳遞的任何數據都可以使用$_GET進行訪問。

+0

您忘記了刪除?在url後面。 (請參閱下面的示例) – RJD22 2010-01-31 22:48:10

+2

不管使用什麼方法,在url中傳遞的任何數據總是被視爲GET。 – 2010-01-31 22:48:19

+0

@ RJD22:謝謝...修復它:) – 2010-01-31 22:57:40

0

我認爲這是因爲您正在使用查詢字符串構建POST URL並將來自tableDnD的值添加到您的URL。

請參閱the documentation,特別是「發送數據到服務器」部分。

0
url: "test.php?"+$.tableDnD.serialize(), 

這部分產生,你需要使用這樣一個GET請求:

$.ajax({ 
    type: "POST", 
    url: "test.php", 
    data: $.tableDnD.serialize(), 
    success: function(html){ 
     alert("Success"); 
    } 
    }); 
0

您的腳本不發送數據槽POST方法,和廣告目標文件是一些與獲取數據,PHP只是將其視爲通過URL傳遞的日期。

您需要將數據放入數據選項。 ;)

1

因爲你把參數放在URL中(這是一種GET方式)。要使用POST參數,您必須將其放入數據區域。喜歡這個。

<script type="text/javascript"> 
$(document).ready(function() { 
    // Initialise the table 
    $('#table_1').tableDnD({ 
    onDrop: function(table, row) { 
    $.tableDnD.serialize(); 

    $.ajax({ 
    type: "POST", 
 url: "test.php"+, data: "" + $.tableDnD.serialize(), 
    success: function(html){ 
     alert("Success"); 
    } 
    }); 
    } 
}); 
}); 
</script>

假設$.tableDnD.serialize()產生一個有效的查詢字符串。

希望這會有所幫助。