I look up the image associated with a pokemon and display it with php. Then I want to be able to “flip the card over,” by clicking on it. I’ve got the first click down, but the second click to flip the card back over isn’t working. I figure it’s the syntax of my php variable within the JS:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>
'Murica!
</title>
<script>
function changeImage()
{
element=document.getElementById('pokemon_card')
if
(element.src.match("http://dmisasi.files.wordpress.com/2010/12/david-pokemon-card- back.jpg?w=750"))
{element.src="'.$result['image_url'].'";} //<- no idea how to express the php string variable here
else
{element.src="http://dmisasi.files.wordpress.com/2010/12/david-pokemon-card-back.jpg? w=750";}
}
</script>
</head>
<body>
<?php
$dbhost = 'databasePlace';
$dbname = 'mine';
$dbuser = 'me';
$dbpass = '******';
$link = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
mysqli_select_db($link,$dbname);
$name = $_GET["fname"];
$query = sprintf("SELECT image_url, Type
FROM Pokemon c
WHERE c.name='%s'",
mysqli_real_escape_string($link,$name));
$result = mysqli_fetch_assoc(mysqli_query($link,$query));
echo '<img id="pokemon_card" onclick="changeImage()" height="225"
width="165" src="'.$result['image_url'].'"/>';
mysqli_close($link);
?>
</body>
</html>
The easiest way would be to put a little PHP script inside the Javascript, like this…
In other words, according to your question, you would replace the line
{element.src="'.$result['image_url'].'";}with the line{element.src="<?php echo $result['image_url']; ?>";}