[英]yii how textfield input call json action
我嘗試調用json操作時遇到問題
首先,在yii _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;
}
在這種情況下,讓我們忽略該函數的確切功能(僅作為示例)。 我想知道,文本字段如何調用/觸發json文件中的getuserdata操作index.php?r = user / getuserdata 。 這意味着如何建立文本字段和json文件的動作之間的關系。
請告訴我如何在yii中進行這項工作
_form.php(textfield)觸發json文件(操作)
使用javascript,您可以實現一個函數,該函數基於某個javascript事件使控制器actioncall產生ajax ...
<?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的引用,以便構建 異步調用以及應用程序如何圍繞它進行旋轉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.