Space_monkey Posted February 15, 2011 Share Posted February 15, 2011 Although this is the PHP coding section i couldn't think of a better place to post this, it has more to do with theory or a way for me to achieve what I want rather than a piece of code which I cant get to work. I have been working on this project, sort of a note taking application. When a new note is added to the database, after the ajax, the page is refreshed and then the new note appears along with the note title in the navigation pane. My problem is when I put the site up on my web server it runs more slowly so when the page is refreshed after the ajax the data is not yet in the database so it needs a second manual refresh for the data to appear. I tried delaying the refresh a tenth of a second and it sort of worked but I ran into problems when actually trying to implement it. But after getting frustrated, I have decided that my refresh workaround isn't very elegant. Is there a technique or method I could use to have certain parts 'refresh' without actually refreshing? I'm sure it is possible and probably quite simple I can think of lots of times when this sort of thing would be needed. Even just making a comment on facebook the whole page isn't refreshed, it just appears. Or rather somehow knows there is new data in the database and updates its self. But this is my first real php project so I'm still learning how everything is done. Quote Link to comment Share on other sites More sharing options...
fxuser Posted February 15, 2011 Share Posted February 15, 2011 do the ajax request , echo the the new note when you insert it into the database via the url of the ajax request and then add $("#div").after(data) or $("#div").before(data) on ajax sucess? Quote Link to comment Share on other sites More sharing options...
Space_monkey Posted February 15, 2011 Author Share Posted February 15, 2011 Sorry didn't quite understand that. I'm still trying to understand all this. The ajax that I use is actually jQuery and looks like this var level1content = $('#level1content').html(); var level1title = $('#level1title').html(); var noteid = <?php echo $level1id; ?>; $.ajax({ url: 'savelevel1.php', type: 'POST', data: { level1content: level1content, level1title: level1title, noteid: noteid, } }); Then in savelevel1.php I insert it into my database. Then after refreshing it shows up when the database is queried again. I know there is a success option I can use to call a function on success but I'm not sure how that would help. Quote Link to comment Share on other sites More sharing options...
fxuser Posted February 15, 2011 Share Posted February 15, 2011 on savelevel1.php after you insert a new row in the database , echo the result and on the ajax , add the sucess function and inside it add ("#mydiv").after or .before if you want the result to appear after the div you show the notes or before and then add (result) which is the success function(result) Quote Link to comment Share on other sites More sharing options...
Space_monkey Posted February 15, 2011 Author Share Posted February 15, 2011 Sorry still don't quite get it. I probably seem like an idiot because they way your explaining it, it sounds quite simple. I don't understand how echoing $result would help because it only gives info on the result of the query eg. "query empty" and that page isn't ever viewed, its just used by the ajax function which is on member.php Unless I'm thinking of the wrong function. Quote Link to comment Share on other sites More sharing options...
fxuser Posted February 15, 2011 Share Posted February 15, 2011 for example lets say i have a "how are you today" feature with an inputbox and a button then i have a file that does php stuff to show what i have typed in a div(i use some html code to show the message , time , name and stuff, right?) and include it in my div which shows my top10 stories. when i click the button i use ajax to insert it into the database. inside the ajax file that i call after the insert Query i would copy the html code i use for the structure of the post and insert it after the query in order to show the one i posted after or before the div via this code: after success: function(data){ //show my new post after the div or before? $("#mydiv").before(data); //shows what i just typed on the top of the div $("#mydiv").after(data); //shows what i just typed on the bottom of the div } thats how i do it for such a feature.. maybe there is someone better at this than me which can explain it better. Good luck Quote Link to comment Share on other sites More sharing options...
Space_monkey Posted February 15, 2011 Author Share Posted February 15, 2011 I still don't quite get it but perhaps I will sleep on it and maybe do some more reading tomorrow to see if I understand it better. Thanks for your help! I'm sure ill get it eventually. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.