Yii:当搜索字段为空并按下提交时显示消息

时间:2016-01-08 17:53:29

标签: php yii

我是一个Yiibie,我希望当我的搜索字段为空并且按下提交按钮时,它会在我的屏幕上显示消息以填充该字段。 这是搜索栏的代码

   <?php
/* @var $this VolunteerFormController */
/* @var $model VolunteerForm */
?>
<?php $roles=Rights::getAssignedRoles(Yii::app()->user->Id);
foreach($roles as $role) 
{ 
if($role->name == 'Admin')
{?><!--till here is done so that if the user is admin the view will be shown and if it not an admin 
   only the message will be shown which we have written in the bottom, same we have done for the user fill the volunteer form-->


<?php
$this->breadcrumbs=array(
    'Volunteer Forms'=>array('index'),
    $model->id,
);

$this->menu=array(
    array('icon' => 'glyphicon glyphicon-list','label'=>'List VolunteerForm', 'url'=>array('index')),
    array('icon' => 'glyphicon glyphicon-plus-sign','label'=>'Create VolunteerForm', 'url'=>array('create')),
    array('icon' => 'glyphicon glyphicon-edit','label'=>'Update VolunteerForm', 'url'=>array('update', 'id'=>$model->id)),
    array('icon' => 'glyphicon glyphicon-minus-sign','label'=>'Delete VolunteerForm', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
    array('icon' => 'glyphicon glyphicon-tasks','label'=>'Manage VolunteerForm', 'url'=>array('admin')),
);
?>

<?php echo BsHtml::pageHeader('View','VolunteerForm '.$model->id) ?>

<?php $this->widget('zii.widgets.CDetailView',array(
    'htmlOptions' => array(
        'class' => 'table table-striped table-condensed table-hover',
    ),
    'data'=>$model,
    'attributes'=>array(
        'id',
        'team_name',
        'name_of_ngo',
        'email',
        'address',
        'no_of_members',
        'experience',
        'user_id',
    ),
)); ?>
<?php }
if($role->name=='Authenticated')
{?>
 <br><br><br><br><br><br><br><br><br><br>
            <div class="alert alert-success">
  <strong>Congrats!</strong> You have signed up for Volunteering, You will be contacted soon..!!.
</div>
     <?php

        }     
}
?>

请帮帮我,谢谢。

1 个答案:

答案 0 :(得分:1)

您需要JQuery以及需要检查的字段的ID(使用正确的值更改YourIdOfTheFieldToEval)

您可以在FireFox中找到ID,按CTRL + u并检查源页面代码..或通过.. firebugs。

<script type="text/javascript">
  $('form').submit(function () {

  // Get the value and trim it
   var field1 = $.trim($('#YourIdOfTheFieldToEval1').val());

   // Check if empty of not
   if (field1=== '') {
       alert('Text-field is empty.');
       return false;
   }

  // Get the value and trim it
   var field2 = $.trim($('#YourIdOfTheFieldToEval2').val());

   // Check if empty of not
   if (field2=== '') {
       alert('Text-field is empty.');
       return false;
   }
   // repeat for the fields you need to check 
   .......

 });
</script>