/    Sign up×
Community /Pin to ProfileBookmark

Getting a string from a php variable or mysql database into javascript variable

Ok I have a chat code, a live chat o_O written with basically js
Not by me, my friend wrote it for me, but im starting to understand it a bit ?

Now, as my Title says, I am needing to store a PHP string or get a string from my database and store it into my javascript variable

so when you type “blah” it’ll show

<yourusername>: blah

instead of <time stamp> blah
or just plain
blah
so im wondering how to do this
Im pretty sure JS cant communicate with MySQL, but then again im a noob?

my PHP variable would be “username” i’d just get it from the database
if not possible is there anyway to use PHP to put it somewhere which javascript can also interact with, say, a cookie?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisDec 29.2006 — [code=php]<?php
$row = mysql_fetch_assoc($result);
$username = $row['username'];
?>
<script type="text/javascript">
var username = "<?php echo $username; ?>";
</script>[/code]
Copy linkTweet thisAlerts:
@Mythic_Fr0stauthorDec 29.2006 — Wow didnt do you could do that Thanks

btw, whats assoc? I use mysql_fetch_array?
Copy linkTweet thisAlerts:
@so_is_thisDec 29.2006 — It is for an associative array. The difference is whether you have to address the array elements by a numeric index or whether you can address the array elements by the name of the database column from your query.
Copy linkTweet thisAlerts:
@Mythic_Fr0stauthorDec 29.2006 — oh, its not an array O_O just a string ^_^ - Thanks
Copy linkTweet thisAlerts:
@so_is_thisDec 29.2006 — Huh? The output from [B]mysql_fetch_array()[/B] and [B]mysql_fetch_assoc()[/B] are both arrays.

The first is an ordinal array and the second is an associative array.
×

Success!

Help @Mythic_Fr0st 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,
)...