Σπύρος Γούλας Σπύρος Γούλας - 1 month ago 8
Javascript Question

PHP/JS/SQL - Alerting a query result when clicking on an image does not always work

Inside a php file i construct a table that i then echo and it shows the results of a query. One of the columns contains cells where i have an image. What i want to do is 1)when i hover over the image to see the string stored in the variable and 2)when i click on the image to see the same string in an alert box.

My code for that is the following

echo "<td href='#' onClick=alert('".$ResultTable[$i][10]."'); style='text-align: center;'><img src='IconImage.png' width='50' height='50' title='".$ResultTable[$i][10]."'/></td>";


My problem is that the alert box does not always work.

Some more info: The

$ResultTable[$i][10]


Contains a string of size

Notes varchar(255) DEFAULT '---',


If the user did not specify that input, i actually insert a small 4 letter word in greek instead of letting the sql insert the default.

My problem is that if i give as input small words in greek or let the php insert the 4 letter greek word, the alert box works as intended. If i give bigger inputs in greek, it does not work at all. When it comes to english, it does not work at all, no matter the length of the input.

Note that each time i insert or search in the database i use

mysqli_query($con,"SET NAMES 'utf8'");
mysqli_query($con,"SET CHARACTER SET 'utf8'");


This is my first post here, so please bear with me if the question's format is messed up or i provided insufficient info. I will provide any info requested. Thank you very much!

Answer
onclick=\"alert('.....'\"

You need to wrap your onclick element inside a string, since you are manually outputting this you'll need to use the backslash to cancel it.

Or you can do this much more readable example:

<tr onclick="alert('?<=$varHere;?>')">

Using the shorthand PHP will basically output the result of whats between the delimiters meaning you do not need to add echo

Comments