I would suggest a slight methodology change:
- submit the new post to the database via AJAX
- 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.
- 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);
}