/    Sign up×
Community /Pin to ProfileBookmark

Problem with setting variable as string

so, i get a set of values from my mysql database. i set the a value to a variable called $chatter. i implode the value with the “|” character.

now, later in the code i try to set a javascript variable to a string of the value of $chatter.

[CODE]
[COLOR=”RoyalBlue”]var comments=”<?=$chatter ?>”;
comments=comments.split(‘|’);[/COLOR]
[/CODE]

the value is passed to the comments variable, however for some reason the value is not a string. and all the words like “for” and “in” are being seen as commands. also, the script wont execute completely because it cant split the variable because it thinks its a value rather than a string. :-/

so i dont know what to do… i have two other variables that are set up in the same way, and they split into an array just fine. however, its just this variable that gives me problems…. any suggestions?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@tirnaFeb 06.2011 — [CODE]
var comments="<?php echo $chatter; ?>";
var commentsA = comments.split('|');
[/CODE]
Copy linkTweet thisAlerts:
@NogDogFeb 06.2011 — Maybe there are some quotes in the data messing up the JavaScript syntax? If so, you could try applying addslashes() to the PHP variable.

Or, you might want to consider using a JSON object in your JavaScript, in which case you could use PHP's json_encode() function and not have to worry about imploding/exploding, just pass the PHP array directly into a JavaScript JSON object via json_encode().
×

Success!

Help @aqutenolej 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.2,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

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