Reputation: 6199
I am trying to get a variable from the php code and put it in jquery i have tryed this and it does not work can anyone help me?
< script type="text/javascript" src="js/jquery.query-2.1.6.js"></script>
<?
$next_exp = 123;
?>
$(document).ready(function() {
var next_exp = $.query.get('next_exp');
$("#pb5").progressBar({ max: next_exp, textFormat: 'fraction',barImage: 'images/progressbg_orange.gif' });
});
Upvotes: 0
Views: 160
Reputation: 4066
You need to set a javascript variable form the PHP variable first:
<?
echo("<script>");
echo("var next_exp=$next_exp");
echo("</script>");
?>
Then you will be able to use it in your Javascript. So your whole example would look like this:
<script type="text/javascript" src="js/jquery.query-2.1.6.js"></script>
<?
echo("<script type='text/javascript'>");
echo("var next_exp=$next_exp");
echo("</script>");
?>
<script type="text/javascript">
$(document).ready(function() {
$("#pb5").progressBar({ max: next_exp, textFormat: 'fraction',barImage: 'images/progressbg_orange.gif' });
</script>
Upvotes: 4
Reputation: 2257
If you need to encode non-scalar values (such as arrays, objects or just about anything), you can use json_encode()
:
<script type="text/javascript">
<?php
$next_exp = array(1, 2, 3);
echo 'var next_exp = ' . json_encode($next_exp);
?>
// next_exp is now a usable JavaScript array: [1, 2, 3]
</script>
Upvotes: 4
Reputation: 88816
Anywhere you want to use the PHP $next_exp, you need to do
<?=$next_exp ?>
if short tags are enabled, or
<?php echo $next_exp; ?>
if short tags are disabled.
Upvotes: 4