I am currently learning how to utilize ajax to post information to an external page and jquery to then refresh specific elements on the page based on the processing of the information posted. I am using the bootstrap-ajax plugin to get started with this and everything is working as expected. However, I'm thinking there has to be a better method or practice for creating the server side html then the method I am using.
This is how the ajax plugin works:
You start with an initial php page and define the forms that are to be handled by the ajax plugin and the div's that you want to either replace or refresh once the data from the form has been posted.
The plugin submits the data using ajax to a specified page, which then responds with a json encoded variable consisting of {'divToReplace'=>'renderedHTMLusedForReplacement'}
My current workflow consist of creating the initial php code for a div that will be updated, which typically looks like this:
<div id="<?php echo $itemID; ?>" class="<?php echo $thisDivClass; ?>">
<div class="span5">
<h4><?php echo $innerTitle;?></h4>
.......
..........
I then have to copy and modify that div's code and create an additional function that will process the php and then save the html into a variable like this:
$divHtml='<div id="'.$itemID.'" class="'.$thisDivClass.'">
<div class="span5">
<h4>'.$innerTitle.'</h4>';
Needless to say I'm having to do this with a MUCH larger amount of code and it just seems like there must be a better method for somehow rendering the original code and saving the html to a variable without having to copy and modify the entire set of code. I am rather new to ajax, jquery, and json, and I am slightly above novice grade on php so I very well may be going about this process completely wrong or missing something very basic.
Any help, advice, or suggestions would be highly appreciated. Thanks!