0

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' });

});

3 Answers 3

4

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>
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

4

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.