简体   繁体   English

我从服务器获取了一个包含数据的div,使用ajax,该div将替换为我用jquery和ajax提交到php路由的表单

[英]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.

相关问题 我已成功将表单加载到<div>使用 jQuery $.Ajax 的元素。 我需要知道如何使用 jQuery Ajax 提交该表单 - I have successfully loaded a form into a <div> element using jQuery $.Ajax. I need to know how to submit that form using jQuery Ajax 我如何使用Ajax和Jquery将数据加载到div中,然后插入表单数据并将数据重新加载到div中? - How can I use Ajax and Jquery to load data into a div then, insert form data and reload the data into the div? Ajax-MySql-Php问题(从表单提交文本,通过AJAX发送到php脚本,再返回div) - Ajax-MySql-Php issue (submit text from form, send via AJAX to php script, back to div) 如何从javascript提交Ajax表单并更新目标div? - How do I submit Ajax form from javascript and update target div? 提交折叠div并从div更改图像后的Ajax表单 - Ajax form after submit collapse div and change image from div 如何使用jquery-ajax提交表单数据? - How do i submit form data using jquery-ajax? JS / Ajax刷新PHP包含在表单提交的div中 - JS/Ajax refresh PHP include inside a div on form submit 如何使用jquery将ajax数据加载到div中? - How do I load the ajax data into a div with jquery? ajax表单更新无法从$(“#formName”)。submit()运行的特定div; - ajax form updating specific div not working from $(“#formName”).submit(); Ajax无法将数据从php加载到div - Ajax cannot load data from php to div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM