简体   繁体   中英

Passing two parameters in yii2 ajax request using jquery to a controller

I have a link when pressed it requests a page from a controller via render ajax, Previously i used to pass only the id but now i would like to pass an extra parameter to the controller, how do i achieve this, This is what i have tried

This is the link' which only passes a single parameter to the controller

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

This is the controller code expecting 2 parameters:

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,
        ]);

    }

}

This is my jquery code of another button that depends on the above controller during post request

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

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

This is the jquery code when there is no post

How can i pass an extra parameter in the link or even the $.post request for the controller

First of all, as you are using JQuery ajax to submit the form, there is no need to set value for the link

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

using this id you can submit the request as follows

$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
        },
      });
});");

There is no need to mention method attribute as 'POST', you want to send through GET method

And finally in your controller, you need to accept parameters as follows

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,
        ]);

    }

}

Try this one, In View file

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM