Page 1 of 1

Passing php variable to javascript function

Posted: Fri Nov 11, 2011 3:23 am
by VFDinc
I am trying to load an image by setting a php variable $image = "0005.jpg"
on click of the button this function is called login('$image')
I have a alert() to tell me if something was passed to it. my way of debugging.
I am still very new to php, and javascript, but having fun learning.

Code: Select all

<html>
<head>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
function login(t) {
alert(t);
document.getElementById("imageHolder").innerHTML="<img src= 't' alt=''/>";
}
-->
</SCRIPT>
</head>
<body> 
<?php

$image = "0005.jpg";
print "<input type='button' name='button' value='button' onClick=login('$image')>";
?>
<div id="imageHolder">this is where the image will show</div>
</body>
</html>

Re: Passing php variable to javascript function

Posted: Fri Nov 11, 2011 8:21 pm
by twinedev
Javascript doesn't let you include variables inside double quotes like PHP does:

Code: Select all

document.getElementById("imageHolder").innerHTML="<img src= '"+t+"' alt=''/>";
-Greg

Re: Passing php variable to javascript function

Posted: Fri Nov 11, 2011 10:26 pm
by VFDinc
It works perfectly, I did a lot of searching about passing php values to javascript function, and nothing showed what you have shown. Can you explain why it works? Thanks for the help!

Eric

Re: Passing php variable to javascript function

Posted: Sat Nov 12, 2011 1:54 am
by VFDinc
So it works, but when I went to apply directly to my game, the above was a simple example of loading an image. I want to pass $id to loadimage() when an opponent is selected from a dropdown list:
}
$id= "0005.jpg";
?>
<strong>Select Opponent:</strong>
<select id='opponent' name='opponent' onChange='select_opponent();loadimage('$id')'>
<OPTION VALUE=0>Choose your opponent
<?=$options?>
</SELECT>
---------------------------------------------------
while ($row=mysql_fetch_array($result)) {

$Class=$row["Class"];
$Name=$row["Name"];
$Level=$row["Level"];
//$OppStats = "Name:".$Name ." Class: ". $Class ." Level: ". $Level;
$OppStats = "Name:{$Name}\nClass: {$Class}\nLevel: {$Level}";
$options.="<OPTION VALUE=\"$OppStats\">".$Name;
}
$id= "0005.jpg";
?>
<strong>Select Opponent:</strong>
<select id='opponent' name='opponent' onChange='select_opponent();loadimage('$id')'>
<OPTION VALUE=0>Choose your opponent
<?=$options?>
</SELECT>

Re: Passing php variable to javascript function

Posted: Sat Nov 12, 2011 5:20 am
by twinedev
I think you are geting confused to as when you are in PHP and when you are not, Both lines that call loadImage() are NOT in PHP code, so you are passing to loadImage the actual string $id. You would need to do something like loadImage('<?php echo $id; ?>');

As to the earlier question, the reason it worked is you are concatenating the string <img src= ' with the contents of the variable t and then with the string ' alt=''/>
(in php you would do "string1" . $variable . "string2" where in JS, you do "string1" + variable + "string2"

-Greg