mardi 30 mai 2017

jquery php ajax insert data failed

I've tried to insert data from modal into database by ajax I'm using Laravel 5.3 I can't enter the data even when I console val is good but no passing into db notice there is relationships between database:may make difference modal code

<div class="modal fade" tabindex="-1" role="dialog" id="edit-modal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Edit Post</h4>
            </div>
            <div class="modal-body">

                <form>
                    <div class="modal-body">
                        <label for="post-body">Edit the Post</label>

                        <table class="table table-bordered">
                            <thead>
                            <tr>


                                <th style="background-color: #e85657; color: #FFFFFF ; text-align: center">pages</th>
                                <th style="background-color: #ff6a06; color: #FFFFFF ; text-align: center">category</th>
                                <th style="background-color: #2c81ba; color: #FFFFFF ; text-align: center">author</th>
                                <th style="background-color: #0f7864; color: #FFFFFF ; text-align: center">book name</th>
                                <th style="background-color: #5d6d7e; color: #FFFFFF ; text-align: center">day</th>
                            </tr>
                            </thead>
                            <tbody>

                            <tr>



                                <td>
                                    <div class="input-group">
                                        <input type="text" id="page" name="page" class="form-control" placeholder="pages" aria-describedby="basic-addon1">
                                        <span class="input-group-addon" id="basic-addon1"><i class="fa fa-file-text" aria-hidden="true"></i></span>
                                    </div>
                                </td>
                                <td>
                                    <div  class="input-group">
                                        <select id="option-list1" name="cat_id">
                                            <option value="1" disabled selected> category </option>
                                            <option value=""></option>
                                        </select>
                                        <span class="input-group-addon" id="basic-addon1"><i class="fa fa-tags" aria-hidden="true"></i></span>
                                    </div>
                                </td>
                                <td>
                                    <div class="input-group">
                                        <input type="text" id="author" name="author" class="form-control" placeholder="author" aria-describedby="basic-addon1">
                                        <span class="input-group-addon" id="basic-addon1"><i class="fa fa-user" aria-hidden="true"></i></span>
                                    </div>
                                </td>
                                <td>

                                    <div class="input-group">
                                        <input type="text" id="book" name="book" class="form-control" placeholder="name" aria-describedby="basic-addon1">
                                        <span class="input-group-addon" id="basic-addon1"><i class="fa fa-book" aria-hidden="true"></i></span>
                                    </div>

                                </td>

                                <span class="input-group-addon" id="basic-addon1"><i class="fa fa-table" aria-hidden="true"></i></span>

                                <th>
                                    <div  class="input-group">
                                        <select id="option-list day" name="day">
                                            <option disabled selected> days</option>
                                            <option value="saturday">saturday</option>
                                            <option value="sunday">sunday</option>
                                            <option value="monday">monday</option>
                                            <option value="thursday">thursday</option>
                                            <option value="wenensday">wenensday</option>
                                            <option value="tuesday">tuesday</option>
                                            <option value="friday">friday</option>

                                        </select>
                                        <span class="input-group-addon" id="basic-addon1 basics"><i class="fa fa-table" aria-hidden="true"></i></span>


                                    </div>
                                </th>


                            </tr>


                            </tbody>

                        </table>

                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" id="modal-save">Save changes</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

javascript code

var urlSave = '';
var token = '';
var weekId = 0;

$(function () {
    $('#error').hide();
    $('.update_user_button').on('click', function (event) {
        event.preventDefault();
        //var token = $('input[name="_token"]').val();
         weekId = $(this).attr('data-weekid');
        var days = $('input[name="day"]').val();
        var book = $('input[name="book"]').val();
        var author = $('input[name="author"]').val();
        var page = $('input[name="page"]').val();
        var data = new FormData();
        data.append('_token', token);
        data.append('week_id', weekId);
        data.append('book_name', book);
        data.append('author', author);
        data.append('page', page);

        $('#edit-modal').modal();


        $('#modal-save').on('click', function () {
            $.ajax({
                method: 'POST',
                url:urlSave,
                data: {
                    week_id: weekId,
                    days: days,
                    book_name: book,
                    author: author,
                    page: page,
                    _token: token
                },
                success: function (data) {
                    $('#edit-modal').modal('hide');
                    alert('done');
                },
                error: function (data) {
                    var errors = data.responseJSON;
                    console.log(errors);
                }



            });
        });


    });
});

BookController code

public function addBook(Request $request)
{
    $message = 'There was an error';

    //validation

    $book = new Book();
    $book->week_id = $request['week_id'];
    $book->days = $request['day'];
    $book->book_name = $request['book'];
    $book->author = $request['author'];
    $book->page = $request['page'];
    if($request->user()->week()->book()->save($book)){
        $message = 'Post Successfully created';
    };


    //save posts to currently auth user


    return Response::json($book);



}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2rghQ55
via IFTTT

Aucun commentaire:

Enregistrer un commentaire