Kelster
Kelster

Reputation: 39

php variables in jquery script

I have these php variables:

$n= $json['health'];
$n2= $json2['health'];

The output are numbers.

Here is the jquery script:

<script>
$(function() {
var valMap = [0, 25, 50, 100, 250, 500];
$("#slider-range").slider({
    min: 1,
    max: valMap.length - 1,
    value: 0,
    slide: function(event, ui) {                        
        $("#amount").val(valMap[ui.value]);                
    }       
});
//$("#amount").val(valMap[ui.value]);
})
</script>

I tried to get the vars in the script as my php variables like that:

var valMap = ['<?php echo($n); echo($n2);?>';];

How can I set the variables in the right way?

Upvotes: 0

Views: 39

Answers (3)

You should try something like that:

var valMap = [<?php echo $n.",".$n2; ?>];

Upvotes: 0

Elias Gomes
Elias Gomes

Reputation: 21

Declare an array with these variables in your PHP code.

$n_arr=array($json['health'],$json2['health']);

Then in your script use that array like this.

var valMap = <?php echo json_encode($n_arr);  ?>;

This should be the tidy way to do it!

Upvotes: 0

benjarobin
benjarobin

Reputation: 4487

You should try something like that:

var valMap = [<?php echo "$n, $n2"; ?>];

Upvotes: 1

Related Questions