1

I want to check if the form was filled out correctly but the alert appears after I load the page. I guess a site refresh is submitting the login button.

I try to check the incoming data and if they are wrong, it should show an alert.

<?php

$alert = "";

if ($_POST['username'] == null || $_POST['password'] == null){
    $alert = "Please fill in all fields!";
}

if ($alert){
    echo $alert;
}

?>

<form method="POST" action=""> <!-- reloads this page -->
  <input type="text" name="username"/>
  <input type="password" name="password"/>
  <button type="submit" name="login">Login</button>
</form>

If I open the page, the alert appears instantly. How should I do the check, that the alert does not appear after the first load.

  • 2
    My advice is to use JavaScript for that alert. That way you can stop the post of the form and alert the user. – Andreas Jul 15 '19 at 22:04
  • if the form is filled out correctly, doesn't it redirect to the other location? So the rest of this page shouldn't even show. – Barmar Jul 15 '19 at 22:07
  • @Barmar I believe he/she means at the first load (before filling in the form) the message appears. Which is odd since then the POST should not be set in my opinion... – Andreas Jul 15 '19 at 22:08
  • 1
    If you submit the form, and then Reload the page, that resubmits the form. The browser should warn you that it's resending the data. – Barmar Jul 15 '19 at 22:10
  • See https://stackoverflow.com/questions/4327236/stop-browsers-asking-to-resend-form-data-on-refresh?noredirect=1&lq=1 for how to prevent this. – Barmar Jul 15 '19 at 22:13
  • After some time I found a good way to prevent this issue. I had my php code and the html form on the same page, so the php script is loaded before the user can fill in the form. Instead separate files for the form and the scripts should be used. In the "action" tag it is possible to lead to your script. –  Jan 10 '20 at 19:50

2 Answers2

0

The IF-Statement is fine, so your problem is in your Session or Cache.

You can destroy your Session, if you close your browser, but this maybe need some time to destroy the session, so you could delete cache, cookies, etc in your browser setting.
But you can also use session_destroy(); in php to destroy all Variables of POST or GET.

klediooo
  • 630
  • 1
  • 7
  • 25
0

I hate to suggest javascript as the solution, but in this case, javascript is the best solution.

With javascript, you can interrupt the form submission process - do some stuff (such as validate the form fields) and then either return false (cancels the submit) or do nothing (the form submission will continue). But Barmar is straight-on correct with his explanation of what is happening. (Of course he is: it's Barmar )

So the overview is:

  1. Use $('#yourFormID').submit(function(){}) to trap the form submission process

  2. Inside that function, perform your field validation

  3. If a field fails validation, display a message and return control to the user

  4. If all fields validate okay, do nothing else - the form will finish submitting as if there was no interruption at all.

Here is another question/answer that demonstrates how to do that.

How to direct a user directly to a form

cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • For the alert I used JS but I checked the data with php. Thanks for suggesting JS. –  Aug 09 '19 at 20:18