douchensou6495 2010-08-12 20:52
浏览 69
已采纳

PHP / jQuery:插入后的Fadein / refresh

i have a "wall" on each profile, and i wish to make it smarter, so you don't need to update the page to view your inserted message you just put up.

When you insert a message, it makes a ajax call and inserts to the database, and you receive a message about it has been inserted. But right now you need to refresh the page to see your inserted message. What i want to do is if you insert a new message, it should fadein /refresh the wall with messages that is right under the form. How can I do this?

I have worked on it alittle and tried to make a new file, inserted all coding to show comments and then i set timeout to refresh each 2 seconds

 function ajax_update() {
    var wrapperId = '#box';
    var profileID = document.getElementById('profileID');
    var postFile = 'showcomments.php?id='+ profileID.value;

    _v++;
    _v2++;

    $.post(postFile, { v2: _v2 , v: _v},
      function(data){
        $(wrapperId).html(data);
      });
    setTimeout('ajax_update()', 2000);
  }   

but this isnt good, as it makes too many server calls, so hope you can help me out, since i dont know how i should do this in a cool way

Form with ajax call: http://phpbin.net/x/838833504

And my current php code that grab from db and list in messages: http://phpbin.net/x/2145692361

  • 写回答

2条回答 默认 最新

  • dph19153 2010-08-12 21:07
    关注

    I would suggest a slight methodology change:

    1. submit the new post to the database via AJAX
    2. in the success callback for that AJAX post, create an element with the content that was submitted and append it to the list of posts on the page.
    3. if you want it to look cool just use some of the built in animation effects (fadeIn, show, etc).

    This way, you're not polling for changes all the time, and you only have to request things from the server upon page loads.

    function DoWallInsert(){
    
      var wrapperId = '#box';
      var profileID = document.getElementById('profileID');
      $("#insert_response").html("Laddar..");
    
      $.ajax({ 
       type: "POST",
       url: "misc/insertWall.php",
       data: {
           value: 'y',
           BuID : $('#BuID').val(),
           uID : $('#uID').val(),
           message : $('#message').val() 
       },
       success: function(msg){
           // in here you will have to add the message to the top of the list of wall posts
           // to do this you use prepend whatever html and the message in whatever way you 
           // are using to display the messages.
           $(wrapperId).prepend("<div>" + $('#message').val() + "</div>");
       }
     });
    }
    

    html might look like this before:

    <form action="javascript:DoWallInsert()" method="post">
        <input name="message" type="text" id="message" value="" size="40">
        <input type="hidden" name="BuID" id="BuID" value="123123">
        <input type="hidden" name="uID" id="uID" value="53425">
        <input name="submit" type="submit" id="submit" value="Skicka">
    </form>
    
    <div id="box">
        <div id="post-1">Some stuff</div>
        <div id="post-2">Some other stuff</div>
    </div>
    

    html should look like this after:

    <form action="javascript:DoWallInsert()" method="post">
        <input name="message" type="text" id="message" value="" size="40">
        <input type="hidden" name="BuID" id="BuID" value="123123">
        <input type="hidden" name="uID" id="uID" value="53425">
        <input name="submit" type="submit" id="submit" value="Skicka">
    </form>
    
    <div id="box">
        <div>Whatever was typed in the box</div>
        <div id="post-1">Some stuff</div>
        <div id="post-2">Some other stuff</div>
    </div>
    

    If the html you want to append to the list of posts has php in it then my best suggestion is to return the html for the new div in the response from the server on the on the AJAX call to this: misc/insertWall.php

    insertWall.php should return "<a href='profil.php?id=".$userinfo[id]."'>".$userinfo["full_name"]."</a>". then you can process it and display it in the success part of DoWallInsert():

       success: function(msg){
           // in here you are receiving a response, you should display it in the page
           // this assumes that you are fully formatting the message before returning it
           // and you just want to insert it here.
           $(wrapperId).prepend(msg);
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding