Javascript Laravel/Ajax在更新后从数据库中获取值
我有一个编辑表单,管理员可以在其中编辑书籍详细信息。提交此表单后,数据库中的值将更新,页面应将更新后的值加载到表单中(无需刷新/重新加载页面) 我将输入的值更新到数据库中,但它们不会加载到输入字段中。。我的代码只是获取“旧”值并将其粘贴回字段中 为了一步一步地解释它,我们将使用“Title”作为字段来更改示例。 我有一本书叫《旧书》。我在编辑表上输入了一个新标题“新标题”。我点击“提交”按钮。页面会卷回顶部(应该如此),但输入框仍然显示“旧标题”。如果我随后刷新页面(F5),输入框现在显示“新标题” 我希望上述情况发生,而不必刷新页面 下面是Javascript/Ajax的编辑页面:Javascript Laravel/Ajax在更新后从数据库中获取值,javascript,php,jquery,ajax,laravel,Javascript,Php,Jquery,Ajax,Laravel,我有一个编辑表单,管理员可以在其中编辑书籍详细信息。提交此表单后,数据库中的值将更新,页面应将更新后的值加载到表单中(无需刷新/重新加载页面) 我将输入的值更新到数据库中,但它们不会加载到输入字段中。。我的代码只是获取“旧”值并将其粘贴回字段中 为了一步一步地解释它,我们将使用“Title”作为字段来更改示例。 我有一本书叫《旧书》。我在编辑表上输入了一个新标题“新标题”。我点击“提交”按钮。页面会卷回顶部(应该如此),但输入框仍然显示“旧标题”。如果我随后刷新页面(F5),输入框现在显示“新标
$(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=。。。
以此类推。在编辑页面成功块中使用$this只会给出一个错误
未定义的属性:illumb\View\Engines\CompilerEngine::$title(视图:C:\wamp64\www\resources\views\admin\editbook.blade.php)
。在控制器中使用它来声明变量只会产生500个内部服务器错误$this->$title=$request->input('title')代码>