Ajax Posting data into the database.

Posted 3 years ago by nanadjei2

please this is my blade.

Drink A Coffee today


Coffee Orders

<h4>Add a Coffee Order</h4>
{!! Form::open(['url' => 'index', 'method' => 'POST']) !!}
    <div class="form-group" role="form">
    <p>
    <label for="name">Name:</label>
    <input id="name" type="text" name="name" class="form-control">
    </p>

    <p>
    <label for="drink">Drink:</label>
    <input id="drink" type="text" name="drink" class="form-control">
    </p>
    <p>
    <button id="add-order" class="btn btn-default" type="submit">Add!                </button>
    </p>
</div>
{!! Form::close() !!}
</div>

and my script is as follows.

$(document).ready(function() {

$('#add-order').click(function(e) {
    e. preventDefault();
    setting variables based on the input fields
    var inputName = $('input[name="name"]').val();
    var inputDrink = $('input[name="drink"]').val();
    var token = $('input[name="_token"]').val();
    var data = {name:inputName, drink:inputDrink, token:token};
        //var data = $(this).closest('form').serialize();

    var request = $.ajax({

        url: "index",
        type: "POST",
        data: data,
        dataType:"json"
        });

        request.done(function( msg ) {
            var response = JSON.parse(msg);
            console.log(response.msg);
        });

        request.fail(function( jqXHR, textStatus ) {
            console.log( "Request failed: " + textStatus );
        });
    });

});

But anytime i try to submit the form, nothing happends.

Please sign in or create an account to participate in this conversation.