如何将变量从javascript传递给yii2函数

时间:2017-11-10 10:51:58

标签: mysql yii2

我有一个公司名称的下拉列表。当我从中选择公司并点击'获取信息'按钮我应该获得具有所选公司ID的行。

查看

$typeahead = Select2::widget([
    'id' => 'front_companies_select',
    'name' => 'state_10',
    'data' => $companyNameList,
    'options' => [
        'placeholder' => 'Select ...',
        'multiple' => true
    ],
]);
 <?= Html::SubmitButton( 'Get info', [ 'class' => 'btn btn-success' , 'id' =>'getinfo']) ?> 

Javascript文件

$("#getinfo").click(function(){
        var id = $("#front_companies_select option:selected").val();
        console.log(id); //want to pass this id in controller as ap parameter actionInfo($id)
     });

控制器

public function actionVerify($id)  // want to get id here
    {
        $model = $this->findModel($id);
        $connection = Yii::$app->db;
        $model = $connection->createCommand("SELECT * FROM companies where id=$id");
$users = $model->queryOne();
            return $this->redirect(['companies/index']);   //want to show rows in this gridview    
    }

任何人都可以解决它吗?我被困住了..

1 个答案:

答案 0 :(得分:0)

假设您在没有任何网址参数的控制器中,您可以执行以下操作:

$("#getinfo").click(function(){
        var id = $("#front_companies_select option:selected").val();
        window.location.href = 'verify?id=' + id;

});

否则,您必须构建URL并将其传递给window.location