我正在编写一个代码,允许我在我的数据库中保存用户查看页面的时间,当他点击"关闭"按钮,通过AJAX我在php页面上发布了一些参数,插入到DB(Mysql)中。
这是我的代码:
window.php(我正在浏览的页面)
<?php $start_time = microtime(true);
?>
*page content*
<div align="center"><input type="button" value="Close" onclick="open()"></div>
<script language="javascript" type="text/javascript">
function open()
{
var start = <?php echo $start_time; ?>;
var end = <?php echo $end = microtime(true); ?>;
var uid = <?php echo $_SESSION['uid']; ?>;
var corso = <?php echo $_SESSION['cod']; ?>;
$.ajax({
url: "http://www.mysite.it/template/pages/save.php",
type: "POST",
data: "startt="+start+"&endt="+end+"&uid="+uid+"&corso="+corso,
success: function(){ // trigger when request was successfull
alert("Recorded!");
},
})
}
</script>
然后我的php脚本页面:
save.php
<?php
require "connection.php";
$inizio = $_POST['startt'];
$fine = $_POST['endt'];
$uid = $_POST['uid'];
$corso = $_POST['corso'];
$duration = $fine-$inizio;
$hours = (int)($duration/60/60);
$minutes = (int)($duration/60)-$hours*60;
$seconds = (int)$duration-$hours*60*60-$minutes*60;
$stringa = ($hours .":" .$minutes .":" .$seconds);
$sql = "INSERT INTO visua (utente, corso, tempo)
VALUES ($stringa, $uid, $corso)";
$result = $link->mysql_query($sql);
if ($result) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $link->error;
}
$link->close();
?>
实际上无法理解为什么不工作,即使&#34;成功&#34; AJAX的一部分显示消息,但DB上没有新行
谢谢你的建议。
答案 0 :(得分:1)
<script language="javascript" type="text/javascript">
function open()
{
var start = '<?php echo $start_time; ?>';
var end = '<?php echo $end = microtime(true); ?>';
var uid = '<?php echo $_SESSION['uid']; ?>';
var corso = '<?php echo $_SESSION['cod']; ?>';
$.ajax({
url: "http://www.mysite.it/template/pages/save.php",
type: "POST",
data: "startt="+start+"&endt="+end+"&uid="+uid+"&corso="+corso,
success: function(){ // trigger when request was successfull
alert("Recorded!");
},
})
}
</script>
答案 1 :(得分:0)
<?php
require "connection.php";
$inizio = $_POST['startt'];
$fine = $_POST['endt'];
$uid = $_POST['uid'];
$corso = $_POST['corso'];
$duration = $fine-$inizio;
$hours = (int)($duration/60/60);
$minutes = (int)($duration/60)-$hours*60;
$seconds = (int)$duration-$hours*60*60-$minutes*60;
$stringa = ($hours .":" .$minutes .":" .$seconds);
$sql = "INSERT INTO visua (utente, corso, tempo)
VALUES ('$stringa', '$uid', '$corso')";
$result = $link->mysql_query($sql);
if ($result) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $link->error;
}
$link->close();
?>
可能会起作用....在此之前你需要检查ajax值或传递或不传...
答案 2 :(得分:0)
试试这段代码,
<script type="text/javascript">
function open(){
var start = '<?php echo $start_time; ?>';
var end = '<?php echo $end = microtime(true); ?>';
var uid = '<?php echo $_SESSION['uid']; ?>';
var corso = '<?php echo $_SESSION['cod']; ?>';
$.ajax({
url: "http://www.mysite.it/template/pages/save.php",
type: "POST",
data: {
startt : start,
endt : end,
uid : uid,
corso : corso
},
success: function(){ // trigger when request was successfull
alert("Recorded!");
}
});
}
</script>
此外,这是一个可以帮助您的逻辑问题。如果您需要计算用户关闭页面的结束时间,请在 save.php 文件中计算结束时间,
<?php
$fine = microtime(true);
//Also try including single-quote in your query for insert
$sql = "INSERT INTO visua (utente, corso, tempo) VALUES ('$stringa', '$uid', '$corso')";
?>
这将为您提供预期的结果。
希望这有帮助!!!