如何检查是否单击了链接(a)提交按钮

How to check if link (a) submit button is clicked

本文关键字:链接 提交 按钮 是否 何检查 检查 单击      更新时间:2023-09-26

如何使用PHP检查此代码是否已被单击,以便在if语句中使用它?

<form id="rating" action="index.php" method="post">
    <a href="#" onclick="document.getElementById('rating').submit();">Rating</a>
</form>

因此,如果点击它,我想使用这个查询:

if () {
$result = mysqli_query($con,"SELECT * FROM users WHERE `approved` = 1 ORDER BY `rating`");
}

您必须在表单中添加一个输入,如<input type="hidden" name="hidden_element" value="data"/>,否则服务器将无法接收POST数据。

然后在index.php脚本中,您可以检查是否设置了$_POST['hidden_element']

例如:

if (isset($_POST['hidden_element']) {
$result = mysqli_query($con,"SELECT * FROM users WHERE `approved` = 1 ORDER BY `rating`");
}

因此,由于当前表单没有提交任何数据,我喜欢如果一个表单中有多个按钮,则每个按钮都应该有其propper名称和提交类型的技术在php上,您检查如下if (this button was pressed then) else if (this button was pressed then) else (redirect in none or what ever you need to do when landed)

在您的表单中,我将ahref更改为输入类型submit,名称为按钮

<form id="rating" action="index.php" method="post">
      <input type="submit" name="button"
       onclick="document.getElementById('rating').submit();">Rating
</form>

php操作应该是这样的,以后可以在这里实现ajax调用

    if (isset($_POST['button']) === true && empty($_POST['button']) === tre) 
    {
    $result = mysqli_query($con,"SELECT * FROM users WHERE `approved` = 1 ORDER BY `rating`");
    }