How to pass a String variable from PHP to Javascript -
this question has answer here:
- how pass variables , data php javascript? 17 answers
i'm trying pass string variable php javascript , giving me troubles.
i pas number variable per code below , works, once introduce (or uncomment) line var cityname = <?=$city;?>;
in javascript code doesn't work. believe has type of data because if introduce number scity = 3
in php works.
thank you
i have following php:
<?php $get_total_rows = 0; $city = "london"; $db = pg_connect("$db_host $db_name $db_username $db_password"); $query = "select * table"; $results = pg_query($query); $get_total_rows = pg_numrows($results); ?>
and have following javascript:
<script type="text/javascript"> $(document).ready(function() { var track_load = 0; //total loaded record group(s) //var cityname = <?=$city;?>; var total_rows = <?=$get_total_rows;?>; $('#results').load("autoload_process.php", {'rows':total_rows}, function() {track_load++;}); //load first group
in case of strings, content must between quotes (ex.: val = "content"). try this:
var track_load = 0; //total loaded record group(s) var cityname = "<?php echo $city; ?>"; // string var total_rows = <?php echo $get_total_rows;?>; // number
Comments
Post a Comment