JavaScript 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 with data in sql,with data in r,with data

Everything works except for the replacement of the 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. But when i submit the form it will place the div that was replaced by the form.

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:

// 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:

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.

Answer:1

I'm trying to display full month name like 'January 2014' with eyecon Bootstrap datepicker http://www.eyecon.ro/bootstrap-datepicker/ Does anybody know if it's possible? I've tried everything I know (...

I'm trying to display full month name like 'January 2014' with eyecon Bootstrap datepicker http://www.eyecon.ro/bootstrap-datepicker/ Does anybody know if it's possible? I've tried everything I know (...

  1. displayport
  2. display case
  3. display synonym
  4. display cabinet
  5. displayport cable
  6. display driver uninstaller
  7. displayport to hdmi
  8. display fusion
  9. display flex
  10. display shelves
  11. displayport vs hdmi
  12. display css
  13. display board
  14. displaylink
  15. display definition
  16. display shelf
  17. display stand
  18. display settings
  19. display cable
  20. display box

Hi I'm new to Backbone and I was just playing around with it a little, here is my code: var Users = Backbone.Collection.extend ({ url : 'http://backbonejs-beginner.herokuapp.com/users' ...

Hi I'm new to Backbone and I was just playing around with it a little, here is my code: var Users = Backbone.Collection.extend ({ url : 'http://backbonejs-beginner.herokuapp.com/users' ...

I am tyring to append different autocomplete list results in one text box. It is easy to give different text box for different autocomplete results. html : <input id="university" name="...

I am tyring to append different autocomplete list results in one text box. It is easy to give different text box for different autocomplete results. html : <input id="university" name="...

I'm trying to convert a simple string to a unix timestamp using moment.js moment('2014-01-14 07:25 PM').unix(); moment('2014-01-14 07:25 AM').unix(); The problem is I get the same result with AM or ...

I'm trying to convert a simple string to a unix timestamp using moment.js moment('2014-01-14 07:25 PM').unix(); moment('2014-01-14 07:25 AM').unix(); The problem is I get the same result with AM or ...

  1. convert string to simpledateformat
  2. convert simpledateformat to date
  3. converting a simple data type into a class