Math.random javascripts with php variable -
i use php variable in javascript. have , working fine:
<?php $test = '"test1","test2",test3"'; ?> <script type="text/javascript"> var values = [<?php echo $test; ?>], valuetouse = values[math.floor(math.random() * values.length)]; alert(valuetouse); </script>
however, if use values retrieved database, code not work. both variables contain same values , string type.
<?php $test = 'test1,test2,test3'; ?> <script type="text/javascript"> var values = <?php echo $test; ?>; values=values.split(","); valuetouse = values[math.floor(math.random() * values.length)]; alert(valuetouse); </script>
your problem test variable sting if call javascript split function on string turns , separated list javascript array
Comments
Post a Comment