0

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: $_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({'.
                 'type: "POST",'.
                 'url: "index.php",'.
                 'data: "selectedRow=" + num'.
             '});'.
4
  • Try to separate the html and php code. It will be more easier to debug and read. Commented Jul 22, 2016 at 23:53
  • tired. all the data at the page is from SQL at the PHP. Commented Jul 22, 2016 at 23:57
  • You are never sending the JS variable to PHP. Commented Jul 23, 2016 at 0:00
  • so where is my mstake? Commented Jul 23, 2016 at 0:05

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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">'. from the table.

I add the editRow=true to the JS function with selectedRow like this: '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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.