简体   繁体   中英

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

How do I check with PHP if this code has been clicked so I can use it in an if statement?

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

So if it is clicked I want to use this query:

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

You would have to add an input, like <input type="hidden" name="hidden_element" value="data"/> to your form, otherwise there is no POST data for the server to receive.

Then in the index.php script you can check if $_POST['hidden_element'] is set.

For your example:

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

So since your current form doesn't submit any data i like the technique of if multiple buttons are inside a form each should have its propper name and a type of submit and on php you check like 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)

your form, i changed the ahref to an input type submit with the name of button

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

the php action should look like this, you can later implement ajax calling here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM