Now I know that I am at risk here of asking a duplicate question, however I don’t really know what to search for because I am a complete AJAX jQuery noob. Believe me, I have tried searching what I think is the obvious, with no luck so please go easy on me.
I have a php wordpress site which shows prices in GBP as default. At the top, is a select box with onchange="this.form.submit()"
which allows the user to change the default currency that all prices are quoted in.
<form method="post" action="">
<select name="ChangeCurrency" onChange="this.form.submit()">
<option value="GBP">GBP</option>
<option value="USD">USD</option>
<option value="EUR">EUR</option>
</select>
</form>
On the home page, are several, what I call “shortcode widgets”, each one containing products and price tables. A dashboard if you like.
How it currently works (inefficient):
- User changes select.
- Form submitted
- Homepage reloaded with updated prices in selected currency.
This is not good, because whenever somebody changes currency, the whole page is reloaded (this takes time, transfers approx 1mb without caching, not to mention unnecessary load on the server).
What I want (more efficient):
- When the select box is changed, I wish to asynchronously post the form which changes the currency session variable.
- Each “shortcode widget” is updated one by one without having to reload the entire page.
Is this something that jquery can do? where do I start?
Just in case it makes any difference so that you can see what I mean, here is the URL so that you can see what I am talking about… http://bit.ly/10ChZys
PART 2:
I have used jQuery and ajax to update the fixTable thanks to a mashup of answers below… I am using session variables to store the users choice, that way, if they return to the site, the option will be saved.
I am having problems with my code because the session variable stored within http://goldealers.co.uk/wp-content/plugins/gd/tables.php?currency=GBP&table=fixTable appears to have a different session_id to the user’s session id because the option is no longer stored.
Is there a way of telling the server that they are one and the same session?
SOLUTION
I used Ribot’s Solution to start with which worked and solved the initial problem, then extended with NomikOS’s solution…
NOTE: this answer show some ideas about the php backend for the AJAX process. It is a complement for the other answers talking about the frontend process.
1.- a mockup to manage AJAX request in WP, just some ideas, ok?
2.- don’t forget security
3.- in general the needed in the frontend (to use with the backend mockup showed above) is something like:
4.- You can use
$.get()
or$.post()
to send/process data to/in server but.load()
is not good when you update DB because you can’t manage returning messages of failures with the precision of ajson
response (for example: multiples validation error messages). Just use.load()
to load HTML views.UPDATE:
Set
session_id()
where can be executed both for normal requests and for ajax requests and at the early stage as possible. I hope you are using a class to wrap your plugin, if not now is the right moment to do it… example:UPDATE 2:
About nonce based security:
In this mockup
nonce_my_ajax
is just an example, indeed it should be more unique likenonce_{my_plugin_name}
, or even betternonce_{my_plugin_name}_{what_action}
wherewhat_action
representsupdating user
, orinserting new book
, etc…More info about it: WP Codex: WordPress Nonces, WPtuts+: Capabilities and Nonces.
Yes, jQuery can do it using ajax.
First of all, when using ajax, you don’t have to post a form to get the data. Ajax in jQuery will load the text data of an url.
You may start by giving your select an id (here id=”changeCurrency”) and:
Now read up on jQuery and ajax for what kind of ajax call you need to do that suites your needs the best.
Drop the onchange and add an ID
On the page give all your prices a price in your base currency as well as outputting them
In your JS have a conversion table
and bind an event to the change
I haven’t checked any of this code