'Pass javaScript variable to php or workaround for just php

I'm trying to get a JavaScript variable to php without refreshing the page or submitting the form. I think my problem is that php is on the server side. I have a button that's set in a php echo...

<input type='button' value='Edit' name='editbtn' onmouseup='reply_click(this.id)' onclick='edit()' id = '" . $row['id'] . "'

This button works correctly. I didn't provide all the code surrounding it, just the part in question. I can inspect the button element in the browser and the "id" shows the correct id number.

Next I'm using some test JavaScript in an effort to get the id to php with having to refresh the page or submit the form.

echo "<script type='text/javascript'>
        function reply_click(clicked_id){
            var selid = clicked_id;
            alert(selid);
            document.writeln(selid);        
        } </script>";

This code works. I get the correct id to show in an alert or document.writeln. The problem comes when I try to set this to a php variable like this...

$testid = "<script type='text/javascript'>document.writeln(selid);</script>";
echo $testid;

$testid returns nothing.

Is there a way to do this or is there a workaround using only php? I need to do this without submitting the form or refreshing the page. Is that possible? The end goal is to create another MySQL statement using that id value. For example: SELECT * FROM table WHERE id = the $testid from JavaScript.

Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source