- Build a php file that returns only the information we need.
- Directly parse the PHP output to send to the network only the necessary information.
The first case is optimal in terms of load and generated traffic since only the necessary information and generates no extra database queries, but it has a drawback: the maintenance, any change in the application logic must be made duplicate in conventional web and the script that sends the AJAX part. And this is a problem especially in large applications. The second case solves this problem because the same code and starting over to get a variable, PHP only returns the necessary contended eliminating unnecessary traffic. If we use some form of caching the output of PHP, as in our case, we eliminate the extra visits to make up the rest of the page will not need. Only penalize memory load and CPU required to parse the XHTML server. We opted for the second option and for this we use http://simplehtmldom.sourceforge.net/ a tool that allows us to work with the DOM in a similar way as we do with JQuery. In our PHP just have to add the following lines:
- ) . load ( "nuestraPagina.php?selector=#seccionACargar" ) ; $ ("# SeccionACargar") load ("# selector = nuestraPagina.php seccionACargar?").;
This loading nuestraPagina.php? Selector = # seccionACargar the php will not return all the HTML of the page if not we will return only the content selector saving a good candidad of bytes (or Kb) in the transmission of data. As I said this is a big savings over the alternative offered by jquery
- ) . load ( "nuestraPagina.php #seccionACargar" ) ; $ ("# SeccionACargar") load ("# seccionACargar nuestraPagina.php.");
Jquery gets as full page and then performs the parsing. We have successfully used this technique in www.berroguetto.com to load each of the sections of the website, and member of them, like the news and each of the pages pagers.