我是一名PHP新手,嘗試實現學校項目的小型網頁部分。 video.js文件中的reportBuffering方法應該使用Jquery ajax調用server.php中的bufferingEventLogger方法,並將傳入的值存儲在數據庫中。我無法觸發bufferingEventLogger函數。這裏是video.js的代碼無法使用jQuery調用php函數:ajax
var video;
$(document).ready(function(){
var timeBuffered = 0;
var timer;
video = $('#main-video')[0];
video.play();
$(video).on("play",function(){
if(timer!=null)
clearInterval(incrementBufferedTime);
});
$(video).on("waiting",function(){
timer = setInterval(function(){
incrementBufferedTime
},1000);
});
function incrementBufferedTime(){
timeBuffered++;
console.log("Buffered time"+timeBuffered);
if(timeBuffered > 5){
reportBuffering()
}
}
function reportBuffering(){
$.ajax({
method: "POST",
url: "server.php",
data:{functionId:'bufferingEventLogger',val0:1,val1:'2016-04-18 16:37:01',val2:'2016-04-18 16:37:02'},
success:function (response) {
console.log(response)
}
});
}
reportBuffering();
});
下面的代碼是server.php
$id=$_POST['val0'];
$bufferStartTime=$_POST['val1'];
$bufferEndTime=$_POST['val2'];
//bufferingEventLogger($id,$bufferStartTime,$bufferEndTime);
function bufferingEventLogger($id,$bufferStartTime,$bufferEndTime){
$conn = mysqli_connect("localhost", "root", "", "metrics");
$sql = "INSERT INTO buffer_time(id,buffer_start,buffer_end) VALUES $id,'$bufferStartTime','$bufferEndTime')";
echo 'entered';
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
mysqli_close($link);
我有過類似的問題了,但不能讓它開始工作。謝謝