Passing php variable to javascript function

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
VFDinc
Forum Newbie
Posts: 10
Joined: Sun Oct 23, 2011 10:54 am

Passing php variable to javascript function

Post 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>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Passing php variable to javascript function

Post 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
VFDinc
Forum Newbie
Posts: 10
Joined: Sun Oct 23, 2011 10:54 am

Re: Passing php variable to javascript function

Post 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
VFDinc
Forum Newbie
Posts: 10
Joined: Sun Oct 23, 2011 10:54 am

Re: Passing php variable to javascript function

Post 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>
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Passing php variable to javascript function

Post 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
Post Reply