0
我有一個編輯表單,管理員可以在其中編輯書籍的詳細信息。提交此表單後,數據庫中的值將更新,並且頁面應該將更新後的值加載到表單中(不刷新/重新加載頁面)。Laravel/Ajax從數據庫獲取更新後的值
我有輸入值在數據庫中得到更新罰款,但他們沒有得到加載到輸入字段..我的代碼只是抓住「舊」的價值觀,並把它們粘回領域..
只是爲了一步一步解釋它,使用「標題」作爲字段,我們將改變爲例子。 我有一本名爲「舊書」的書。我在編輯表格上輸入了一個新標題「新標題」。我點擊「提交」按鈕。頁面滾動回頂部(如同它應該那樣),但輸入框仍顯示「舊標題」。如果我然後刷新頁面(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'));
}
}
任何幫助,不勝感激!
在編輯頁面中使用$ this成功塊只會給出一個錯誤'Undefined property:Illuminate \ View \ Engines \ CompilerEngine :: $ title(View:C:\ wamp64 \ www \ resources \ views \ admin \ editbook。 blade.php)'。在控制器中使用它來聲明變量只會給出500內部服務器錯誤'$ this - > $ title = $ request-> input('title');' – WolfieeifloW