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.

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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行