2016-07-22 68 views
0

我表格中的每一行都有一個編輯按鈕。在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'. 
      '});'. 
+0

嘗試分離html和php代碼。調試和閱讀會更容易。 – msantos

+0

累了。 頁面上的所有數據都來自PHP的SQL。 – Shachar87

+0

你永遠不會把JS變量發送給PHP。 –

回答

1

在while循環與下面的文字更改您的錨標記href的值:

<a href="?editRow=true"> <---> <a href="javascript:;"> 

現在更換您的window.location.href腳本

'window.location.href = "?selectedRow="+ num;'. <---> 'window.location.href = "?editRow=true&selectedRow="+ num;'. 

它將工作文件中的條件。

0

我繼續做測試,有人測試在他的電腦我的代碼,並告訴我我的代碼工作,我繼續我的測試,並最終找到它的工作方式。

我從表格中刪除了這一行:'<a href="?editRow=true">'.

我添加editRow =真給JS函數selectedRow這樣的:'window.location.href = "?editRow=true&selectedRow="+ num;'.

而現在它的工作,反正它應該是雙向的,所以我不知道是什麼問題。