Question

Evening all, this is really the last piece to my site being ready for testing.

Cant get my head around the correct way of this.

So im building a blogging cms per say.

I have blog posts that are echoed out into their own divs. In this div is also a hidden ckeditor text area.

If the user is logged in, and its their blog, they can see a edit button and when clicked the current text is hidden and the ckeditor is shown.

This is okay and works, when the ckeditor is shown, its also shown with a save button.

On clicking the save button some validation takes place and an ajax call is made to the update.php page where by the blog is updated. Also all good!

However, what i have tried to is when the response from that php page is okay, hide the editor, and update the current content of that div.

So far i have used this:

$("#container").load("#container");

The #container is a main container that holds all the blog information, and it works in sense as the content is reloaded. However, if i go to click the edit button again the text area is not replaced and also the text area has the red border around it suggesting its been submitted, and its empty. I set a red border if its empty.

The .load() seems to be messing with the other scripts on the page.

Am i using the .load() function correctly? And how can i just reload the current div as i think its more effective.

I was thinking of assigning each blog post div its own unique id, and then send that to the php page and send it back along with the response.

And then do e.g.

 $("#container").load(response.blogPostID),

But i just want to know if im going about this in the way i should be, and is also going to be non buggy etc.

Worst case is that i have to perform a page reload after each edit, but it just doesnt feel a slick as i want.

I know the examples talk about loading content from another page and then loading a certain div from that. But the way i have build my site is that its just one query and the blog posts are echoed via a foreach loop.

Just need to know how to reload a single div, on the current page and not affect the other scripts etc.

If needed, i can set up a test account and blog on my site for an example.

Craig!

EDIT: this is the javascript for when the edit button is clicked:

$(".edity").click(function () {
     $(this).parent().find('.edity').toggle();
     $(this).parent().find('.saveupdatebutton').toggle();
     $(this).parent().find('.updatebutton').toggle();
     $(this).parent().find('.editor').toggle();
     $(this).parent().find('.buildtext').toggle();
     $(this).parent().find('.deleteupdatebutton').toggle();
});

This is the ajax call:

if (check) {
            $.ajax({
                type: "POST",
                url: "process/updatepost.php",
                data: $targetForm.serialize(),
                dataType: "json",
                success: function (response) {

                    if (response.databaseSuccess)
                    $("#container #postholder").load("http://url.com/viewblog.php?id=155 #container #postholder");
                    else
                    $ckEditor.after('<div class="error">Something went wrong!</div>');

                }
            });
        }

This is how the data is shown on the page:

<div class="blogtest" id="156">
<form action="process/updatepost.php" class="updatepost" method="post">
<input type="button" class='.$editenabled.' value="Edit">
<input type="submit" name="saveupdatebutton" class="saveupdatebutton" value="Save">
<input type="submit" name="deleteupdatebutton" class="deleteupdatebutton" value="Delete">
<input type="hidden" class="postid" name="postid" value="'.$postID.'">
<input type="hidden" class="editorid" name="editorid" value="'.$ckEID.'">
<input type="hidden" class="pageurl" name="pageurl" value="'.$pageurl.'">
<input type="hidden" name="buildowner" value="'.$buildcreator.'" />
<br>

<div class="text">

<div class="buildtext" id="'.$postID.'">'.$convertedtext.'</div>

<div class="editor"><textarea name="muffin" id="'.$ckEID.'" class="ckeditor">'.$text.'</textarea></div>

</div>

</form>

</div>
Was it helpful?

Solution 2

I wouldnt recommend reloading the entire page with an ajax call every time, the advantage of ajax calls is that you can load seperate parts to save a lot of bandwidth, so you guessed right.

I dont know what update.php returns exactly, if this can only return the entire page it's smart to make a special function that only returns a blog post

Then the call would like something like (a GET request would not be my first choice, that just depends on your system)

$( "#container #blog" + blogPostId ).load( "update.php?q=updatePost&id="+blogPostId );

For a quick fix, just give every post's div a unique id and add that to your load call

$("#container #blogPost"+blogPostId).load("*url* #container #blogPost"+blogPostId);

OTHER TIPS

It's kind of hard to understand what you are trying to do but I'll give it a go...

could you not just use .html() to replace the content inside the div where the editor is. If you have sent the new data to where ever using ajax you should have a variable set or be able to set a variable for the content that you sent to the server and as such want to use in the div.

$("#container").html(what ever you used as data for the ajax request)

or something to that effect.

But it would be a lot easier if we could at least see some form of code snippet.

Hope this helps

I could probably have explained better let me know if it doesn't make sense and I'll try again :)

event delegation is what i was after and it seems like this is the answer:

$(document).on('click','.edity',function(){
    // DO SOMETHING
)};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top