activedropDownList在添加提示时不加载ajax函数

时间:2012-12-05 14:20:21

标签: php yii

我在yii中遇到问题,我创建了代码来调用activedropDownList中的ajax来加载值 dropDownList,每件事都有效!

但我的问题是:

1-添加时 array('prompt'=>'选择Yous国家') 所以代码就是这样:

echo CHtml :: activedropDownList($ model,'country',$ model-> getcountry(),array('prompt'=>'Select Yous country'),

和结果:不起作用。

但如果我删除数组('prompt'=>'选择Yous国家'), 它的工作和ajax加载新的列表!

这是我的代码

查看:

<?php
/* @var $this UsersController */
/* @var $model Users */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'users-index-form',
    'enableAjaxValidation'=>false,
)); ?>







    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'الأسم الأول'); ?>
        <?php echo $form->textField($model,'first_name'); ?>
        <?php echo $form->error($model,'الأسم الاول'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'العائلة'); ?>
        <?php echo $form->textField($model,'last_name'); ?>
        <?php echo $form->error($model,'العائلة'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'كلمة المرور'); ?>
        <?php echo $form->passwordField($model,'password'); ?>
        <?php echo $form->error($model,'password'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'تأكيد كلمة المرور'); ?>
        <?php echo $form->passwordField($model,'passwordconfirm'); ?>
        <?php echo $form->error($model,'passwordconfirm'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'البريد الإلكتروني '); ?>
        <?php echo $form->textField($model,'email'); ?>
        <?php echo $form->error($model,'email'); ?>
    </div>
        <div class="row">
        <?php echo $form->labelEx($model,'تأكيد البريد الإلكتروني'); ?>
        <?php echo $form->textField($model,'emailconfirm'); ?>
        <?php echo $form->error($model,'emailconfirm'); ?>
    </div>

        <div class="row">
        <?php echo $form->labelEx($model,'تاريخ الميلاد'); ?>
    <?php

$this->widget(
    'ext.jui.EJuiDateTimePicker',
    array(
        'model'     => $model,
        'attribute' => 'birth_date',
        'language'=> 'en',//default Yii::app()->language
         'mode'    => 'date',//'datetime' or 'time' ('datetime' default)
        'options'   => array(
        'dateFormat' => 'yy-mm-dd',
            //'timeFormat' => '',//'hh:mm tt' default
        ),
    )
);


?>

        <?php echo $form->error($model,'birth_date'); ?>
    </div>





    <div class="row">
        <?php echo $form->labelEx($model,'الجنس'); ?>
        <?php echo chtml::activeDropDownList($model,'gender',$model->getStatusOption(),array('prompt'=>'أختر جنسك')
); ?>
        <?php echo $form->error($model,'gender'); ?>
    </div>

        <div class="row">
        <?php echo $form->labelEx($model,'البلد'); ?>
    <?php

    /*
     echo chtml::activeDropDownList($model,'country',$model->getcountry(),array('prompt'=>'Select your country  ')   , 
    array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('current/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city', //selector to update
//'data'=>'js:javascript statement' 
//leave out the data key to pass all form values through
))

    ); 

    */
    echo CHtml::activedropDownList($model,'country',$model->getcountry(),array('prompt'=>'اختر البلد أو المنطقة '),

    array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('register/dynamiccities'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#city', //selector to update
//'data'=>'js:javascript statement' 
//leave out the data key to pass all form values through
))
    );


   ///




    ////

    ?>


        <?php echo $form->error($model,'country'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model,'المدينة'); ?>
        <?php 
       //empty since it will be filled by the other dropdown
echo CHtml::dropDownList('city','', array());

//echo CHtml::activedropDownList($model,'city',array());
 ?>
        <?php echo $form->error($model,'city'); ?>
    </div>



    <div class="row">
        <?php echo $form->labelEx($model,'رمز الاتصال'); ?>
        <?php echo $form->textField($model,'mobile_code'); ?>
        <?php echo $form->error($model,'mobile_code'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'رقم الجوال'); ?>
        <?php echo $form->textField($model,'mobile_number'); ?>
        <?php echo $form->error($model,'mobile_number'); ?>
    </div>




    <div class="row buttons">
        <?php echo CHtml::submitButton('تسجيــــــــــــل'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

Ajax控制器:

public function actionDynamiccities() /// Called Ajax
{
$country=intval($_POST['Users']['country']);

       $data=Cities::model()->findAll('country_id=:country_id', 
                  array(':country_id'=>$country));
 $data=CHtml::listData($data,'id','city_name_e');

    foreach($data as $value=>$name)
    {
        echo CHtml::tag('option',
                   array('value'=>$value),CHtml::encode($name),true);
    };


}  

提前致谢

1 个答案:

答案 0 :(得分:2)

activeDropDownList只需要4个参数。

  

public static string activeDropDownList(CModel $ model,string $ attribute,array $ data,array $ htmlOptions = array())

'prompt'=>'اختر البلد أو المنطقة '放在与ajax代码相同的数组中。

array(
  'prompt'=>'اختر البلد أو المنطقة ',
  'ajax' => array(
    'type'=>'POST', //request type
    'url'=>CController::createUrl('register/dynamiccities'), //url to call. //Style: CController::createUrl('currentController/methodToCall')
    'update'=>'#city', //selector to update
    //'data'=>'js:javascript statement' 
    //leave out the data key to pass all form values through
  )
)