dqxuiq7772 2014-01-31 19:21
浏览 261
已采纳

正确的方法来重新加载已编辑的博客文章

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>
  • 写回答

3条回答

  • doxrxwn2252 2014-01-31 19:49
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?