简体   繁体   中英

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. 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

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. 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.

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.

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;
}

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