jQuery load or better ajax way? - javascript

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!

Related

Using WordPress, have dynamic query, want to avoid caching the results

I do not want to turn off caching for a site, but I do want to avoid caching in some areas. Wondering the best way.
First, I pull data from an API to check the "online" status of an advisor.
Next, I store that (and any other data that may change) in a CPT.
At the same time, I store a random string of characters (that I can sort on later giving the appearance of random order).
I pull the data from the API on every page load, because I need real-time data. This makes me cringe, but I don't know any other way. This part isn't cached.
However, when I display the list of "advisors", I sort them by online status, then the random string. This is meant to give fairness as to who is above the fold, and near the beginning of the results.
Well, that is all generated with PHP, so therefore the resultant HTML is cached.
I read a bit about the WP Rest API, and perhaps that will help with the speed of the query, but that won't help with the cached HTML right?
So, regardless of how I query the data (REST API, WP_Query), am I to assume that I must iterate through the data with JavaScript to avoid it being cached by the Full Page Cache solution of the server?
If I use WP_Query still, and I use PHP to display the results, can I just call the PHP function from JavaScript?
Every page of the site will display some or all of the advisors (ex: homepage 8 advisors, the "advisor" page shows all, the "advisor" category pages, and 4 advisors in the footer of every other page), so it doesn't make sense to turn off caching.
Any direction would be greatly appreciated! Thanks in advance.
Are you not better of doing it with AJAX?
Or, I'm pretty sure there's like a line of PHP that you can add to pages so they won't get cached. Woocommerce sometimes needs this for example. I guess it depends on which caching plugin you are using. Which one are you using?
Or, for example WP Super Cache has an area where you can exclude certain pages from caching. Under Settings -> WP super chache -> Advanced -> Accepted Filenames & Rejected URIs.

Showing html form data on subsequent pages

Page 1, complete a HTML form. Submitting form links to page 2.
Page 2+, shows a story populated by the answers to the HTML form.
I can't find a why to bring back elements of the HTML form to complete gaps in future pages. Should I be looking at trying to use Javascript or to learn some PHP to store and then re-use the form data?
Yes, you will some sort of server side programming, store data locally and then retrieve it with javascript or passing the information to the page using query strings. Maybe other options are possible but those are the things that come to mind right now. Have a look at this answer for an example of the last method I mentioned.

individual variables versus objects

I'm re-developing a very simplistic javascript include I made a while back to be used in several similar html sites. We use a lot of footers on our pages but they tend to be the same copy used over and over but with different dates and other small variations. So, on each page I create an array of variables. These match up with variables found in an external .js file containing a for loop to match up the requested variables that contain paragraphs of footer copy. This eventually is put together to make a div full of footer copy.
My question is this, is it better to send in the specific dates and other variable changes within the copy along with the variable array as one object or should i continue to just send in the variable array alone, create the footer in the html and then insert the dates/changing data in a separate .js file (this is how I've been doing it till now). One way keeps all the data together and seems pretty clean, but the other separates out the different steps (ie step one is creating the footer, the second step puts in the varying info like a date or bonus amount, etc).
sorry if this is confusing, hope i got the basic idea across. Just trying to explore my options since I have the unique opportunity of previous usage of this script and getting a clean slate to make a fresh version.
I think the best way to do this is on the server-side code.IF you are using php use could include the file directly like this.What include does is it pull code from other files and shows it on the page where the include is written.
include('name of file');
Since you have different pages with different footers you could edit this footer file to show different results for different pages.This is a standard way to doing things.Popular frameworks like Wordpress do something similar to this to display the footer

Controller and View for creating one-to-many object, both "container" and unlimited number of "content" objects?

Users will be able to write some documents. Those documents will consists of chapters (one-to-many relation).
Normally I would do this by creating separate views for creating chapter and document.
How to implement web page that allow to edit "composite" view? Where I can edit document details, but also create chapters, without visiting different pages? Also how can I ensure that I pass order of chapter user have arranged (by moving chapters freely up and down)?
(Sorry if that question already have be asked&answered but I do not even know how to search for it :| since I do not know proper keywords beyond "AJAX", so help in naming my requirement would also be welcomed!)
Backend servers applications based on REST principles work nicely with Ajax client-side implementations.
For example, your URLs could be:
/book/1
/book/1/chapters
/book/1/chapter/1
You could set it up so that a POST to /book/1/chapters would add a chapter. A GET on that same URL would return all chapters. A GET on /book/1/chapter/1/ would only return chapter 1. A PUT on /book/1/chapter/1/ would update an existing chapter. This is a "RESTful" architecture:
http://en.wikipedia.org/wiki/Representational_state_transfer
This is an interesting introduction: http://tomayko.com/writings/rest-to-my-wife
This is a big subject, but if you create the right backend server architecture you will find your job a lot easier. Hope this helps answer your question.
Ok Partial solution.
Just google Nested Forms Ruby on Rails. Plenty of examples, all in ajax, all easy.

Pass GET variables without the URL?

Suppose I have these two links on my homepage
http://example.com/user?id=1
http://example.com/user?id=1&sex=male
These two links point to the same page. The users page loads information based on the id.
If the second link is clicked the page loads extra information based on sex.
These two pages are almost identical, therefore not good for SEO.
My question is it possible to pass the second argument without using the url?
Maybe using POST or javascript?
Thanks
Using POST is the answear. Even though I have no idea, how this influences any SEO strategies...
The only other way to pass it without GET would be POST or by using cookies. They may be other methods that I am missing, and please correct my if I am wrong.

Categories