0

I am trying to parse an int value from php into a javascript variable but it is not working.

The first alert correctly displays the int value, but the second says "undefined". How can you parse the php variable correctly into the variable "test" as an integer? I tried parseInt but it didn't work. The value is for a graph so I need to at some point use the fillRect() command, the alerts are for testing only. Thanks.

    alert('<?php echo( $graph_max_max_value ); ?>');
    var test = <?php $graph_max_max_value; ?>
    alert(test);
1
  • Try var test = <?php echo $graph_max_max_value; ?> Commented Oct 8, 2012 at 10:00

4 Answers 4

3

var test = <?php echo $graph_max_max_value; ?>; should work.

This outputs using echo, which you forgot, and adds the statement terminating semi-colon at the end.

Sign up to request clarification or add additional context in comments.

4 Comments

@user1334130, make sure this value is supposed to be an integer. If it's not, use this : var test = "<?php echo $graph_max_max_value; ?>";
Thanks Grant. Now it works perfectly. :) Will accept the answer as soon as I can.
@TWCrap yes but the answer from Thomas was more helpful.
@user1334130, it's your call, and if you find that, then you should do that ;-)
2

You forgot to do anything to output the variable.

You want an echo, print, printf, etc.

(You also need to make sure that the data in PHP conforms to the syntax for a Number type in JavaScript.)

Comments

1

Try this

var test = <?php echo $graph_max_max_value; ?>,

and then

alert(test);

If you dont use echo then the value is undefined

Comments

0

You forgot an echo and you need to make sure you pass an integer value to your variable.

<script>
    <?php $graph_max_max_value = 1024;?>
    alert('<?php echo( $graph_max_max_value ); ?>');
    var test = '<?php echo $graph_max_max_value;?>';
    test.replace(/[^0-9]/g, '');
    alert(test);
</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.