1
votes

I built and basic Jquery Ui slider:

    $("#sliderNumCh").slider({
        range: "min",
        min: 0,
        max: 20,
        step: 1,
        value: 20,
        change : function(e, slider){
            $('#sliderAppendNumCh').empty();
            var getSliderVal = document.getElementById('sliderValue').value = sliderValue;

            $('#sliderAppendNumCh').append(
            <div class=\"form-group\" style=\"width:100%;margin:0 auto;\">
                ...
            </div>);

        },
        slide : function(e , slider){
            $('#number_of_chapters').val(slider.value);
        },
    });     

My question is now how to post the slider value on each slide to my Laravel Controller.

I tried this:

        $.ajax({
            type: 'POST',
            url: "{{Route('getAjaxRequest')}}",
            headers: {'X-Requested-With': 'XMLHttpRequest'},
            data: {value: getSliderVal},
            success: function (option) {
                console.log(getSliderVal);
            }
        });

route:

Route::post('create', ['as' => 'getAjaxRequest', 'uses' => 'ProductController@getAjaxRequest']);

and in my controller I tried to call and return the value like this:

public function getAjaxRequest()
{
    $value =  Request::get('value');
    return view('productRom.edit')->with('value', $value);
}

so basically I want to get the slider value and later on return the value to a php function, to use that value for example in a loop.

I am not used to Ajax and therefor not sure what I am doing wrong.

Edit I tried Jose Rojas suggestion:

public function getAjaxRequest()
{
    $value =  Request::get('value');
    //do your stuff
    return $slideValue;
}

And I get following error in the console:

http://localhost/myapp/public/product/edit/%7BproductID%7D 500 (Internal Server Error)

the actual url is:

http://localhost/myapp/public/product/edit/51

Edit

this is my route:

Route::get('edit/{productID}', ['as' => 'editProduct', 'uses' => 'ProductController@editProduct']);
1
you're posting variable to a method to set to other view?Jose Rojas
yes so basically to use a javascript variable in phputdev
put your ajax code in slide change option or slide optionD Coder
it is inside the change option function, I get the error again if I slide :)utdev
is it show internal serve error..?D Coder

1 Answers

0
votes

If I undestand well, you want to get the value for a slide so the method in your controller instead return a view should return the value for the slide, somthing like this:

public function getAjaxRequest($productID)
{
    $value =  Request::get('value');
    //do your stuff
    return $slideValue;
}

then, in your method that receives the value, you do what you want with this value. The goal of using AJAX is load parts of web page without reloading whole web page.