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


Laravel/Ajax Grab Values From Database After Update

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

我让输入的值在数据库中得到更新,但它们不会加载到输入字段中。。我的代码只是获取"旧"值并将它们粘贴回字段中。。

为了逐步解释,使用"标题"作为我们将为示例更改的字段。我有一本名为《旧书》的书。我在编辑表单上输入了一个新标题"新标题"。我点击"提交"按钮。页面滚动回顶部(应该是这样),但输入框仍然显示"旧标题"。如果我刷新页面(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'));
    }
}

非常感谢您的帮助!

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

    $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=。。。等等。