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

时间:2015-11-13 04:25:59

标签: javascript php html arrays forms

我正在尝试在提交按钮点击时收到错误消息,但仅从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>

2 个答案:

答案 0 :(得分:0)

您还没有在此行中检查过空:

改变这个:

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

为:

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

答案 1 :(得分:0)

id_rsa.pub