How to pass value from javascript to php controller in laravel?

Passing value from Javascript to php controller in laravel.

I created as a {select} (html element) by clicking a button using Javascript to pass the value and {select} (html element) id of the newly created element to the php controller. So that I can get this value in the controller and insert the value into my DB.

Below is my code:

Javascript Code:

 function addNew() {
    var count = document.getElementById("skill_count").value;

    $('#skills_table tr:last').after('<tr><td>Skill:</td><td><selectname="newskill'+count+'" id="newskill'+count+'">\n\
    </td><td>Exp.in years:</td><td><input type="text" name="newexperience'+count+'"></td><tr>');
    document.getElementById("skill_count").value= parseInt(count)+1;
    var skill = document.getElementById('newskill'+count);
    $.get('skills_json', function(data) {
        $.each(data, function(i, data) {
            var opt = document.createElement('option');
            opt.value = data.skill_id;
            opt.innerHTML = data.skill_name;
            skill.appendChild(opt);
        });
    }, 'json');
}

code in the controller:

 DB::table('userskillmappings')->insert(array(
        'experience' => Input::get("newexperience'+count+'"),
        'user_id' => Session::get('userid')

        ));

Route to the controller:

Route::get( 'skills_json', array( 'uses' => 'MyProfile@skillsReturnJson' ));
+4
source share

Source: https://habr.com/ru/post/1537831/


All Articles