【问题标题】:if empty text area on button submit will shows sucess message如果按钮提交上的空文本区域将显示成功消息
【发布时间】:2016-02-13 09:26:21
【问题描述】:

我试图在单击提交按钮时获取错误消息,但仅从文本字段获取消息而不是从文本区域获取消息。这是我的代码。

问题是如果我提交没有文本区域的字段将显示成功。请帮忙。

    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 Sucessfully'); 
                }
                else
                {
?>                           
                  <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">
                                <label class="control-label" for="inputSuccess">Offer title
                                </label>
                                <input type="text" class="form-control" name="offered" id="offered" required>
                                <label class="control-label" for="inputSuccess">Offer Description
                                </label>
                                <textarea id="description" name="description" placeholder="Offer Description" class="form-control " rows="3" required>
                                </textarea>
                                <br>  
                                <button type="submit" class="btn btn-primary">
                                <span>SUBMIT
                                </span>
                                </button>
                            </div>
                            </form>

【问题讨论】:

    标签: php html arrays forms


    【解决方案1】:

    如果我提供此代码,则如果所有字段都已填写,则无法提供成功消息

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

    【讨论】:

      【解决方案2】:

      在这一行:

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

      您只检查$_POST['offered'] 是否为空,然后检查$_POST['description'] 是否为true(不是您想要做的)。您还需要对$_POST['description'] 执行empty()

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

      【讨论】:

      • 如果我提供此代码,那么如果所有字段都已填写,则无法提供成功消息
      猜你喜欢
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多