mercredi 25 septembre 2019

Laravel Ajax on Update data getting Success Message on same Page

i am trying to update data using Ajax in Laravel, my data is being updated successfully but when i click on update it's success message is showing on next page, i want it to show message and updated data on same Page without loading page.

Laravel Controller:

public function update(Request $request, $id)
    {
        $teacher = Teacher::find($id);
        $teacher->efirst = $request->efirst;
        $teacher->esecond = $request->esecond;
        $teacher->save();

        return response()->json([
            'status' => 'success',
            'msg' => 'esecond has been updated'
        ]);
    }

AJAX function: Update.Js,

jQuery(document).ready(function($)  {
    $("#update-form").submit(function (e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "teachers/" + $('#update-id').attr("value"),
            dataType: 'json',
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            data : $(this).serialize(),
            success: function (data) {
                    let teacher = Object.entries(data.teacher);
                     teacher.forEach(item => { $(`[name=${item[0]}]`).val('item[1]'); });
            },
        });
    });
});

view: it contains table to show list of teacher with edit, and form under table with update button. My data is being updated, but i don't want page reload. maybe something to do with append?



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

Aucun commentaire:

Enregistrer un commentaire