Hi
I have variable,that i've got from database
$example=$playerinfo3 ['level'];
Now i need to use this $example variable in javascript,how could i do that?
How can i insert php variable into javascript?
- vitinho444
- Posts: 2819
- Joined: Mon Mar 21, 2011 4:54 pm
Re: How can i insert php variable into javascript?
I've asked this a long time ago, and I still don't know the answer
What I think it's best to do is to store it in a hidden html element like
Then just use javascript getElementById("var").innerHtml; and it will get the value of the variable.
Not the best solution, but might just work.
What I think it's best to do is to store it in a hidden html element like
Code: Select all
echo '<p id="var" style="display: none:">' . $example . '</p>';
Not the best solution, but might just work.
- KyleMassacre
- Posts: 573
- Joined: Wed Nov 27, 2013 12:42 pm
Re: How can i insert php variable into javascript?
You can do:
JavaScript
PHP
JavaScript
Code: Select all
function myFunction(p1) {
alert(p1 * 30);
}
Code: Select all
$var = 10;
echo "<button onclick='myFunction({$var});'>Click Me!</button>";
Re: How can i insert php variable into javascript?
can this second one be done withouth button,i need like 300 variables to be converted in javascript
Edit:
actually i think i found solution
this works for me
Edit:
actually i think i found solution
Code: Select all
<?php
$var=15; //of course this is simplified as much as it can be
?>
<script type="text/javascript">
var works= '<?php echo $var; ?>'
document.write(works)
</script>
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: How can i insert php variable into javascript?
Yep, you can do just about anything this way. Remember that PHP executes on the server where the webpage, including any Javascript, is generated and then sent to the user, at which point the Javascript is executed. Since they are executed at completely different times in completely different place, you can simply use PHP to write the Javascript you are sending to the client.
The indelible lord of tl;dr