-1
<?php
if ( isset( $_GET['fail']) && !empty($_POST["uname"]))
{
  echo '<script language="javascript">';
  echo 'alert("Your name or password is incorrect")';
  echo '</script>';
}

How do I check for empty field when I press the submit button?

Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
Jovan
  • 13
  • 2
  • 8
  • 1
    Heading things off at the pass - **Never store plain text passwords!** Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). Make sure that you [don't escape passwords](http://stackoverflow.com/q/36628418/1011527) or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Jun 08 '16 at 12:36

3 Answers3

1

Why do you have a "GET" and a "POST" check in your php code? You can't use both verbs when sending an HTTP request unless your form in html looks something like:

<form id="myForm" method="post" action="myAction.php?fail=someStatus">
    <input type="text" name="uname" required />
</form>

If you are not sending the value of fail, the isset( $_GET['fail']) will always return false and your code will never get past your if

<?php
// isset( $_GET['fail']) is always false
if ( isset( $_GET['fail']) && !empty($_POST["uname"]))
{
  echo '<script language="javascript">';
  echo 'alert("Your name or password is incorrect")';
  echo '</script>';
}
StaticBeagle
  • 5,070
  • 2
  • 23
  • 34
0

You could use required attribute of html5 in your input tag as

<input type="text" name="USERNAME" required>
<input type="text" name="PASSWORD" required>

This won't allow to submit form without content in the textbox.

Tirthraj Barot
  • 2,671
  • 2
  • 17
  • 33
  • 3
    While you're not wrong, it's not a great idea to rely only on client side validation, server side validation is a lot more accurate and can't be tampered with as easily. – Andrei Jun 08 '16 at 12:36
  • 1
    He asked "How do I check for empty field when I press the submit button?" It directly implies the client side validation. @Andrew – Tirthraj Barot Jun 08 '16 at 12:38
  • Absolutely, I don't question the validity of your answer in any way. This was only pointed towards OP so he knows that he shouldn't ONLY do that. – Andrei Jun 08 '16 at 12:40
  • I never thought of that! Thanks @TirthrajBarot – Jovan Jun 08 '16 at 12:41
0

I don't see what you are trying to do here. If you submit your form with Ajax you can request a php script (eg. form_submit.php) where you validate your form fields and return the response which will be used by your javascript to display something if there is an error (or not).

Here is simple process to explain how you can do:

html form submitted => Javascript with Ajax call to form_submit.php => Form verification with PHP => Return result from verification => Javascript Ajax get the response and do what you want to do like displaying an error/success popin for example.

KrustyHack
  • 410
  • 3
  • 7