I'm building a website that has dual languages with two flags as an entry page. I'm planning on using <form method="post">
around the flags so the user can select the language they want.
Then on the following pages I want to use something like:
<?php
if( $_POST['language']=='uk' ){
echo $uk;
}elseif( $_POST['language']=='french' ){
echo $french;}
?>
So on clicking the flag, they have selected the language they want. Will that only work on the next page after they have clicked the flag or can they carry on navigating to different pages and it still pick up what language was selected?
If that doesn't work, how else can it be done?
UPDATE:
I don't think I made it clear before that I'm using Wordpress, which apparently doesn't like $_SESSION
.
I have this on a template called region.php to submit the language selection:
<form action="<?php the_permalink(); ?>/home" name="region" method="post">
<div id="uk">
<a href="javascript:document.region.submit()" name="UK">
<img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/-uk.png" width="259" height="160" alt="UK" />
</a>
<h1 style="color:black!IMPORTANT;">Enter United Kingdom site</h1>
</div>
<div id="world">
<a href="javascript:document.region.submit()" name="World">
<img style="margin-bottom:10px;" src="<?php bloginfo('stylesheet_directory'); ?>/images/
world.png" width="258" height="160" alt="
Rest of the World" />
</a>
<h1 style="color:black!IMPORTANT;">Enter Rest of the World site</h1>
</div>
</form>
What do I need to put on every other template to check what language was selected? To help with the example if UK has been selected then it can just echo "UK", if the rest of the world was selected then it can just show "World".
This needs to work across several pages so if they goto the about page it checks the language, then if they navigate to the contact page it checks the language again - all that has to come from the initial language selection.
I would drop the selection into a
$_SESSION
variable. That will stay with them until they leave. You could also use a cookie quite nicely. Actually a combination of the two would be great, it ties up users who don't allow cookies to run on a visit to visit basis, and folks who have cookies enables will only have to pick once.Edit: Example of working code:
page that the form redirects to:
yourIndexpage.php
If you are using wordpress you should probably read this.
The $_POST variable contains data contained in single request only (see POST (HTTP)) so they will see appropriate content after "click" or post some form, so after request to your page only.
For more permanent storage of the user settings it's necessary to save these settings to the "sessions" data which are available in $_SESSION variable and are persistent, so will be available until your visitors will browse your pages. More permanent but slightly less pure solution is saving settings to the cookies for which case you can define expiration so they will be used also while next visit.
See some example:
...and elsewhere:
Or you can use cookies:
...and elsewhere:
Answers
Firstly, for adding regional / multilingual support to a wordpress site there is no better alternative than this plugin: http://wpml.org/. It has a cost associated with it, but its not prohibitive (you pay for awesome support).
Secondly, You cannot use $_SESSION by default. WP is stateless by design. That said there are tons of plugins and tutorials online for getting this functionality.
Code stuff
Your original form html had no inputs. It was a form that submitted nothing. This form has two submits named the same thing
location
. So whichever button is clicked will submit its value against the$_POST['location']
.Write a wordpress action to handle the post. Add this to your theme's functions file. look at http://php.net/manual/en/function.setcookie.php for setcookie docs.
Then, anywhere you want to know the location:
ADDITIONAL INFO
I initially clicked UK to start with and this set the cookie, I then went back and clicked World but that didn't copy over the cookie with world, it just showed UK again. Is it possible to wipe over the cookie each time a different selection is made?
So this is an issue regarding how cookies work at a technical level:
The new cookie data is not initially sent, b/c it inst until after the request has been sent from your browser to the server that the new cookie data is saved and available for sending with the request.
How do you make this work then? redirect after a successful set cookie event.
Also is it possible to use the images of the flags as the submit buttons? Yes, use CSS to style your input buttons.
Add an id to each input:
id="uk-button"
andid="world-button"
CSS:
It won't works on another pages. POST variables sends not all the time. It sends only when submit the form. You can use COOKIES for your task. Save the variable there and check it on every page.
You really need sessions/cookies if you want the selection to persist (or keep passing the value through
GET
like?lang=uk
, but that is not very elegant). By the way, you do not need an image within a hyperlink of which the default behavior is blocked and its parent form is submitted with JavaScript. You could simply use something like this:This would work as an image that is also a form submit button.
If you don't want to use cookies you could do it this way.
then when you have 2 buttons, where one is English and the other is German or whatever you desire.
you could use this check to varify what language the page should be.