我表格中的每一行都有一個編輯按鈕。在PHP中使用JavaScript變量時未定義的索引
我試圖通過單擊編輯按鈕來獲取行號。我成功地在JavaScript中做到這一點,但在PHP中,我不知道如何做到這一點。
所以我想從JS的變量傳遞給PHP和某些原因,我得到一個錯誤
當我使用這個未定義指數:selectedRow
:$_GET['selectedRow']
;
最終目標是做出特定的行編輯器。
所以,如果你有不同的想法來完成它,我想聽聽。
相關一張我的代碼:
echo '<table width = "100%" id = "contactsTable"><tr>'.
'<th style=" width:3em; ">עריכה</th>'.
'<th style=" width:7em; ">אזור</th>'.
'<th style=" width:7em; ">תפקיד</th>'.
'<th style=" width:15em; ">הערה</th>'.
'<th style=" width:15em; ">אימייל</th>'.
'<th style=" width:10em; ">טלפון</th>'.
'<th style=" width:15em; ">כתובת</th>'.
'<th style=" width:10em; ">שם מלא</th>';
while($row = mysql_fetch_array($query)){
echo '<tr><td onclick="selectedRow(this)">'.
'<a href="?editRow=true">'.
'<input type="image" src="../image/edit-icon.png" alt="עריכה" width="30" height="30">'.
'</a></td>'.
'<td>'.$row['area'].'</td>'.
'<td>'.$row['role'].'</td>'.
'<td>'.$row['note'].'</td>'.
'<td>'.$row['email'].'</td>'.
'<td>'.$row['phoneNumber'].'</td>'.
'<td>'.$row['address'].'</td>'.
'<td>'.$row['fullName'].'</td>'.
'</tr>';
}
echo '</table>';
echo '<script>';
echo 'function selectedRow(obj) {'.
'var num = obj.parentNode.rowIndex - 1;'.
'alert ("selectedRow: "+num);'.
'window.location.href = "?selectedRow="+ num;'.
'}';
echo '</script>';
}
if(isset($_GET['editRow'])){
echo 'selectedRow :'. $_GET['selectedRow'];
}
我想還,而不是使用'window.location.href = "?selectedRow="+ num;'.
AJAX:
'$.ajax({'.
'type: "POST",'.
'url: "index.php",'.
'data: "selectedRow=" + num'.
'});'.
嘗試分離html和php代碼。調試和閱讀會更容易。 – msantos
累了。 頁面上的所有數據都來自PHP的SQL。 – Shachar87
你永遠不會把JS變量發送給PHP。 –