php - 单击 PHP 回显 HTML 按钮时运行 PHP 函数
问题描述
我正在和我的学校一起做这个项目,但我不知道如何在单击 HTML 按钮时调用 PHP 函数
<?php
function sqlUpdateTheScore($idMinusOneString){
$idminusOneVal=(int)$idMinusOneVal;
$MsgQuery=mysqli_query($conn,"UPDATE Point SET Point=".returnCurrentPoint($idMinusOneVal)."WHERE id=".($idMinusOneVal+1).";");
}
for($i=0;$i<$tot;$i++){
print "<tr>" ;
print "<td align='center'>" .$tot_result[$i][0]."</td>";
print "<td align='center'>" .$tot_result[$i][1]."</td>";
print "<td align='center'>" .$tot_result[$i][2]."</td>";
print "<td align='center'><button id='dec_".$i."' onclick='decreaseByOne(".$i.");'>-1</button><div id='pointOfStudent".$i."'>" .$tot_result[$i][3]."</div><button id='inc_".$i."' onclick='increaseByOne($i);'>+1</button></td>";
print "<td align='center'><button onclick='sqlUpdateTheScore($i);'>SAVE</button></td>";
print "</tr>" ;
}
?>
这是sqlUpdateTheScore(); 顶部的功能
解决方案
您不能直接从 html 调用 php 函数。(我也觉得这很烦人)
您可以使用两种解决方法。最简单的是一个表格:
<form action="thePage.php" method="POST">
<input type="text" name="newValue" id="newValue" hidden />
<button onclick="changeValue()">Change Value</button>
<input type="submit" value="SAVE" name="submit" />
</form>
JavaScript:
function changeValue() {
document.getElementById("newValue").innerText = "Some new value";
}
PHP:
if (isset($_POST["submit"])) {
$newValue = $_POST["newValue"]; // You can then run functions off this value
}
另一种方法是使用 AJAX。这是一个很好的教程。
推荐阅读
- java - PreparedStatement 与调用 execute() 的记录不匹配
- nix - 如何将表达式实例化为 json 字符串并在此字符串中构建/实现它的所有依赖项/包
- c - 无法通过系统()从程序中设置带有“回声”的文件
- jquery - Sharepoint 2013,JS 链接人员选择器字段无法为多个用户保存
- android - 当互联网不可用时,我可以在我的 Android 应用程序中显示天气状况吗
- android - 无法从 WebView Android 打开图库或文件管理器
- mysql - 有没有办法使用 ansible 连接 mysql 服务器并执行诸如执行 .sql 文件和各种 mysql 转储和恢复之类的操作?
- json - Json.NET IsValid with schema passing even though not matching
- android - 有没有办法读取小米(Redmi)的电池权限以进行后台进程?
- java - 当两个单独的应用程序使用同一个数据库时,Hibernate 一级缓存结果是否会过时?