douxi2670 2016-04-18 13:41
浏览 91

无法使用jQuery调用php函数:ajax

I am new to php and trying to implement a small webpage part of a school project. reportBuffering method in video.js file should call bufferingEventLogger method in server.php using Jquery ajax and store the passed values in Database. I am not able to trigger the bufferingEventLogger function. Here's the code for video.js

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();

});

Below code is for 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);

I have gone through similar questions, but couldn't get it to work. Thanks

  • 写回答

2条回答 默认 最新

  • doupu1957 2016-04-18 13:47
    关注

    The only line that calls bufferingEventLogger is commented out.

    Do you want to only call this function? If so, just uncomment the line.

    If you want to add more functions, you might need to use something like a switch statement.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考