我正在尝试创建一个用户界面,它会自动将数据插入数据库。这个数据还应该包括鼠标坐标,因为我想在那个地方的世界地图上创建一个图钉。
问题是我不知道如何将鼠标坐标输入到 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) );
});
});