Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Passing php variable into JS function
Old 01-23-2008, 05:03 PM Passing php variable into JS function
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
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
 
 
Register now for full access!
Old 01-23-2008, 05:12 PM Re: Passing php variable into JS function
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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; ?>">
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-25-2008, 12:02 PM Re: Passing php variable into JS function
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
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 12:06 PM..
Truly is offline
Reply With Quote
View Public Profile
 
Old 01-26-2008, 03:48 AM Re: Passing php variable into JS function
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
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!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-28-2008, 11:52 AM Re: Passing php variable into JS function
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.77410 seconds with 12 queries