如果提交按钮上的文本区域为空,则表单验证中将显示成功消息


If empty text area on button submit will show success message in form validation

当点击提交按钮时,我正在尝试获取错误消息,但仅从textfield获取消息,而不是从textarea获取消息。这是我的密码。问题是,如果我提交没有textarea的字段,它将显示成功消息。

  if(empty($_POST)===false)
            {
                if(empty($_POST['offered'])===true||($_POST['description'])===true)
                    {
    ?>                    
                        <div class="alert alert-warning alert-dismissible text-center" role="alert">
                        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>Add some offers and descriptions
                        </div>
     <?php
}
  else
    {
    $title = $_POST['offered'];
    $offer = $_POST['description'];
    $data = array(
        $page_id,
        $title,
        $offer
    );
    if ($data)
        {
        $add = add_data($data);
        header('location:hotel1_galery.php?page_id=1 && msg=Add Offers Successfully');
        }
      else
    if (!empty($_POST['offered']) && !empty($_POST['description']))
        {
?>                           
                                <div class="alert alert-danger alert-dismissible text-center" role="alert">
                                <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span><?php
        echo "Add offers and descriptions "; ?></div>
    <?php
        }
    }
}

HTML

<form action="hotel1_galery.php?page_id=1" method="post" class="col-sm-4" role="form">
   <div class="form-group has-info">
      <input type="hidden" name="id" value="<?php echo $val->offer_id;?>">
      <label class="control-label" for="inputSuccess">Offer title
      </label>
      <input type="text" class="form-control" name="offered" id="offered">
      <label class="control-label" for="inputSuccess">Offer Description
      </label>
      <textarea id="description" name="description"  class="form-control " rows="3" required="offer description is required">
      </textarea>
      <br>  
      <button type="submit" class="btn btn-primary">
      <span>SUBMIT
      </span>
      </button>
   </div>
</form>

您尚未在以下行中检查空:

更改此项:

if(empty($_POST['offered'])===true||($_POST['description'])===true)

至:

if(empty($_POST['offered'])||empty(trim($_POST['description'])))
<?php
if(!empty($_POST))
        {
            if(empty($_POST['offered']) || empty($_POST['description']))
                {
?>                    
                    <div class="alert alert-warning alert-dismissible text-center" role="alert">
                    <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>Add some offers and descriptions
                    </div>
 <?php                  
                }
            else
                {
                    $title=$_POST['offered'];
                    $offer=$_POST['description'];
                    $data=array($page_id,$title,$offer);
                    if($data)
                        {   
                            $add=add_data($data); 
                            header('location:hotel1_galery.php?page_id=1 && msg=Add Offers Successfully'); 
                        }
                    else if(!empty($_POST['offered']) && !empty($_POST['description']))
                        {
?>                           
                            <div class="alert alert-danger alert-dismissible text-center" role="alert">
                            <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span><?php echo "Add offers and descriptions "; ?></div>
<?php
                        }               
                }
        }