Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

javascript - Ajax does not send variable to PhP

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...