我的页面上有一个按钮:
<button class="play"></button>
当我点击它时,它会通过jQuery启动视频
$('.play').on('click',function(){
player.play();
});
但我还想添加PHP代码,以便在点击该按钮时在数据库中保存用户ID。
我如何混合PHP&amp; jQuery的?我不习惯Ajax,它是解决方案吗?
感谢您的帮助!
答案 0 :(得分:2)
添加功能:
function add_to_db(user_id){
$.post('add.php', {userId: user_id}, function(response){
//do something with the response
r = JSON.parse(response);
if (r.status === 'error'){
//handle the error
alert(r.message);
}
});
});
玩游戏时请执行以下操作
$('.play').on('click',function(){
player.play();
user_id = //however you chose to set this in the page
add_to_db(user_id);
});
你的add.php:
//perform your db update or insert
//if successful:
$response['status'] = 'success';
//else:
$response['status'] = 'error';
$response['message'] = 'update was not successful';
//look into try/catch blocks for more detailed error messaging.
//then send the response back
echo json_encode($response);
答案 1 :(得分:0)
尝试使用ajax
$('.play').on('click',function(){
player.play();
$.ajax({
type: "POST",
url: "some.php", // your php file name
data:{id :"id"}, //pass your user id
success:function(data)
{
if(data==true)
alert("success");
else
alert("error");
}
});
}
<?php
$success=false; //Declare and initialize a variable
// do your code to update your database
//and check if the database is updated , if so set $success=true
echo $success;
?>