Skip to content Skip to sidebar Skip to footer

Alert Message On Submit If Text Box Is Empty Using Php

How to prevent a form from submit if text box is empty? This I have done in JSP successfully using alert message. Javascript: function validate() { var x=document.forms['Form

Solution 1:

You cannot stop a form from submitting using PHP.

PHP is server side, you would need to have Javascript to handle the form submit if there is an issue with your validation. You should handle validation on both client side (JS) and server side (PHP).

So no, not possible with just PHP as you outlined, the form WILL submit, then you validate it. Can't stop it with PHP (as its just HTML to the user).

Solution 2:

you can used from this jquery code:

$("#btnSubmitID").click(function(event){
     if($("#txtID").val()==''){
          event.PreventDefault();
          alert("your message");
     }
});

this is a sample, you can validate your form with this way before post to the server.

Solution 3:

You could submit the form using XHR (known as AJAX) and have php verify the data. You would still need to submit the XHR using javascript.

Solution 4:

Your javascript code looks fine and it should not submit the form when its empty. However, if you want to do from PHP code, you can do the same, but the form needs to submit and return back to same page when its not valid (or empty).

Here is sample code

<?phpif ($_POST['comp'] == '')
   {
    header("location:yourpagename");
  }
else
{
 // process
}
?>

Solution 5:

And now for the non-pseudo code answer. This is working, tested code that elaborates on the concepts I already posted.

<?phpfunctionformWasPosted()
{
    return array_key_exists( 'comp', $_POST );
}
// -------functionvalidate( &$errors)
{
    if ( $_POST['comp'] == '' )
    {
        $errors['comp'] = 'cannot be blank';
    }

    return count( $errors ) == 0;
}
// -------functiongetError($fieldName, $errors)
{
    $out = '';

    if ( array_key_exists( $fieldName, $errors ) )
    {
        $out = '<span class="errorMsg">' . $errors[$fieldName] . '</span>';
    }

    return$out;
}
//------------------------------------------------------------------------// $errors will be passed to our validate function so we can set error messages per field if desired.$errors = array();
$formMsg = '';

if ( formWasPosted() )
{
    if ( validate( $errors ) )
    {
        // do processing here
        header( 'Location: http://www.example.com/success' );
        exit();
    }
    $formMsg = '<div class="errorNotice">There were problems with your submission</div>';
}
?><html><head><scripttype="text/javascript">functionvalidate()
    {
        var x=document.forms["Form1"]["comp"].value;
        if (x==null || x=="")
        {
            alert("comp cannot be blank");
            returnfalse;
        }
    }
</script><style>.errorMsg, .errorNotice { color: red; }
.errorNotice { font-size: 150%;}
</style></head><body><?phpecho$formMsg; ?><formname="Form"action="welcome.php"onsubmit="return validate()"method="post"><labelfor="comp">Comp <?phpecho getError( 'comp', $errors ); ?></label><inputid="comp"type="text"name="comp"></form></body></html>

Post a Comment for "Alert Message On Submit If Text Box Is Empty Using Php"