0

I am fairly new to Laravel and ajax in general, what I am trying to implement is to pass the value of an input field through an ajax get request. My request looks like this:

function getInfo() {

        $.ajax({
            url: "info",
            dataType: "json"
        }).success(function(data){
            $('#result').append(JSON.stringify(data));
        }).fail(function(){alert('Unable to fetch data!');;
        });
    }
    $('#infoSubmit').click(getInfo);

I have setup a route for my function in laravel that works like this public/info/Variable <--

When I add a variable after info/ I get the data for that variable (e.g profile name)

I need to pass this variable from an inputfield to ajax request to something like this: url: "info/+$inputfieldVariable"

2 Answers 2

1

Change:

url: "info",

TO:

url: "info/" + $('input-field-selector').val(),

Not sure about the correctness of your JS code: Shouldn't you be using done instead of success?

Sign up to request clarification or add additional context in comments.

1 Comment

forgot to remove it , i didn't had "fail" before it. I would revise it and use done instead
0

JavaScript:

function getInfo() {

    var myFieldsValue = {};
    var $inputs = $("#myForm :input");
    $inputs.each(function() {
        myFieldsValue[this.name] = $(this).val();
    });

    $.ajax({
        url: "info",
        dataType: "json",
        data: myFieldsValue,
        type: "GET" // Even if its the default value... looks clearer
        success: function(data){
            $('#result').append(JSON.stringify(data));
        },
        error: function(){
            alert('Unable to fetch data!');
        }
    });

    return false;
}
$('#infoSubmit').click(getInfo);

Untested but should be something like that

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.