0

I'm trying to pass a string variable from PHP to Javascript and it is giving me troubles.

I actually pas a number variable as per the code below and it works, but once I introduce (or uncomment) the line var cityname = <?=$city;?>; in my javascript code then it doesn't work. I believe it has something to do with the type of data because if I introduce a number like Scity = 3 in my PHP then it works.

Thank you

I have the following PHP:

<?php
    $get_total_rows = 0;
    $city = "London";

    $db = pg_connect("$db_host $db_name $db_username $db_password");
    $query = "SELECT * FROM table";
    $results = pg_query($query);

    $get_total_rows = pg_numrows($results); 
?>

and I have the 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
user229044
  • 232,980
  • 40
  • 330
  • 338
Sam
  • 143
  • 2
  • 11

1 Answers1

0

In the case of strings, the content must be between quotes (ex.: val = "content"). Try to do something like 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
Marcel Kohls
  • 1,650
  • 16
  • 24