Want javascript variable as index variable in php array in javascript.
Code is not working .....
<script>
var total = <?php echo count($resvalue); ?> ; //$resvalue is an array holding the values are 1,2,3,4. total will hold count of that array.
for(var j=0;j<total;j++)
{
<php echo $resvalue[j]; ?> // where j is javascript variable and $resvalue is PHP array
}
</script>
1 Answer 1
You cannot read values in a php array from javascript. Instead echo the array and turn it into a javascript array and let javascript do the count.
<script>
var resvalue = [<?php echo $resvalue; ?>]; // or something like this
for(var j=0; j < resvalue.length; j++)
{
// your value is available in the js-array
// resvalue[j]
}
</script>
answered Aug 9, 2017 at 6:26
Eric Herlitz
26.4k28 gold badges117 silver badges167 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
$resvalueto the client using API call, or by using full php code<php echo...should be<?php echo...you also have another error in your for loop. it should betotal.lengthand nottotal...