创建函数可增加每次有人单击按钮时称为clicks的会话值,php可以正常工作,但无法通过AJAX调用它。
jQuery:
$("button").click(function() {
$.ajax({
url: 'resources/ajax/addclick.php',
success: function(output) {
alert(output);
}
});
});
php:addclicks.php
<?php
require_once("../config.php");
function click(){
$_SESSION['clicks'] ++;
}
?>
php:config.php
session_id();
session_start();
$id = session_id();
if (!isset($_SESSION['started'])) {
date_default_timezone_set("Asia/Tokyo");
$_SESSION['started'] = date("h:i:s a");
$_SESSION['date'] = date("Y/m/d");
}
if (!isset($_SESSION['clicks'])) {
$_SESSION['clicks'] = 0;
}
html按钮:
<form class="button" method="post">
<button class="button" id="button" type="submit">Click
me</button>
</form>
答案 0 :(得分:1)
$("button").click(function(){
$.ajax({
method: "POST",
url: "addclick.php",
data: { add: 1 }
})
.done(function( msg ) {
alert( msg );
});
});
您的php最像这样
<?php
$_SESSION['clicks'] = $_SESSION['clicks'] + $_POST['add'];
echo $_SESSION['clicks'];
?>
您的html不需要表单标签,至少您想阻止提交
<button class="button" id="button" type="submit">Click
me</button>