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

时间:2013-07-13 23:25:26

标签: php javascript

如果单击此代码,我如何检查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`");
}

2 个答案:

答案 0 :(得分:4)

您必须在表单中添加<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`");
}

答案 1 :(得分:0)

因此,由于您当前的表单不提交任何数据,我喜欢的技术是,如果表单中有多个按钮,则每个按钮都应具有其提示名称和提交类型 在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更改为输入类型提交,其名称为按钮

<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`");
    }