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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看