我試圖創建一個用戶界面,可自動將數據插入到數據庫中。該數據還應該包括鼠標座標,因爲我想在那個地方的世界地圖,以創建一個PIN。如何讓JS鼠標座標到一個PHP插入查詢?
的問題是我不知道如何讓鼠標座標轉換成PHP插入查詢。可以幫我嗎?
PHP
<body>
<h1>Add Project to the database</h1>
<div class="left_column">
<svg version="1.1" xmlns="//www.w3.org/2000/svg" viewBox="0 0 400 620" xmlns:xlink="//www.w3.org/1999/xlink">
//country paths ...
</svg>
</div>
<?php
if(isset($_POST['submit']))
{
$username = "root";
$password = "clacs_2013";
$hostname = "localhost";
$conn = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
print_r("INSERT INTO projects VALUES ('".$_POST['project_ID']."', '".$_POST['project_name']."', '".$_POST['project_desc']."', '".$_POST['leftCoord']."', '".$_POST['topCoord']."')");
$insertProject= mysql_query("INSERT INTO projects VALUES ('".$_POST['project_ID']."', '".$_POST['project_name']."', '".$_POST['project_desc']."', '".$_POST['leftCoord']."', '".$_POST['topCoord']."');")
or die(mysql_error());
mysql_select_db("clacs") or die(mysql_error());
$retval = mysql_query($insertProject, $conn);
if(! $retval)
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
project ID: <input type="text" name="project_ID"><br/>
project name: <input type="text" name="project_name"><br/>
project description: <input type="text" name="project_desc"><br/>
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
?>
</body>
JS
$(document).ready(function(){
$(".country").click(function(e) {
$("form").css("display", "block");
var offset_t = $(this).offset().top - $(window).scrollTop();
var offset_l = $(this).offset().left - $(window).scrollLeft();
var leftCoord = Math.round((e.clientX - offset_l));
var topCoord = Math.round((e.clientY - offset_t));
});
});
當然喲你可以使用Javascript來做到這一點;但如果你無論如何提交表單,你可以考慮使用''(它提交鼠標點擊的座標)。 – eggyal