更新后,Laravel / Ajax从数据库中获取值

时间:2016-04-18 16:58:20

标签: javascript php jquery ajax laravel

我有一个编辑表单,管理员可以在其中编辑图书详细信息。提交此表单后,值将在数据库中更新,页面应将更新的值加载到表单中(无需刷新/重新加载页面)。

我输入的值在数据库中得到了更新,但它们没有加载到输入字段中。我的代码只是抓取“旧”值并将它们重新粘贴到字段中..

只是一步一步地解释它,使用“标题”作为我们将为示例更改的字段。 我有一本名为“旧书”的书。我在编辑表格上输入了新标题“新标题”。我点击“提交”按钮。页面滚动回到顶部(应该如此),但输入框仍然显示“旧标题”。如果我然后刷新页面(F5),输入框现在显示“新标题”。

我希望不必刷新页面就可以实现上述目的。

这是编辑页面Javascript / Ajax:

$(document).ready(function()
{
    $('#editbook_form').bootstrapValidator(
    {
        feedbackIcons:
        {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields:
        {
            eb_title:
            {
                validators:
                {
                    notEmpty:
                    {
                        message: 'You must enter a book title'
                    }
                }
            },
            eb_insoft:
            {
                validators:
                {
                    notEmpty:
                    {
                        message: 'You must enter a value for In Soft'
                    }
                }
            },
            eb_inhard:
            {
                validators:
                {
                    notEmpty:
                    {
                        message: 'You must enter a value for In Hard'
                    }
                }
            }
        },
    })
    .on('success.form.bv', function(e)
    {
        // Prevent form submission
        e.preventDefault();

        // Get the form instance
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');


        title = $("#title").val();
        m_keywords = $("#eb_mkeywords").val();
        m_description = $("#eb_mdescription").val();
        description = $("#eb_description").val();
        electronic_price = $("#eb_electronicprice").val();
        audio_price = $("#eb_audioprice").val();
        soft_price = $("#eb_softprice").val();
        hard_price = $("#eb_hardprice").val();
        in_soft = $("#eb_insoft").val();
        in_hard = $("#eb_inhard").val();
        status_id = $("#eb_statusid").val();
        isbn = $("#eb_isbn").val();
        date_published = $("#eb_datepublished").val();
        notes = $("#eb_notes").val();

        console.log("BEFORE AJAX CALL");

        $.ajax(
        {
            type: "POST",
            //url: base_url+"/book/updateBook",
            url: "[[URL::to('book/updateBook')]]",
            dataType : 'json', // expected returned data format.
            data:
            {
                book_id: window.book_id,
                title: title,
                m_keywords: m_keywords,
                m_description: m_description,
                description: description,
                electronic_price: electronic_price,
                audio_price: audio_price,
                soft_price: soft_price,
                hard_price: hard_price,
                in_soft: in_soft,
                in_hard: in_hard,
                status_id: status_id,
                isbn: isbn,
                date_published: date_published,
                notes: notes,
            },
            success: function(data)
            {
                if(data.valid==true)
                {
                    console.log("DATA VALID IS TRUE");
                    //alert("VALID: " + data.valid + "\nTITLE: " + data.title);


                    $("#edit_err").removeClass('text-danger').addClass('text-success');
                    $("#edit_err").html(data.message);

                    oldInSoft = "";

                    $('#editbook_form').data('bootstrapValidator').resetForm();
                    $('#editbook_form')[0].reset();

                    //location.reload();
                    window.scrollTo(0,0);

                    //$('#eb_message').html("Book successfully updated!");

                    $('#title').html("[[ $book->title ]]");
                    $('#eb_mkeywords').html("[[ $book->m_keywords ]]");
                    $('#eb_mdescription').html("[[ $book->m_description ]]");
                    $('#eb_description').html("[[ $book->description ]]");
                    $('#eb_electronicprice').html("[[ $book->electronic_price ]]");
                    $('#eb_audioprice').html("[[ $book->audio_price ]]");
                    $('#eb_softprice').html("[[ $book->soft_price ]]");
                    $('#eb_hardprice').html("[[ $book->hard_price ]]");
                    $('#eb_insoft').html("[[ $book->in_soft ]]");
                    $('#eb_inhard').html("[[ $book->in_hard ]]");
                    $('#eb_statusid').html("[[ $book->status_id ]]");
                    $('#eb_isbn').html("[[ $book->isbn ]]");
                    $('#eb_datepublished').html("[[ $book->date_published ]]");
                    $('#eb_notes').html("[[ $book->notes ]]");
                }
                else
                {
                    console.log("DATA VALID IS FALSE");
                    $("#edit_err").addClass("text-danger");
                    $("#edit_err").html(data.message);
                }
            },
            beforeSend:function()
            {
                console.log("INSIDE BEFORESEND");
                $("#edit_err").html("Loading...");
            }
        });

        return false;
    });
});

这是控制器:

public function updateBook(Request $request)
{
    $valid = false;
    //$data = Input::all();
    //$message = '';

    $id = $request->input('book_id');
    $title = $request->input('title');
    $m_keywords = $request->input('m_keywords');
    $m_description = $request->input('m_description');
    $description = $request->input('description');
    $electronic_price = $request->input('electronic_price');
    $audio_price = $request->input('audio_price');
    $soft_price = $request->input('soft_price');
    $hard_price = $request->input('hard_price');
    $in_soft = $request->input('in_soft');
    $in_hard = $request->input('in_hard');
    $status_id = $request->input('status_id');
    $isbn = $request->input('isbn');
    $notes = $request->input('notes');
    $date_published = $request->input('date_published');


    $valid = DB::table('book')->where('book_id', $id)->update(
        [
            'title' => $title,
            'm_keywords' => $m_keywords,
            'm_description' => $m_description,
            'description' => $description,
            'electronic_price' => $electronic_price,
            'audio_price' => $audio_price,
            'soft_price' => $soft_price,
            'hard_price' => $hard_price,
            'in_soft' => $in_soft,
            'in_hard' => $in_hard,
            'status_id' => $status_id,
            'isbn' => $isbn,
            'date_published' => $date_published,
            'notes' => $notes
        ]
        );

    if($valid) 
    {
        return response()->json(array('valid' => true,'message' => 'Book successfully updated!'));
    } 
    else 
    {
        return response()->json(array('valid' => false,'message' => 'Book not updated, please fix any errors'));
    }

}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我认为您没有更新您正在使用的对象的旧数据。 您只需创建一个新变量,如下所示。并将它们发送到更新方法。 但是您正在使用旧对象中的旧数据。 $书

    $id = $request->input('book_id');
    $title = $request->input('title');
    $m_keywords = $request->input('m_keywords');
    $m_description = $request->input('m_description');
    $description = $request->input('description');
    $electronic_price = $request->input('electronic_price');
    $audio_price = $request->input('audio_price');
    $soft_price = $request->input('soft_price');
    $hard_price = $request->input('hard_price');
    $in_soft = $request->input('in_soft');
    $in_hard = $request->input('in_hard');
    $status_id = $request->input('status_id');
    $isbn = $request->input('isbn');
    $notes = $request->input('notes');
    $date_published = $request->input('date_published');

尝试更新对象的数据 使用$ this-> title = ... 等等。