/    Sign up×
Community /Pin to ProfileBookmark

javascript n php integration question

im wondering if it its possible to get data from the database and store it in the javascript variable…i tried doing this :

var x = “<?php echo($data[‘id’]); ?>”;
document.write(“<select> <option> x </option> </select>”);

or even something like this

<script langugae=”JavaScript” type=”text/javascript”>

item = new Array(<?php echo $ctr; ?>);

for(var i=0;i<<?=$ctr?>;i++)
{
item[i] = “<?php
$x = 0;
$_SESSION[‘x’] = $x++;
echo $item[$_SESSION[‘x’]]; ?>”;

}
</script>

but no values would come out can anyone tellme wats wrong? thanks

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@James_GatkaFeb 14.2006 — You can pass a string onload to a hidden input type. You cannot pass an array or "rows" from a query. You'll have to "implode" the array to a string.

[CODE]<?php

$dataStr = "This is some data";

?>

<html>
<head>
<script type="text/javascript">

var phpData = "";

function getData(){

phpData = document.getElementById('dbData').value;
alert(phpData);
}

onload=getData;

</script>
</head>
<body>

<input type='hidden' id='dbData' value="<?php echo $dataStr ?>">


</body>
</html>[/CODE]
×

Success!

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