/    Sign up×
Community /Pin to ProfileBookmark

PHP to Javascript not working :(

For some reason this code to bring a PHP variable to Javascript isn’t working…Can someone show me why??

First, we select the value from SQL and store it in a PHP variable…this part works correct as I tested it just to echo the result.

[code=php]$chrname = “select Charname from $table where 1”;
$chrnme = mysql_query($chrname);
$Charname = mysql_result($chrnme, $Id);[/code]

Then, later on in the code, we try to convert it to Javascript, but it doesn’t work.

[code=html]<script type=”text/javascript”>
var charname = “<?$Charname?>”;
alert(charname);
</script>[/code]

This gives me a blank alert box…why isn’t this working???

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehApr 20.2006 — For maximum portability write your code longhand:[code=php]var charname = "<?php echo $Charname ?>";[/code]
Copy linkTweet thisAlerts:
@tenniskid493authorApr 20.2006 — Thank you!!! That worked.
Copy linkTweet thisAlerts:
@schizoApr 20.2006 — Shorthand code should be alright, you just forgot the =
<i>
</i>var charname = "&lt;?=$Charname?&gt;";
×

Success!

Help @tenniskid493 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 5.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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...