How to send multiple parameter AJAX in Laravel 5.2

How do I send multiple values for AJAX Laravel. for Example:

$('#submit_').on('click', function (e) {
     e.preventDefault();
     var form_data = $('#create').serialize();
     var form_taxonomy = 'category';
     $.ajax({
          headers: {
              'X-CSRF-Token': $('input[name="_token"]').val()
          },
                type: 'post',
                url: '{!! URL::route('category') !!}',
                data: {formData:form_data,formTaxonomy: form_taxonomy},
                success: function () {
                    $('#append').load('{!! URL::route('loadCat') !!}');
                },
                error: function (xhr, status, errorThrown) {
                    alert(JSON.parse(xhr.responseText).category[0]);
                }
            });
            jQuery("#create").val('');
        });

controller code:

public function create(messageRequest $request)
{
    if($request->ajax()) {
        $name = Input::get('formData');
        $taxonomy = Input::get('formTaxonomy');
        return response()->json($name, $taxonomy);
    }
}

html code:

<div class="col-sm-6">
<form method="POST" action="http://localhost:8000/category" accept-charset="UTF-8"><input name="_token"
                                                                                          value="IzByO9fU5yeanaVCudCQpkL5bXGzUh9B4jb400iU"
                                                                                          type="hidden">

    <div class="form-group">
        <div class="form-group">
            <input class="form-control text-right" id="create" name="category" type="text">
        </div>
        <div id="submit_"><input name="createSub" id="submit" class="btn btn-primary" value="submit" type="submit">
        </div>
    </div>
</form>

message request validate:

    public function rules()
{
    return array(
    'category'=>'required|alpha|unique:taxonomies,name',
    );
}
public function messages(){
    return [

        'category.required'=>'fill',
        'category.alpha'=>'only charecter',
        'category.unique'=>'dublicate'
    ];
}

This code not work . I used my other examples, but no one was not responsive to the problem is that I don't know only parameter data in laravel how to call the amount that would not be faced with an error and stored in the database .

Answers 1

  • You've already serialized the form, which generates the name=value&name=value query string format. It looks like you then want to add data to this query string for submission. You will need to do this somewhat manually, but it can be done like this:

    $('#submit_').on('click', function (e) {
        e.preventDefault();
        var form_data = $('#create').serialize();
        var form_taxonomy = 'category';
        $.ajax({
            headers: {
                'X-CSRF-Token': $('input[name="_token"]').val()
            },
            type: 'post',
            url: '{!! URL::route('category') !!}',
            // manually combine your form data and your additional post data
            // into one query string
            data: form_data + '&' + $.param({formTaxonomy: form_taxonomy}),
            success: function () {
                $('#append').load('{!! URL::route('loadCat') !!}');
            },
            error: function (xhr, status, errorThrown) {
                alert(JSON.parse(xhr.responseText).category[0]);
            }
        });
        jQuery("#create").val('');
    });
    

    Edit

    With your existing code, the issue that you're having is that your messageRequest validation says that the category field is required, but your request data does not have a category field. Because of this, your validation is failing, and will return a 422 response with a JSON object containing your validation errors.

    With the updated code above, your request data now has a category field, so validation is passing, but you have some other error in your code that is generating a 500 error. You need to track this down and fix it, which may require another question.


Related Articles