douren2395 2014-04-04 12:37
浏览 63
已采纳

使用yii框架提交表单时,检查文本框的值是否为空白

I am new in yii framework.I am doing a search search operation using yii framework.My requirement is to check textbox value is null or not in the controller.I can print the textbox value in a variable '$experience'.but i cant check it is blank or not.controler name Sitecontroller.php,view search.php and model job.php

My controller is Sitecontroller.php

<?php
 class SiteController extends Controller
 {
 public function actionsearch()
  {
   $user_id = trim($_GET['id']);  
   $model = new Job ;
   if(isset($_POST['Job']))
      {
        if(Yii::app()->getRequest()->getIsAjaxRequest())
          {
            echo CActiveForm::validate(array($jobProfileM2)); 
            Yii::app()->end(); 
          }
          $model->attributes=$_POST['Job'];
          if($model->validate())
           {
             $title=$_POST['Job']['title'];
             $experience=$_POST['Job']['experience'];
             echo "data".$experience=trim($experience);
             if($_POST['Job']['experience'] == " ")
             {
                echo "hai";
             }
  $model=Job::model()->findAll(array('select'=>'*',"condition"=>"(title like '%$title%')
  or   (key_skills like '%$title%')",));
             $number=count($model);
//$this->redirect($this->createUrl('site/search_result',array('title'=>$title,)));  
           }
           else
           {
             Yii::app()->user->setFlash('error', "Validation failed!");
           }                                 
    }
   $this->render('search',array('model' =>$model));
 }
 }
  ?>

//view-search.php

<div class="form">
  <?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>false,
'htmlOptions' => array(),
    'clientOptions'=>array(
    'validateOnSubmit'=>true
),
 )); ?>
  <?php
foreach(Yii::app()->user->getFlashes() as $key => $message) {
    echo '<div class="flash-' . $key . '">' . $message . "</div>
";
   }
 ?>
   <div class="row">
   <?php echo $form->labelEx($model,'Keyword'); ?>

    <?php echo $form->textField($model,'title'); ?>
    <?php echo $form->error($model,'Keyword'); ?>
</div>
   <div class="row">
   <?php echo $form->labelEx($model,'Experience'); ?>
    <?php echo $form->textField($model,'experience'); ?>
    <?php echo $form->error($model,'experience'); ?>
</div> 
<div class="row buttons">
    <?php echo CHtml::submitButton('Search'); ?>
</div>
   <?php $this->endWidget(); ?>
  </div>

I cant print 'hai' message when experince textbox is blank.Anybody help me?

  • 写回答

1条回答 默认 最新

  • drq61040 2014-04-04 13:56
    关注
    if($_POST['Job']['experience'] == " ")
    {
        echo "hai";
    }
    

    This does not check whatever the string is empty. This check whatever there is a space. What you want is to check for "". If however it not supposed to continue follow Michiel suggestion.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题