简体   繁体   English

如何在Laravel 4.2中使用URL :: action方法使用JavaScript函数将变量视图传递给控制器

[英]How to pass a variable view to controller using JavaScript function using URL::action method in laravel 4.2

The choice value is assigned from a dropdownlist.That value is passing to javascript myFunction as a parameter. 选择值是从一个下拉列表中分配的,该值作为参数传递给javascript myFunction。 And i want to create a table in view using data in database. 我想使用数据库中的数据在视图中创建表。 The code works fine and creates table when im not passing a parameter. 当我不传递参数时,代码可以正常工作并创建表。 But i want to pass a parameter.Help me here guys. 但是我想传递一个参数。

This is my code in view 这是我的代码

 function myFunction($choice) {
    document.getElementById("demo").innerHTML = "You selected: " + $choice;

    $.get("{{ URL::action('CombinationController@readData',array('choice'=> 'choice'))}}", function (data) {
        $.each(data, function (i, value) {
            var tr = $("<tr/>");
            tr.append($("<td/>", {
                text: value.student_id
            })).append($("<td/>", {
                text: value.initials + " " + value.last_name
            })).append($("<input />", {
                type: 'checkbox', value: value.student_id
            }))

            $('#student').append(tr);
        });
    });
}

This is code in Route 这是路线中的代码

 Route::get('readData/{choice}','CombinationController@readData');

This is my CombinationController 这是我的CombinationController

public function readData($choice)
{
    if($choice==0) {
        $StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
    ->select('combination_submits.student_id', 'student.initials', 'student.last_name')
    ->where(['choice_1' => 'PS1'])
    ->get();
    }
    return $StuPS1ch1;
}

When your view is rendered, php has already finished compilind all the code. 呈现视图时,php已经完成了所有代码的编译。 By the time your javascript function runs (client side), your URL (generated by PHP on the server side) is already a string, so you won't have the effect you want. 在您的JavaScript函数运行时(客户端),您的URL(由服务器端的PHP生成)已经是字符串,因此您将无法获得想要的效果。

If you try to generate a url with dinamic segments, like this: 如果您尝试使用动态分段生成网址,如下所示:

URL::action('CombinationController@readData',array('choice'=> $choice))

You'll probably get an error, because PHP's URL facade requires the $choice variable to be available on render. 您可能会得到一个错误,因为PHP的URL外观要求$ choice变量在渲染时可用。

I sugest changing the variable to a query string, so you can do something like this: 我建议将变量更改为查询字符串,因此您可以执行以下操作:

function myFunction(choice) {
    document.getElementById("demo").innerHTML = "You selected: " + choice;

    $.get("{{ URL::action('CombinationController@readData') }}?choice=" + choice, function (data) {
        $.each(data, function (i, value) {
            var tr = $("<tr/>");
            tr.append($("<td/>", {
                text: value.student_id
            })).append($("<td/>", {
                text: value.initials + " " + value.last_name
            })).append($("<input />", {
                type: 'checkbox', value: value.student_id
            }));

            $('#student').append(tr);
        });
    });
}

Also you'll have to change your controller: 另外,您还必须更改控制器:

public function readData()
{
    $choice = \Request::input('choice');
    if($choice==0) {
        $StuPS1ch1 = CombinationSubmits::join('student', 'combination_submits.student_id', '=', 'student.student_id')
    ->select('combination_submits.student_id', 'student.initials', 'student.last_name')
    ->where(['choice_1' => 'PS1'])
    ->get();
    }
    return $StuPS1ch1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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