<?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?
<?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?
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>';
}
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.
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.