elite_thut Posted October 20, 2006 Share Posted October 20, 2006 Hi,on my website, I want to have a drop down menu where you can select English or French...Then as soon as you make your choice, it ads a cookie with the value of your choice (so if you come back later it's the good language) and it reloads the page with the good language.But I don't know why, when my page is, for example in English, and I select French, the page reloads but it's in English. But if I reload the page manually then, it will be in French. I hope I was clear...Please help me out with this one. Here's my code... ///////////////// The form ////////////////<div style="color:#f2c114; float:right; position:relative; right:2%;"><form method="post" name="changelang" action="<?php cookie() ?>" style="padding:0; margin:0;">Language: <select name="lang" onChange="submit()" style="font-size:100%"> <option value="no" selected="selected"><u>Select</u></option> <option value="en">English</option> <option value="fr">Français</option>'; </select></form></div>//////////////////////////// The cookie() function////////////////////////////function cookie(){if ($_COOKIE['lang'] == ""){ setcookie("lang", "en", time()+70000);}if (isset($_POST['lang'])){if ($_POST['lang'] == "fr") { setcookie("lang", "fr", time()+70000); }else if ($_POST['lang'] == "en") { setcookie("lang", "en", time()+70000); }}}////////////////////// The index page//////////////////////<?phpif ($_COOKIE['lang'] == "" || $_COOKIE['lang'] == "en"){ require("forums/SSI.php"); require("header.php"); require("rightsidebar.php"); require("content.php"); require("footer.php");}if ($_COOKIE['lang'] == "fr"){ require("forums/SSI.php"); require("header.php"); require("rightsidebar.php"); require("contenu.php"); require("footer.php");}?> Thanks a lot. Link to comment Share on other sites More sharing options...
boen_robot Posted October 20, 2006 Share Posted October 20, 2006 I suggest that alongside the cookie, you use a GET variable on the targeted page. The PHP will throw English if no GET variable is set and no Cookie is found. The GET variable will ensure the first time the page displays correctly and the cookie will ensure future visits will use the selected language. Btw this method also allows the user to bookmark the page in a language of their choise, instead of relying on the cookie. Link to comment Share on other sites More sharing options...
elite_thut Posted October 20, 2006 Author Share Posted October 20, 2006 I suggest that alongside the cookie, you use a GET variable on the targeted page. The PHP will throw English if no GET variable is set and no Cookie is found. The GET variable will ensure the first time the page displays correctly and the cookie will ensure future visits will use the selected language. Btw this method also allows the user to bookmark the page in a language of their choise, instead of relying on the cookie.Yeah...that's a good idea...I'm gona try this right away!------------------------------------------------------------------------------------I tried it... and it worked (almost) I have another problem... At least I found the problem but I'm not able to solve it.Ok...so if my code is like this:if ($_GET["lang"]=="fr" || $_COOKIE["langd3"]=="fr"){ require("forums/SSI.php"); require("header.php"); require("rightsidebar.php"); require("contenu.php"); require("footer.php");}else if ($_GET["lang"]=="en" || $_COOKIE["langd3"]=="en"){ require("forums/SSI.php"); require("header.php"); require("rightsidebar.php"); require("content.php"); require("footer.php");} When I use this code, if I select french, it works the first time, but if I select english it works only if I select it, and then reload the page.But if I use that code: if ($_GET["lang"]=="en" || $_COOKIE["langd3"]=="en"){ require("forums/SSI.php"); require("header.php"); require("rightsidebar.php"); require("content.php"); require("footer.php");}else if ($_GET["lang"]=="fr" || $_COOKIE["langd3"]=="fr"){ require("forums/SSI.php"); require("header.php"); require("rightsidebar.php"); require("contenu.php"); require("footer.php");} When I use that code, if I select english it works the first time, but if I select french, I need to reload the page after for it to work.Can you help me out? PLEASE Link to comment Share on other sites More sharing options...
elite_thut Posted October 20, 2006 Author Share Posted October 20, 2006 SOLVEDOK...I solved my problem. I just used switch instead of if/else if Link to comment Share on other sites More sharing options...
reportingsjr Posted October 20, 2006 Share Posted October 20, 2006 try this, it will be more efficient: if ($_GET["lang"]=="en" || $_COOKIE["langd3"]=="en" || !isset($_COOKIE["langd3"]) && !isset($_GET["lang"])){ require("forums/SSI.php"); require("header.php"); require("rightsidebar.php"); require("content.php"); require("footer.php");}else if ($_GET["lang"]=="fr" || $_COOKIE["langd3"]=="fr"){ require("forums/SSI.php"); require("header.php"); require("rightsidebar.php"); require("contenu.php"); require("footer.php");} that checks if the cookie isnt AND the get variable isnt set. Link to comment Share on other sites More sharing options...
elite_thut Posted October 20, 2006 Author Share Posted October 20, 2006 try this, it will be more efficient:if ($_GET["lang"]=="en" || $_COOKIE["langd3"]=="en" || !isset($_COOKIE["langd3"]) && !isset($_GET["lang"])){ require("forums/SSI.php"); require("header.php"); require("rightsidebar.php"); require("content.php"); require("footer.php");}else if ($_GET["lang"]=="fr" || $_COOKIE["langd3"]=="fr"){ require("forums/SSI.php"); require("header.php"); require("rightsidebar.php"); require("contenu.php"); require("footer.php");} that checks if the cookie isnt AND the get variable isnt set. Yeah..I tryed that...it doesn't work...same problem as before Link to comment Share on other sites More sharing options...
jhecht Posted October 21, 2006 Share Posted October 21, 2006 how many languages are you using? if its only english and french, then just use if($_GET['lang'] etc etc){}else{etc etc}If its only 2, no point in an elseif Link to comment Share on other sites More sharing options...
boen_robot Posted October 21, 2006 Share Posted October 21, 2006 I'm just wondering... why are you suggesting stuff still... I mean SOLVEDOK...I solved my problem. I just used switch instead of if/else ifI do agree that if there are only two languages if/else is enough though anyway. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 23, 2006 Share Posted October 23, 2006 <form method="post" name="changelang" action="<?php cookie() ?>" style="padding:0; margin:0;">Wow, that's a new one. Setting the form action to be a PHP function, I like your style. But you should know that the form action will always be blank, and will always submit to the current page, and also the cookie function will execute every time you load the page, no matter if the form has been submitted or not. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now