簡體   English   中英

如何檢查鏈接(a)提交按鈕是否被單擊

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

如何使用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"/>添加輸入,例如<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`");
}

因此,由於您當前的表單不提交任何數據,我喜歡表單中是否包含多個按鈕的技術,每個按鈕應具有其屬性名稱和提交類型,並且在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`");
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM