How to use a wpdb query to plot markers on Google Maps API?
-
[ Moderator note: moved to How-to and Troubleshooting. ]
Hi there.
I’m trying to plot markers on a map using a query that outputs a custom meta field of wpdb.
I tried to assign the query to a variable and pass it to the Javascript through AJAX, however I failed to do so, in fact I did not understand how AJAX works on WordPress.
I have tried to modify the code of this tutorial: https://byronyasgur.wordpress.com/2011/06/27/frontend-forward-facing-ajax-in-wordpress/ like this:
[ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]
<?php
/*
Plugin Name: Hello World Ajax Frontend 2
Plugin URI: https://rainrain.com
Description: A simplified ajax front end
Version: 2
Author: Bob Murphy
Author URI: https://rainrain.com
*/// enqueue and localise scripts
wp_enqueue_script( 'my-ajax-handle', plugin_dir_url( __FILE__ ) . 'ajax.js', array( 'jquery' ) );
wp_localize_script( 'my-ajax-handle', 'the_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
// THE AJAX ADD ACTIONS
add_action( 'wp_ajax_the_ajax_hook', 'the_action_function' );
add_action( 'wp_ajax_nopriv_the_ajax_hook', 'the_action_function' );
// THE FUNCTION
function the_action_function(){global $wpdb;
$name = $_GET['name'];
// Query on the WPDB to get the latlng
$latlng = $mylink = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_id = 37" );echo"This: " . $latlng->meta_value;
die();
}
// ADD EG A FORM TO THE PAGE
function hello_world_ajax_frontend(){
$the_form = '
<form id="theForm">
<input id="name" name="name" value = "name" type="text" />
<input name="action" type="hidden" value="the_ajax_hook" />
<input id="submit_button" value = "Click This" type="button" onClick="submit_me();" />
</form>
<div id="response_area">
This is where we\'ll get the response
</div>';
return $the_form;
}
add_shortcode("hw_ajax_frontend", "hello_world_ajax_frontend");
?>
And this is the JS:jQuery.post(the_ajax_script.ajaxurl, { 'action': 'the_ajax_hook' } ,
function(response_from_the_action_function){
jQuery("#response_area").html(response_from_the_action_function);
}
);Can someone give me a light on this matter? I hit a wall here =P
- The topic ‘How to use a wpdb query to plot markers on Google Maps API?’ is closed to new replies.