Jump to content

Help with variable to pass to javascript.


Lassie

Recommended Posts

I am stuck trying to think of how I can use the results from my query to create php variables into a form that I can use, further in my php script or pass to a javascript to displat a map
    If I can pass the vars to javascript what is the best/safe way to do this.
    I am thinking of 
var postcode = "<?php echo $postcode; ?>";

    var latitude = "<?php echo $latitude; ?>";	
    
    var longitude = "<?php echo $longitude; ?>";

This is the query and problem

The results of the echo 

KT17 51.34 -0.24       KT18 51.31 -0.26    KT19  51.35 -0.26

-0.26global $wpdb;
                            $wpdb->show_errors();//debug
                            
                            $result = mysql_query("SELECT postcode,latitude,longitude FROM uk_postcodes WHERE town = '$town'AND county='$county' ");// Get the postcode and lat and long
                        while ( $row = mysql_fetch_assoc( $result ) ) {
                            
                             $postcode=$row['postcode'];
                             $latitude=$row['latitude'];
                             $longitude=$row['longitude'];
                             
                             echo $postcode .'<br/>';
                             echo $latitude .'<br/>';
                             echo $longitude .'<br/>';
                        
                        }
                        //put php variables into the form
                        postcode, latitude, longitude ?
                        
                        //pass to javascript to draw map.
                        
                        var locations = [
      									['KT17', 51.34, -0.24, 4],
      									['KT18', 51.31, -0.26, 5]
      									['KT19', 51.35, -0.26, 5]
      									
    									];	

This is the javascript

 <script type="text/javascript">
    //need var for each 
    var locations = [
      ['KT17', 51.34, -0.24, 4],
      ['KT18', 51.31, -0.26, 5]
      ['KT19', 51.35, -0.26, 5]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(51.35, -0.26),//need var for latling as centre of map
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  </script>			

 

You can use JSON encoding to pass variables from PHP to Javascript.  I prefer a JS Object as apposed to a variable.  

<script>
var coordObj = new Object;
coordObj.postcode = <?php echo htmlspecialchars(json_encode($postcode), ENT_NOQUOTES); ?>;
coordObj.latitude = <?php echo htmlspecialchars(json_encode($latitude), ENT_NOQUOTES); ?>;
coordObj.longitude = <?php echo htmlspecialchars(json_encode($longitude), ENT_NOQUOTES); ?>;
</script>

Then to access your JS object, included later one in the same page render (this is not asynchronous).

<script>
var postcode = coordObj.postcode;
var latitude = coordObj.latitude;
var longitude = coordObj.longitude;
//now do the rest of your stuff
</script>

If I'm understanding your question properly, in WordPress you'll want to take a look at the register_script(), localize_script(), and enqueue_script() functions fired from the wp_enqueue_scripts action hook.

function enqueueStuff(){
	wp_register_script('script_id',$this->getPluginDirectory()."/js/yourJSFile.js");
	wp_localize_script('script_id','script_data',array(
		'postCode'=>$this->getPostCode(),
		'latitude'=>$this->getLatitude(),
		'longitude'=>$this->getLongitude()
	));
	wp_enqueue_script('script_id');
}
add_action('wp_enqueue_scripts','enqueueStuff');

Of course, the pathing to your JavaScript file and the methods getting the post code, latitude, and longitude will be different for you - these are random stubs, but that's the basic idea of it.

Before solving the javascript challenge I need to get my result into a useable format.

Each row has three cols and I need each row with the three cols for example and with vars.


$co-ordinate 1=['KT17', 51.34, -0.24, 4],
$co-ordinate 2=['KT18', 51.31, -0.26, 5]
$co-ordinate 3 =['KT19', 51.35, -0.26, 5]
$co-ordinate 1=['$postcode1', $lat1, $long1, 4],etc
$co-ordinate 2=['$postcode2', 51.31, -0.26, 5]
$co-ordinate 3 =['KT19', 51.35, -0.26, 5]
 

So my problem is how to get to that from this,

Any help appreciated.

$i=0;
                            while($row = mysql_fetch_assoc($result))
                        {
                            $postcode[$i] = $row['postcode'];
                            $latitude[$i] = $row['latitude'];
                            $longitude[$i]= $row['longitude'];

                             $i++;
                             echo $poatcode[$i] .'<br/>';
                             echo$latitude[$i] .'<br/>';
                             echo $longitude[$i] .'<br/>';
                        }

 

Before solving the javascript challenge I need to get my result into a useable format.

Each row has three cols and I need each row with the three cols for example and with vars.

$co-ordinate 1=['KT17', 51.34, -0.24, 4],
$co-ordinate 2=['KT18', 51.31, -0.26, 5]
$co-ordinate 3 =['KT19', 51.35, -0.26, 5]
$co-ordinate 1=['$postcode1', $lat1, $long1, 4],etc
$co-ordinate 2=['$postcode2', 51.31, -0.26, 5]
$co-ordinate 3 =['KT19', 51.35, -0.26, 5]
 

So my problem is how to get to that from this,

Any help appreciated.

$i=0;
                            while($row = mysql_fetch_assoc($result))
                        {
                            $postcode[$i] = $row['postcode'];
                            $latitude[$i] = $row['latitude'];
                            $longitude[$i]= $row['longitude'];

                             $i++;
                             echo $poatcode[$i] .'<br/>';
                             echo$latitude[$i] .'<br/>';
                             echo $longitude[$i] .'<br/>';
                        }

 

Are you having difficulties with php output for screen or php output for JavaScript?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.