3 回答

TA贡献1829条经验 获得超9个赞
如果您真的想使用 ajax 调用来计算数字,这里是示例:
计数器.php:
<?php
$counter = $_GET['current_value'] + 1; //value is increase here
echo json_encode(['new_value' => $counter]); //return the value in JSON format
?>
您的 HTML 文件:
<span class="points-count">0</span>
<button onclick="count()">Increase</button>
<script type="text/javascript">
function count() {
$.ajax({
url : "counter.php",
type : "GET",
dataType: "json",
data : {
current_value: $(".points-count").html() //set value here
},
success: function(data) {
$(".points-count").html(data.new_value); //finally, get the returned value of the php script
}
});
}
</script>

TA贡献1818条经验 获得超8个赞
您可以使用jQuery、javascript等来完成此操作,如果您只想使用PHP,请检查此代码
<?php
$current_value = 0;
if($_POST['count']){
$current_value = $_POST['count'];
$current_value++;}
?>
<form action="" method="POST">
<span class="points-count"><?=$current_value?></span>
<input type="hidden" name="count" value="<?=$current_value?>">
<button type="submit">Add</button>
</form>
- 3 回答
- 0 关注
- 113 浏览
添加回答
举报