I'm quite new to AJAX, but I want to save my javascript-calculated coordinates to my database via php. It does not seem to work. I tried changing it to the short $.post
variant instead but that also did not work.. when I open posts.php, the variable $lat
does not seem to exist(the insides of the if-function don't get called).
This is in my index.php
let lat;
let lng;
navigator.geolocation.getCurrentPosition(showPosition);
function showPosition(position) {
lat = position.coords.latitude;
lng = position.coords.longitude;
alert (lat);
$.ajax(
{
type: "POST",
url: 'php/posts.php',
data: { LatID : lat },
success: function(data)
{
alert("success!");
}
});
}
And the following is in posts.php:
<?php
if(isset($_POST['LatID']))
{
$lat = $_POST['LatID'];
$addinfo = strip_tags($_POST["detailthing"]);
$titel = strip_tags($_POST["titel"]);
$type = $_POST["welke"];
$lng = $_POST["lng"];
session_start();
include("opendb.php");
echo $lat;
echo $lng;
$stmt = $db->prepare('INSERT INTO requests (type, user_id, addinfo, title, lat, lng) VALUES ( ? , ? , ? , ? , ? , ? )');
$stmt->bindValue(1, $type, PDO::PARAM_STR);
$stmt->bindValue(2, $_SESSION["user_id"], PDO::PARAM_STR);
$stmt->bindValue(3, $addinfo, PDO::PARAM_STR);
$stmt->bindValue(4, $titel, PDO::PARAM_STR);
$stmt->bindValue(5, $lat, PDO::PARAM_STR);
$stmt->bindValue(6, $lng, PDO::PARAM_STR);
$stmt->execute();
}
header("refresh: 9;url=https://agile130.science.uva.nl/#linkposts");
?>
question from:
https://stackoverflow.com/questions/65926355/ajax-does-not-send-variable-to-php 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…