gpt4 book ai didi

javascript - 为什么我的保存时间功能不起作用? PHP + AJAX JS

转载 作者:行者123 更新时间:2023-11-29 12:26:30 24 4
gpt4 key购买 nike

我正在编写一个代码,该代码允许我在数据库中保存用户在单击“关闭”按钮时查看页面所花费的时间,通过 AJAX 我在 php 页面上发布一些要插入的参数进入数据库(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 脚本页:

保存.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();

?>

实际上无法理解为什么不起作用,即使 AJAX 的“成功”部分向我显示消息,但数据库上没有新行

谢谢您的建议。

最佳答案

<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>

关于javascript - 为什么我的保存时间功能不起作用? PHP + AJAX JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28229832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com