猿问

按下按钮时加一(在 Ajax 调用中)

我有一些标记<span class="points-count">0</span>。按下按钮时,我想在 php.ini 中将此值增加一。我如何实现这一目标?



qq_遁去的一_1
浏览 150回答 3
3回答

PIPIONE

如果您真的想使用 ajax 调用来计算数字,这里是示例:计数器.php:<?php&nbsp; &nbsp; $counter = $_GET['current_value'] + 1;&nbsp; &nbsp; &nbsp; &nbsp;//value is increase here&nbsp; &nbsp; 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">&nbsp; &nbsp; function count() {&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url&nbsp; &nbsp; &nbsp;: "counter.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type&nbsp; &nbsp; : "GET",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: "json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data&nbsp; &nbsp; : {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_value: $(".points-count").html() //set value here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(".points-count").html(data.new_value); //finally, get the returned value of the php script&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }</script>

弑天下

您可以使用jQuery、javascript等来完成此操作,如果您只想使用PHP,请检查此代码&nbsp; &nbsp; <?php&nbsp;&nbsp;$current_value = 0;if($_POST['count']){&nbsp; &nbsp; $current_value = $_POST['count'];&nbsp; &nbsp; $current_value++;}&nbsp;?><form action="" method="POST">&nbsp; &nbsp; <span class="points-count"><?=$current_value?></span>&nbsp; &nbsp; <input type="hidden" name="count" value="<?=$current_value?>">&nbsp; &nbsp; <button type="submit">Add</button></form>

天涯尽头无女友

而不是使用 php。您可以为此使用 jquery 或 javascript。php 仅用于服务器端渲染。
随时随地看视频慕课网APP
我要回答