Reply
Passing php variable into JS function
Old 01-23-2008, 04:03 PM Passing php variable into JS function
Extreme Talker

Posts: 214
Hey guys. Like the title says I am trying to pass a php variable into a JS function.

This is what I have at the moment:

Code:
	function highlight(name)
		{
		document.name.src ="\images\rating\star2.gif";
		}	

<img src="\images\rating\star1.gif" onmouseover="highlight('<?php echo $count; ?>')" name="<?php echo $count; ?>">
When I hover the mouse over I get the error that: document.name is null or not an object.

Any suggestions?
Truly is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 01-23-2008, 04:12 PM Re: Passing php variable into JS function
tripy's Avatar
Fetchez la vache!

Posts: 1,987
Name: Thierry
Location: In the void
What you do to pass from php to js is right, but what are you trying to do ?

There is no document.name property, and that's your error....
You are passing a "name" variable to your function, so reference it.
Your problem is to find the reference to the image.

For that, I either recommand you to put an id rather than a name on the image, and to use documnet.getElementById('the_id'); to find it back
Code:
function highlight(_id){
  var trg=document.getElementById(_id);
  trg.src ="\images\rating\star2.gif";
}
<img src="\images\rating\star1.gif" onmouseover="highlight('<?php echo $count; ?>')" id="<?php echo $count; ?>">
Or to put a reference on the image directly on the function call:
Code:
function highlight(trg){
  trg.src ="\images\rating\star2.gif";
}
<img src="\images\rating\star1.gif" onmouseover="highlight(this)" id="<?php echo $count; ?>">
Here, this reference to img from whose the onmouseover event trigerred, so no need to search it again.
I personally prefer to use this syntax, rather than to search for an id.

Extend the call to include every variables that you might need:
Code:
function highlight(_value, _elm){
  _elm.src ="\images\rating\"+_value+".gif";
}
<img src="\images\rating\star1.gif" onmouseover="highlight('<?php echo $count; ?>', this)" id="<?php echo $count; ?>">
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 01-25-2008, 11:02 AM Re: Passing php variable into JS function
Extreme Talker

Posts: 214
I am just trying to make a Star rating system and I have no JS experience :P, but I figured I would just dive in.

Anyways I am trying to have it switch to a star on mous over and then on mouse out I will have it change back to an empty star.

But thanks for the pointer I will give it a try and see where I end up

Second question for you though first, If in the head I put a JS function and then within the JS function I put PHP code, if I call the JS function somewhere in the body will it execute the PHP code then?

Last edited by Truly : 01-25-2008 at 11:06 AM.
Truly is offline
Reply With Quote
View Public Profile
 
Old 01-26-2008, 02:48 AM Re: Passing php variable into JS function
chrishirst's Avatar
Super Moderator

Posts: 12,811
Location: Blackpool. UK
Quote:
If in the head I put a JS function and then within the JS function I put PHP code, if I call the JS function somewhere in the body will it execute the PHP code then?
Nope the PHP has run and finished before the page gets to the browser.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-28-2008, 10:52 AM Re: Passing php variable into JS function
Extreme Talker

Posts: 214
Thanks. Ya chris I figured that was the case, just thought that would be cool if that did work.
Truly is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Passing php variable into JS function
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12409 seconds with 13 queries