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

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -