我有一個表(3 行),用戶可以通過單擊“選擇按鈕”來選擇一行的三個變量。我需要這三個變量作為 php 變量才能繼續(xù)使用它們。桌子echo '<table class="formatHTML5" id="myTable">';$count = 0;foreach($data2['wow_accounts']['0']['characters'] as $key => $item) { $count = $count + 1; echo '<tr>'; echo '<td class="p"><span>'; echo $count; echo '</td></span>'; echo '<td>'; echo $item['name']; echo '</td>'; echo '<td>'; echo $item['realm']['name']; echo '</td>'; echo '<td>'; echo $item['level']; echo '</td>'; echo '<td>'; echo '<button class="btnSelect">Select</button>'; echo '</td>'; echo '</tr>';} echo '</table>';表 HTML 輸出<table class="formatHTML5" id="myTable"><tbody><tr><td class="p"><span>1</span></td><td>User100</td><td>Malygos</td><td>1</td><td><button class="btnSelect">Select</button></td></tr></tbody></table>jquery腳本:$(document).ready(function(){ // code to read selected table row cell data (values). $("#myTable").on('click','.btnSelect',function(){ // get the current row var currentRow=$(this).closest("tr"); var col1=currentRow.find("td:eq(0)").html(); // get current row 1st table cell TD value var col2=currentRow.find("td:eq(1)").html(); // get current row 2nd table cell TD value var col3=currentRow.find("td:eq(2)").html(); // get current row 3rd table cell TD value var data1=col1; var data2=col2; var data3=col3; $.ajax({ type: "POST", url: "ajax-callback.php", data: data1, data2, data3; success: function(resultData) { alert("Save Complete") } }); });});我想在用戶單擊“選擇”按鈕后將每個表行的變量作為 php 變量。我讀到這可以通過 ajax 和“ajax-callback.php”文件實(shí)現(xiàn),所以我嘗試過但沒有成功。這是我的 ajax-callback.php:$data1 = $_POST['data1'] ;echo $data1;$data2 = $_POST['data2'] ;echo $data2;$data3 = $_POST['data3'] ;echo $data3;
1 回答

慕姐8265434
TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個贊
您需要一個對象來傳遞data
您想要在 $_POST 中使用的鍵。
這為您提供了 jQuery 在內(nèi)部序列化對象后要發(fā)送的各種鍵/值
改變
data: data1, data2, data3;
到
data: {data1: data1, data2: data2, data3: data3}, //note comma not `;`
- 1 回答
- 0 關(guān)注
- 157 瀏覽
添加回答
舉報
0/150
提交
取消