简体   繁体   English

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

[英]Undefined index when using JavaScript variable in PHP

Each line in my table has an Edit button. 表格中的每一行都有一个“编辑”按钮。

I trying to fetch the row number by clicking on the Edit button. 我试图通过单击“编辑”按钮来获取行号。 I succeed to do that in JavaScript but in PHP I don't know how. 我在JavaScript中成功做到了这一点,但在PHP中我不知道如何做到。

So I thought to pass the variable from JS to PHP and from some reason I get an error 所以我想将变量从JS传递到PHP,由于某种原因我得到了一个错误

Undefined index: selectedRow 未定义索引:selectedRow

when I use this: $_GET['selectedRow'] ; 当我使用这个: $_GET['selectedRow'] ;

The final goal is to make specific row editor. 最终目标是制作特定的行编辑器。

So if you have an different idea to make it done I'd like to hear. 因此,如果您有不同的想法可以实现,我想听听。

Relevant piece of my code: 我的代码的相关部分:

   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'];
}

I tried also to use AJAX instead of 'window.location.href = "?selectedRow="+ num;'. 我也尝试使用AJAX代替'window.location.href = "?selectedRow="+ num;'. :

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

Change your anchor tag href value in while loop with following text : 在while循环中使用以下文本更改锚标记href值:

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

And now replace your window.location.href in script 现在替换脚本中的window.location.href

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

It will work file in your condition. 它将按您的条件工作。

I continued to do tests, Someone test my code in his computer and told me that my code works, I continued my tests and eventually found the way it works. 我继续进行测试,有人在他的计算机上测试了我的代码,并告诉我我的代码可以工作,我继续进行测试并最终找到了工作方式。

I removed this line : '<a href="?editRow=true">'. 我删除了以下行: '<a href="?editRow=true">'. from the table. 从桌子上。

I add the editRow=true to the JS function with selectedRow like this: 'window.location.href = "?editRow=true&selectedRow="+ num;'. 我添加了editRow =真正的JS功能,像这样selectedRow: 'window.location.href = "?editRow=true&selectedRow="+ num;'.

And now its working, Anyway it should work both ways so I don't know what was the problem. 现在它可以正常工作了,无论如何它都应该同时工作,所以我不知道问题出在哪里。

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

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