2013-03-13 42 views
0

有一個問題,當我嘗試調用JSON動作警予我怎麼文本框輸入電話JSON動作

首先,在警予_form.php這個,我已經得到了包括文本框輸入一個名稱的形式,看起來象下面這樣:

<div class="row"> 
<?php echo $form->labelEx($model,'name'); ?> 
<?php echo $form>textField($model,'name'); ?> 
<?php echo $form->error($model,'name'); ?> 
</div> 

我要的是,當我輸入字符「n」,現場工具(電話)行動 我在獨立JSON文件定義(如的index.php ?r = user/getuserdata),

read: { 
     url:"index.php?r=user/getuserdata", 
     dataType: "json", 
     type:"post" 
     }, 

UserController.php,有它告訴細節這是什麼動作究竟做所謂的 「actionGetUserData()」, 的功能。

public function actionGetUserData(){ 
    $amount = User::model()->findAll("1 = 1 order by id DESC"); //為了排序 
    $count = count($amount); 
    for ($i=0;$i<$count;$i++){ 
     $arr[$i]['id'] = $amount[$i]['id']; 
     $arr[$i]['username'] = $amount[$i]['username']; 
     $arr[$i]['userpwd'] = $amount[$i]['userpwd']; 
     $arr[$i]['usertype'] = $amount[$i]['usertype']; 
     $arr[$i]['modifytime'] = $amount[$i]['modifytime']; 
     $arr[$i]['createtime'] = $amount[$i]['createtime']; 
     $arr[$i]['allowip1'] = $amount[$i]['allowip1']; 
     $arr[$i]['allowip2'] = $amount[$i]['allowip2']; 
     $arr[$i]['allowip3'] = $amount[$i]['allowip3']; 
     $arr[$i]['allowip4'] = $amount[$i]['allowip4']; 
     $arr[$i]['allowip5'] = $amount[$i]['allowip5']; 

     /* $arr[$i][] = $amount[$i]->attributes; */ 
     /* echo "<pre>"; 
     print_r($amount[$i]->attributes); 
     echo "</pre>"; */ 
    } 
    $result = json_encode($arr); 
    echo $result;  
} 

在這種情況下,讓我們忽略該函數的準確性(僅舉例)。 我想知道,textfield如何調用/觸發getuserdata動作index.php?r = user/getuserdata在json文件中。 這意味着如何建立文本字段和json文件操作之間的關係。

請告訴我如何在警予

_form.php這個(文本框)這項工作觸發JSON文件(動作)

回答

0

使用JavaScript,你可以實現一個功能,這使得一個Ajax到你基於一定的JavaScript事件控制器actioncall ...

<?php Yii::app->clientScript->registerScript('makeAjaxCall', 
    'function makeAjaxCall(){ 
     $.ajax({ 
      url:"index.php?r=user/getuserdata", 
      dataType: "json", 
      type:"post" 
      /*Any Other Ajax Options here, like the beforeSend, then, done and fail callbacks*/ 
     }) 
    });?> 
<?php echo $form->textField($model,'name',array('onblur'=>'js:makeAjaxCall();')); ?> 

要做到這一點,你可以學習有關jQuery.ajax()功能和jqXHR因此參考您可以構建異步調用以及您的應用程序如何圍繞它執行操作。