1

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>
1
  • 1
    <?php echo $result['image_url']; ?> Commented Nov 20, 2012 at 7:18

5 Answers 5

7

The easiest way would be to put a little PHP script inside the Javascript, like this...

<script type="text/javascript">
function bla() {
    var thevar = "<?php echo $thevar; ?>";
}
</script>

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']; ?>";}

Sign up to request clarification or add additional context in comments.

2 Comments

That is far from the best way (IMO), but +1 for a correct answer.
This would be a good answer if code was right! You should put echo inside a string, if the var isn't a number, app crashes!
1

You have to use

 json_encode

 <?php
  $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
  echo $t=json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5}

 ?>

Now $t you can pass in js function

In js code:

<script type="text/javascript">
function you_fun_nm() {
    var val = <?php echo $t; ?>
     alert(val);
}

Comments

1

Try by writing php varible in javascript is as follows

{element.src="'<?php echo $result['image_url']; ?>'";}

Comments

1

Replace your line with this one:

element.src = "<?= $result['image_url'] ?>";

Or you can set a javascript var and call it:

var imageUrl = "<?= $result['image_url'] ?>";

// ...

element.src = imageUrl;

Comments

1
  1. You have to modify your Javascript function, below is modified version

    function changeImage(image_from_db)
    {
        element=document.getElementById('pokemon_card')
    
        if 
        (element.src == "http://dmisasi.files.wordpress.com/2010/12/david-pokemon-card-back.jpg?w=750")
        {
            element.src = image_from_db;} //<- image_from_db is being passed by you PHP script
        else
        {
            element.src="http://dmisasi.files.wordpress.com/2010/12/david-pokemon-card-back.jpg?w=750";
        }
    }
    
  2. Then call above function on your image tag like this

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.