-1
我正在嘗試使用JavaScript變量並將其放入mySQL數據庫中。將javascript值分配給PHP變量
我想設置php變量$緯度與javascript position.coords.latitude的值,並把它放在我的數據庫中。
<button onclick="getLocation()">Try It</button>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
<?php
$latitude = 'position.coords.latitude';
$longitude = 'position.coords.longitude';
// Create connection
$conn = mysqli_connect("localhost", "root", "", "site");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO annonce (longitude, latitude) VALUES ('$longitude', '$latitude')";
if (mysqli_query($conn, $sql))
{
\t header('Location: member.php');
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
?>
\t
}
如果您希望將javascript變量保存到數據庫中,您需要通過表單或AJAX提交它們。 – slapyo
請不要在您的問題中傾銷所有代碼。只發布相關的片段。 –