/    Sign up×
Community /Pin to ProfileBookmark

Passing PHP variables to javascript function

Hello, I have some PHP code that looks like this:

<?php
for ($index=1; $index<=$numLinks; $index++)
{
echo ‘<div id=”nav”><a href=”” onclick=”SetClientImage(‘ . $client .’)” onmouseover=””><img src=”images/dot_unselected.jpg” border=”0″ alt=”unsel_dot”></a></div>’;
}
?>

The variables $numLinks and $client are passed in from another HTML doc. The javascript code looks like this:

<script type=”text/javaScript”>
<!– hide script from old browsers
function SetClientImage(client)
{
alert (“hello”);
}
// end hiding script from old browsers –>
</script>

In other words, I’m trying to pass a PHP variable to a javascript function. I know this is probably a noob question but I can’t seem to be able to get it to work when I’m sure it should. The HTML part of it seems to display properly but passing $client to the JS function doesn’t seem to work. Can someone give me any ideas as to what’s wrong with my code? TIA.?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJul 19.2009 — What data type is $client? If it's a string I imagine you will be getting a javascript error because you are not quoting it. You can check for javascript errors using firebug btw.

Try this:

[code=php]<?php for ($index=1; $index<=$numLinks; $index++): ?>
<div id="nav">
<a href="" onclick="SetClientImage('<?php echo $client ?>')" onmouseover="">
<img src="images/dot_unselected.jpg" border="0" alt="unsel_dot">
</a>
</div>
<?php endfor ?>[/code]
Copy linkTweet thisAlerts:
@PierceMooreJul 19.2009 — Sorry to bug with my 2 cents, and I am probably wrong, but shouldn't there be a semicolon after the PHP echo statement?

[code=php]onclick="SetClientImage('<?php echo $client; ?>')"[/code]

Just trying to help :-)


Pierce
Copy linkTweet thisAlerts:
@MindzaiJul 19.2009 — No, technically speaking you should not include the semicolon preceding the closing PHP tag as the ?> itself terminates the statement - although including it does not produce any adverse effects that I've ever noticed. However zend recommend you don't do it, so I don't tend do do it in small snippets such as the one above since it looks cleaner.
Copy linkTweet thisAlerts:
@NogDogJul 19.2009 — I've never heard that there's any advantage/disadvantage to the terminal semi-colon (other than saving a keystroke ? ). To understand, though, think of ";" as a command [i]separator[/i], not a command [i]terminator[/i]. Since the last command before a "?>" (or the end of a script) has no succeeding command that needs to be separated, the ";" becomes redundant.
Copy linkTweet thisAlerts:
@PierceMooreJul 19.2009 — Ah, thank you for the clarification, NogDog! I sincerely appreciate it, as I honestly did not know that. I mean, I knew about

[code=php]<?= $var ?>[/code]

Which obviously echoes the statement, however I now understand what you mean :-)


And to OP, I apologize that I do not know what could be the issue with your script, as everything looks perfectly fine. Trust me, if NogDog or Mindzai can't figure it out, there is probably no hope. They are two of the most advanced PHP gurus on this forum! (But do keep at it, regardless :-D )


Pierce
Copy linkTweet thisAlerts:
@moose151authorJul 19.2009 — What data type is $client? If it's a string I imagine you will be getting a javascript error because you are not quoting it. You can check for javascript errors using firebug btw.

Try this:

[code=php]<?php for ($index=1; $index<=$numLinks; $index++): ?>
<div id="nav">
<a href="" onclick="SetClientImage('<?php echo $client ?>')" onmouseover="">
<img src="images/dot_unselected.jpg" border="0" alt="unsel_dot">
</a>
</div>
<?php endfor ?>[/code]
[/QUOTE]


Yes, $client is a string, sorry I forgot to mention that. I tried your solution and it works great. Thanks a lot.?
×

Success!

Help @moose151 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...