0

I am creating a product overview page via PHP and want to add detail information via AJAX. I am using the jQuery .load-function. The thing is I want to add detail information at three different places, each having a different format, while the detail data being about the same.

I wanted to be smart and created one PHP page where the database connection and query for the details are established and the three different kinds of information formats are created and then referenced each information format using the load function's ability to address page fragments, i.e. something like

$("#123").load(bestprice.php?id=123 #part1")
$("#123").load(bestprice.php?id=123 #part2")
$("#123").load(bestprice.php?id=123 #part3")

It works, but of course my hope that some sort of cache would arrange for the page to be loaded only once was disappointed. It loads three times for each id.

So, my questions are: a) Is there a better way for reaching my goal, i.e. creating an array in the loaded page containing the data that I can use in my original page with doing the formatting there? How would I hand it over? b) Would it be possible to load the page only once and address the parts with some kind of processing in my original page?

Please, don't answer only yes. Consider me a newbie who needs as much explanation as possible. Code samples are greatly appreciated!

heckerf
  • 33
  • 1
  • 7
  • what does your php code return? HTML, JSON, etc... – Neo Nov 16 '16 at 15:33
  • What I would do is use jquery to load your page into a hidden div.. You can then extract the sections and put them were you want, again you could use jquery to do this. – Keith Nov 16 '16 at 15:33
  • If you want to load all the three segments at the same time you can just do an ajax call and update the three items at the same time. If you need them in different moment I don't see many other options – Lelio Faieta Nov 16 '16 at 15:34
  • 2
    A better implementation would be to make a single request conatining all the ids you want to retrieve (ie `['part1','part2','part3']`), then your PHP retrieves the data and returns JSON. You can then create the HTML on the client. This saves bandwidth, server processing time and should be quicker overall. – Rory McCrossan Nov 16 '16 at 15:34
  • A) Yes, use JSON and load the data with `$.ajax` with the `success` callback. B) Yes, use local storage. – Charlotte Dunois Nov 16 '16 at 15:35

0 Answers0