Shachar87 Shachar87 - 2 months ago 9
Ajax Question

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.

So I thought to pass the variable from JS to PHP and from some reason I get an error

Undefined index: selectedRow

when I use this:

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">'.
echo '</table>';

echo '<script>';
echo 'function selectedRow(obj) {'.
'var num = obj.parentNode.rowIndex - 1;'.
'alert ("selectedRow: "+num);'.
'window.location.href = "?selectedRow="+ num;'.
echo '</script>';

echo 'selectedRow :'. $_GET['selectedRow'];

I tried also to use AJAX instead of
'window.location.href = "?selectedRow="+ num;'.

'type: "POST",'.
'url: "index.php",'.
'data: "selectedRow=" + num'.


Change your anchor tag href value in while loop with following text :

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

And now replace your window.location.href in script

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

It will work file in your condition.