繁体   English   中英

在PHP中使用JavaScript变量时的未定义索引

[英]Undefined index when using JavaScript variable in PHP

表格中的每一行都有一个“编辑”按钮。

我试图通过单击“编辑”按钮来获取行号。 我在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'];
}

我也尝试使用AJAX代替'window.location.href = "?selectedRow="+ num;'.

                 '$.ajax({'.
                 'type: "POST",'.
                 'url: "index.php",'.
                 'data: "selectedRow=" + num'.
             '});'.

在while循环中使用以下文本更改锚标记href值:

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

现在替换脚本中的window.location.href

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

它将按您的条件工作。

我继续进行测试,有人在他的计算机上测试了我的代码,并告诉我我的代码可以工作,我继续进行测试并最终找到了工作方式。

我删除了以下行: '<a href="?editRow=true">'. 从桌子上。

我添加了editRow =真正的JS功能,像这样selectedRow: 'window.location.href = "?editRow=true&selectedRow="+ num;'.

现在它可以正常工作了,无论如何它都应该同时工作,所以我不知道问题出在哪里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM