[英]I got a div with data from the server, with ajax the div will be replaced with a form that i submit with jquery and ajax to a php route
Everything works except for the replacement of the div. 除更换div之外,一切正常。 When i submit the form with jquery and ajax, my php stores the data in the database and it creates a new div that needs to be placed where the form was.
当我使用jquery和ajax提交表单时,我的php将数据存储在数据库中,并创建了一个新的div,需要将其放置在表单所在的位置。 But when i submit the form it will place the div that was replaced by the form.
但是,当我提交表单时,它将放置被表单替换的div。
The controller: 控制器:
public function postStoryPanel(UpdateStoryRequest $request, $user_id)
{
$provider = new StoryProvider($user_id);
$panel[] = $provider->updateStoryByUserId($request, $user_id);
return Response::json($panel);
}
The StoryProvider: StoryProvider:
// This just stores the data in the database and it wil trigger the method
// for creating the div with the data from database
public function updateStoryByUserId($request, $user_id)
{
$story = User::findOrFail($user_id)->story->last();
if(is_null($story)) {
$story = new Story();
$story->bio_id = Bio::firstOrCreate(['user_id' => $user_id])->id;
$story->title = $request->get('title');
$story->body = $request->get('body');
$story->save();
}else{
$story->delete();
$story = new Story();
$story->bio_id = Bio::where('user_id', $user_id)->first()->id;
$story->title = $request->get('title');
$story->body = $request->get('body');
$story->save();
}
return $this->getStoryPanelByUserId($user_id);
}
// This will create the div with the data from database
public function setEditStoryPanelByUserId($user_id)
{
return $this->storyPanel = new StoryPanel(
$user_id,
$this->setEditHeaderContent($user_id),
$this->setEditBodyContent($this->story),
$this->setEditFooterContent($user_id)
);
}
JQuery and Ajax: jQuery和Ajax:
function postRoute(route, form_id)
{
$.ajax({
type: "POST",
url: route ,
data: $('#'+form_id).serialize(),
dataType: 'json',
success: function (data) {
placePanel(data);
}
});
}
function placePanel(data){
var response = '';
if(data.length > 1){
response = 'multi';
}else{
response = 'single';
}
var panels = [];
var panel_id = [];
if(response == 'multi'){
for(var i = 0; i < data.length; i++){
panels[i] = renderPanel(data[i]);
panel_id[i] = data[i]['panel_id'];
}
}else{
panels[0] = renderPanel(data[0]);
panel_id[0] = data[0]['panel_id'];
}
for(var x = 0; x < panels.length; x++){
injectPanel(panels[x], panel_id[x]);
}
}
function renderPanel(panelObj)
{
var panel = "";
for(var key in panelObj){
if(panelObj[key] != null) {
console.log(panelObj[key]);
if(key != 'panel_id'){
panel += panelObj[key];
}
}
}
return panel;
}
function injectPanel(panel, panel_id)
{
var found_panel = document.getElementById(panel_id);
if(found_panel != null){
$('#'+panel_id).replaceWith(panel);
}else{
$('#bio').append(panel);
}
}
Hopefully I have my question well formulated. 希望我的问题得到了很好的阐述。 Forgive me for my (bad) english my main language is dutch.
原谅我的英语不好,我的主要语言是荷兰语。
I found the fault in my code. 我在代码中发现了错误。 When i created a new
StoryProvider($user_id),
i get in the __construct
the data from the database for my div
. 当我创建一个新的
StoryProvider($user_id),
我从__construct
中获取了来自div
的数据库中的数据。 That's why i got the old div instead of the new. 这就是为什么我得到旧div而不是新div的原因。
Now i get the data from the database when the method is triggered that will create the div. 现在,当触发该方法将创建div时,我将从数据库中获取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.