如果按钮提交上的空文本区域将显示成功消息

时间:2015-11-12 05:21:13

标签: php html arrays forms

我正在尝试在提交按钮单击时收到错误消息但仅从文本字段获取消息未从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 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>

2 个答案:

答案 0 :(得分:0)

在这一行:

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

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

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

答案 1 :(得分:0)

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

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