alert message on submit if text box is empty using PHP

18,737

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.

Share:
18,737
user1074824
Author by

user1074824

Updated on July 09, 2022

Comments

  • user1074824
    user1074824 almost 2 years

    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["Form1"]["comp"].value;
       if (x==null || x=="")
       {
         alert("comp cannot be blank");
         return false;
       }
    
       <form name="Form" action="welcome.php" onsubmit="return validate()"  method="post">
          <input type="text" name="comp">
    

    How can I do the same using PHP? So that whenever a user submits without entering text it should give message to user through an javascript alert() message.

    I can display alert message:

    echo  '<script language="javascript">alert("comp cant blank");</script>';
    

    But how can i give condition?what are the changes has to be made in the above please put it in a code form.i have to do it only in PHP.

  • Damien Pirsy
    Damien Pirsy over 12 years
    You misunderstood the question, he's not asking for a javascript validation (as he's already doing that)
  • Damien Pirsy
    Damien Pirsy over 12 years
    Uhm..so OP asks for a php real-time validation and you give him pseudo code validation algorithm? :/ Please guys read the questions well..
  • Tim G
    Tim G over 12 years
    If you're using POST, then check $_POST['field_name'] - you can't use DOM addressing in PHP.
  • Tim G
    Tim G over 12 years
    @damien I'm inferring his intent from his question - he intends to prevent a form submission from being processed. The question seems confused because of the OP's history with JSP. I'm merely trying to be helpful with my experiences. If someone comes along with a better answer I'm ok with that.
  • Tim G
    Tim G over 12 years
    Even if you do validate it using PHP (i'm thinking an ajax submission and server side validation to prevent the form from being submitted) you STILL need to validate again on the actual form submission because a person using firebug, or a browser without javascript can still send bad data.
  • user1074824
    user1074824 over 12 years
    form shouldn't be submit. it jst give message to user the text box shludn't be empty. how can i do this?
  • Tim G
    Tim G over 12 years
    Just so we're clear on this... there is no way to prevent a form from being submitted using php, the best practice is to validate and selectively process based on a form submission.
  • AjayR
    AjayR over 12 years
    when you return false from the javascript code, it should not submit.