-1

I have a script showing below

 for (var i = 0; i < checkboxes.length; i++) {
        var checkbox = checkboxes[i];
        checkbox.onclick = function() {
            var currentRow = this.parentNode.parentNode;
            var secondColumn = currentRow.getElementsByTagName("td")[1];

            alert("My text is: " + secondColumn.textContent );
        };
    } 

SO I have a variable secondcolumn I want value of this variable in php... How can I do this... I have tried this in php:

  1. echo $id= secondColumn.value;

  2. echo "<script>alert('".$id."');</script>";

but not work anymore... Please help

APURV PATEL
  • 107
  • 1
  • 3
  • 15

3 Answers3

0

There's a few ways to do this, but the easiest I've found is to set some hidden value on the page/element to whatever you want to use, and then bring it into javascript.

So, on your HTML, for example:

<td data-id="<?php echo $id; ?>"

Then, in javascript:

 for (var i = 0; i < checkboxes.length; i++) {
        var checkbox = checkboxes[i];
        checkbox.onclick = function() {
            var currentRow = this.parentNode.parentNode;
            var secondColumn = currentRow.getElementsByTagName("td")[1];

            var el = document.querySelectorAll('td'); // You need to be more specific here....
            var your_value = el.getAttribute('data-id');

            alert("My text is: " + secondColumn.textContent );
        };
    } 
Nate Ritter
  • 2,386
  • 3
  • 20
  • 28
0

you can not append the javascript value to php variable
but you can call javascript variable just like that

echo "<script>alert(secondColumn);</script>";
hassan
  • 7,812
  • 2
  • 25
  • 36
0

you have to use ajax for this:

because the main reason is php it self.it will be executed before even your document is loaded so you can not assign a javascript variale to a php but the reverse is always possible.

read here about ajax:

http://api.jquery.com/jquery.ajax/

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44