2016-10-03 24 views
0

我有一個鏈接時,它通過渲染ajax從控制器請求一個頁面,以前我曾經只傳遞id,但現在我想傳遞一個額外的參數控制器,我如何做到這一點, 這是我曾嘗試傳遞yii2中的兩個參數ajax請求使用jQuery到控制器

這是鏈接」只有傳遞給控制器​​

Html::a('click me', ['#'], 
['value' => Url::to('checktruck?id='.$model->id), //this is where the param is passed 
'id' => 'perform']); 

這是控制器代碼預計2個參數一個參數:

public function actionChecktruck($id,$category) //it expects 2 parameters from above link 
{ 
    $truckdetails = Truck::find()->where(['id' =>$id])->one(); 

    if (Yii::$app->request->post()) { 
     $checklistperform = new TodoTruckChecklist(); 
     $truck = Truck::find()->where(['id'=>$id])->one(); 
     $checklistperform->truck_id=$id; 
     $checklistperform->registered_by=Yii::$app->user->identity->id; 
     $checklistperform->save(); 
     $truck->update(); 

     var_dump($checklistperform->getErrors()); 
     //var_dump($truck->getErrors()); 
    } 
    else { 
     $truckcategory = Checklist::find()->where(['truck_category'=>$truckdetails->truck_category])->andWhere(['checklist_category'=>$category])->all(); 
     return $this->renderAjax('truckyard/_checklistform', [ 
      'truckcategory' => $truckcategory,'truckvalue'=>$id, 
     ]); 

    } 

} 

這是另一個按鈕後請求期間取決於上述控制器上的我的jQuery代碼

$("#postbutn").click(function(e) { 

    $.post("checktruck?id="+truckid, //here i would like to pass 2 params 
       {checked:checked,unchecked:unchecked,truckid:truckid} 
      ) 
} 

這是jQuery代碼時,有沒有交

我如何可以傳遞一個額外的參數對於控制器

回答

1

首先,當你正在使用JQuery AJAX提交表單,就沒有必要使用該ID的鏈接

Html::a('click me', ['#'],['id' => 'perform']); 

設定值,則可以按如下

$this->registerJs("$('#perform').click(function(event){ 

event.preventDefault(); // to avoid default click event of anchor tag 

$.ajax({ 
    url: '".yii\helpers\Url::to(["your url here","id"=>$id,"param2"=>param2])."',   
    success: function (data) { 
      // you response here 
     }, 
     }); 
});"); 
提交申請

沒有必要提及方法屬性作爲「POST」,你想通過GET方法

最後送你的控制器,你需要接受的參數如下

public function actionChecktruck() //it expects 2 parameters from above link 
{ 
    $id = Yii::$app->request->queryParams['id']; 
    $param2 = Yii::$app->request->queryParams['param2']; 

    $truckdetails = Truck::find()->where(['id' =>$id])->one(); 

    if (Yii::$app->request->post()) { 
     $checklistperform = new TodoTruckChecklist(); 
     $truck = Truck::find()->where(['id'=>$id])->one(); 
     $checklistperform->truck_id=$id; 
     $checklistperform->registered_by=Yii::$app->user->identity->id; 
     $checklistperform->save(); 
     $truck->update(); 

     var_dump($checklistperform->getErrors()); 
     //var_dump($truck->getErrors()); 
    } 
    else { 
     $truckcategory = Checklist::find()->where(['truck_category'=>$truckdetails->truck_category])->andWhere(['checklist_category'=>$category])->all(); 
     return $this->renderAjax('truckyard/_checklistform', [ 
      'truckcategory' => $truckcategory,'truckvalue'=>$id, 
     ]); 

    } 

} 
0

鏈接甚至$。員額要求試試這個, 在查看文件

$this->registerJs("$('#postbutn').click(function(){ 
$.ajax({ 
    url: '".yii\helpers\Url::to(["u r URL"])."',   
    method: 'POST',  
    data: {id:id, truckid:truckid }, 
    success: function (data) { 
     }, 
     }); 
});"); 
相關問題