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 關注
- 131 瀏覽
添加回答
舉報