son.of.the.morning Posted November 25, 2011 Share Posted November 25, 2011 Alright guys, am trying to build a basic PHP commenting system but i would like to add some Jquery/Ajax in the mix so the page doesnt need to reload itself when each comment it posted. Does any one have any simple solutions? this is the project... http://dvplus.webuda.com/oophptest/database.query.php Quote Link to comment https://forums.phpfreaks.com/topic/251794-jquery-ajax-call-to-reload-a-specific-div/ Share on other sites More sharing options...
joe92 Posted November 25, 2011 Share Posted November 25, 2011 I'm doing an extremely similar thing on a side project of mine, but I haven't implemented it yet so I can only say how I'm planning to go about it. Storing all comment divs within a container div, id=container: 1. Use javascript to do very simple checks like to make sure it isn't blank etc. 2. If checks are ok, store the required details in javascript variables 3. Start an Ajax POST request to send the message and use the GET to send small length details like the name. 4. Update the mysql database and if it was successfully updated send back a success message to the javascript, if failed, a failed one. 5. If successful, create a div in javascript using var newdiv = document.createElement('div'); 6. Set the class/style and any js events to the newdiv and using the details stored earlier set the innerHTML of the newly generated div to be that of the message (and any other details that are needed) 7. Using the following, append the container div with the newly created message div var divToAppend = document.getElementById('container'); divToAppend.appendChild(newdiv); (I create a var because I intend to append multiple items, if only one is needed you could execute that on one line) 8. If it failed on the update just alert them saying so. Hope this helps, Joe P.s. I like your website design. Quote Link to comment https://forums.phpfreaks.com/topic/251794-jquery-ajax-call-to-reload-a-specific-div/#findComment-1291177 Share on other sites More sharing options...
son.of.the.morning Posted November 25, 2011 Author Share Posted November 25, 2011 Thanks Joe, am going to go through these steps in a short while just going to create the PHP code to update the database first. I was thinking if the comments are loaded dynamically along with the new div, the JavaScript can then display the variables in the mark-up rather than the php but because the php have already stored these variables within the database then if the page was ever to be full reloaded then the php with come into place and display them directly from the database. Is this what you had in mind? Quote Link to comment https://forums.phpfreaks.com/topic/251794-jquery-ajax-call-to-reload-a-specific-div/#findComment-1291181 Share on other sites More sharing options...
joe92 Posted November 25, 2011 Share Posted November 25, 2011 Yeah, that was my thinking as well. Once the message is in the database the php will write it to the page on next load. The javascript is to please the user who just sent the message by removing the need for a complete page reload. Quote Link to comment https://forums.phpfreaks.com/topic/251794-jquery-ajax-call-to-reload-a-specific-div/#findComment-1291183 Share on other sites More sharing options...
son.of.the.morning Posted November 25, 2011 Author Share Posted November 25, 2011 Yea, the logic create's the same Illusion as it would if the query was re-processed. Quote Link to comment https://forums.phpfreaks.com/topic/251794-jquery-ajax-call-to-reload-a-specific-div/#findComment-1291188 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.