Search code examples
phphtmlerror-handlingis-empty

PHP textbox box empty


I has read all the various post on StackOverflow regards this same but that was not helpful ...

Php script not shows error alert when text box empty or in invalid pattern. I know only empty script which I mention below but I don't known How I add pattern error also.

Also when textbox empty it not show error why? Please help. Please don't mark as it is duplicate because I read various post but same issue that is why I posted a threads. Please help in full script.

Advance thank you..

 <?php
 if(isset($_POST['submit']))
 {
 $name= $_POST['name'];
 if (!($name))
 {
 $m1 = "<Div class="alert">Enter your Name</Div>" ;
 }

 $email= $_POST['email'];
 if (!($email))
 {
 $m2 = "<Div class="alert">Enter Your Email"</Div> ;
 }

 $_SESSION['name'] =$name;
 $_SESSION['email'] =$email;
 $_SESSION['otp']=$rndno;
 header( "Location: next-page.php" ); 
} 
?>

In HTML Body

<div>
<?php if(isset($m1)) { echo $m1; } ?>
<?php if(isset($m2)) { echo $m2; } ?>
</div>

Solution

  • Try This code it is runnable

    <?php
     if(isset($_POST['submit']))
     {
     $name= $_POST['name'];
     if ( $name == '')
     {
     echo $m1 = "<script type='text/javascript'>alert('Please Enter Your Name');</script>" ;
     }
    
     $email= $_POST['email'];
     elseif ( $email == '')
     {
     echo $m2 = "<script type='text/javascript'>alert('Enter Your E-mail');</script>" ;
     }
    
     $_SESSION['name'] =$name;
     $_SESSION['email'] =$email;
     $_SESSION['otp']=$rndno;
    else{
     header( "Location: next-page.php" ); 
    }
    }
    
    ?>