How you treat in JavaScript a data from PHP

1

I have a table where a cell contains an image, which when clicked will execute a function responsible for opening a box dialog with the image in big.

<td style="width: 20%;"><?php
$source = $row['T_IMG'];
echo "<img width='50px' height='50px' src=".$source." onclick='maximizeImg()'> "; 
?></td>

My question is, if I send maximizeImg(".$source.") for the function to receive the appropriate image of each row, in the function:

function maximizeImg(){
                var table = document.getElementById('tableRelatedTest');
                var rCount = table.rows.length;;
                //alert(table.rows[rCount - 1].cells[14].innerHTML);
                var sourceImg = table.rows[rCount - 1].cells[14].innerHTML;
                document.getElementById("imageMax").src = sourceImg;
                $(function () {
                            $("#dialog1").dialog();
                            $("#dialog1").show();
                        });
                        return;
            }

How do I pick up or treat that String in the function so that the image of each row is assigned correctly, that way I could remove the rCount-1 I have indicated.

    
asked by Diego Anton Inelmatic Electron 08.11.2017 в 07:36
source

1 answer

3

It is not necessary to send the source that you get from PHP to access the image, for this example it would be enough to pass the reserved word this . So that later from JavaScript access to that element that will arrive as parameter (img) for the example

PHP

echo "<img width='50px' height='50px' src=".$source." onclick='maximizeImg(this)'> ";

JS

function maximizeImg(img){
  document.getElementById("imageMax").src = img.src;
}
    
answered by 08.11.2017 / 07:58
source