Ultimately, my goal is to obtain the latitude and longitude of the current user and then be able to save it into the database.
Before that I need to make an ajax call from jquery and pass these information into the php.
Right now, I simply want to echo any data from jquery. This is a child theme. Here is my current example:
functions.php
function my_enqueue() {
wp_enqueue_script( 'ajax-script', get_stylesheet_directory_uri(). '/js/custom.js', array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
//then
function singletest() {
if (isset($_POST["latitude"]) && !empty($_POST["latitude"])) {
$lat = $_REQUEST["latitude"];
$lon = $_REQUEST["longitude"];
echo '<br>';
echo 'latitude: '. $lat;
echo '<br>';
echo 'longitude: '.$lon;
}
die();
}
add_action ('wp_ajax_singletest', 'singletest'); //logged users
add_action ('wp_ajax_nopriv_singletest', 'singletest'); //for all users
custom.js
function showPositionFirstTime(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
console.log(latitude + ' space ' + longitude);
jQuery.ajax({
url: my_ajax_object.ajax_url,
type: "POST",
data: {
action: 'singletest',
latitude: latitude,
longitude: longitude
},
success: function(response) {
var responseTxt = 'Response for the page ' + response;
console.log('Got this from the server: ' + response);
$('.testing-block').append(responseTxt);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
}
function getLocationFirstTime() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPositionFirstTime);
} else {
$('.cords').text("Geolocation is not supported by this browser.");
}
}
getLocationFirstTime();
display-location.php
echo '<div class="testing-block"></div>';
I am now able to display the content of the console log into the page. I think now I am going to be able to pass the coordinates, establish database connection and insert them.
Thank you everyone for your effort.
Ultimately, my goal is to obtain the latitude and longitude of the current user and then be able to save it into the database.
Before that I need to make an ajax call from jquery and pass these information into the php.
Right now, I simply want to echo any data from jquery. This is a child theme. Here is my current example:
functions.php
function my_enqueue() {
wp_enqueue_script( 'ajax-script', get_stylesheet_directory_uri(). '/js/custom.js', array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
//then
function singletest() {
if (isset($_POST["latitude"]) && !empty($_POST["latitude"])) {
$lat = $_REQUEST["latitude"];
$lon = $_REQUEST["longitude"];
echo '<br>';
echo 'latitude: '. $lat;
echo '<br>';
echo 'longitude: '.$lon;
}
die();
}
add_action ('wp_ajax_singletest', 'singletest'); //logged users
add_action ('wp_ajax_nopriv_singletest', 'singletest'); //for all users
custom.js
function showPositionFirstTime(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
console.log(latitude + ' space ' + longitude);
jQuery.ajax({
url: my_ajax_object.ajax_url,
type: "POST",
data: {
action: 'singletest',
latitude: latitude,
longitude: longitude
},
success: function(response) {
var responseTxt = 'Response for the page ' + response;
console.log('Got this from the server: ' + response);
$('.testing-block').append(responseTxt);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
}
function getLocationFirstTime() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPositionFirstTime);
} else {
$('.cords').text("Geolocation is not supported by this browser.");
}
}
getLocationFirstTime();
display-location.php
echo '<div class="testing-block"></div>';
I am now able to display the content of the console log into the page. I think now I am going to be able to pass the coordinates, establish database connection and insert them.
Thank you everyone for your effort.
The solution to my problem is to:
functions.php
.display-location.php
.jQuery.ajax
in success
function to previously created empty block.I hope this will help someone in the future (final code is inside the edited question itself).
Thank you.
POST
data to the server on one request, then use that data via$_POST
on a new request. Nothing persists betweens requests unless you put it in the database. This isn't like a Node or a Python app that runs 24/7, when a PHP request ends, it exits, each request spins up a new instance from scratch – Tom J Nowell ♦ Commented Oct 30, 2018 at 0:18