gasma1975 Posted January 6, 2008 Share Posted January 6, 2008 Hello, I want to create a multi language website (english & french) I will have a dropdown list with 2 values <FORM NAME="Langform"> <SELECT NAME="language"> <OPTION VALUE= 1 >English <OPTION VALUE= 2 >French </SELECT> </form> How to automatically, upon change, set the $_SESSION['language'] to the selected value ? and reload the page with the new language ? thank you, gasma1975, Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/ Share on other sites More sharing options...
chronister Posted January 6, 2008 Share Posted January 6, 2008 sounds to me your looking at ajax... in order to have a jump box like this you have to have javascript, to set the session var, you have to have PHP... so AJAX sounds like the way to go. I am not that familiar with javascript and it's use in AJAX so I may be wrong. the easiest way to do it would be add a button and do it all in PHP call the button changeLanguage <FORM NAME="Langform" method="post"> <SELECT NAME="language"> <OPTION VALUE= 1 >English <OPTION VALUE= 2 >French </SELECT> <input type="submit" name="changeLanguage" value="Submit"> </form> <?php if(isset($_POST['changeLanuage'])) { $lang=$_POST['language']; session_start(); $_SESSION['language']=$lang; } ?> thats the way I would do it.... nate Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/#findComment-431590 Share on other sites More sharing options...
revraz Posted January 6, 2008 Share Posted January 6, 2008 $_SESSION['language']=$_POST['language']; if ($_SESSION['language']==1) { //English picked, load English page } elseif ($_SESSION['language']==2) { //French picked, load French page } else { //Neither picked } Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/#findComment-431591 Share on other sites More sharing options...
gasma1975 Posted January 6, 2008 Author Share Posted January 6, 2008 Revraz, Do I need a sbmit button with your code ? how does it work upon a change in the drop down list ? $_SESSION['language']=$_POST['language']; if ($_SESSION['language']==1) { //English picked, load English page } elseif ($_SESSION['language']==2) { //French picked, load French page } else { //Neither picked } Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/#findComment-431601 Share on other sites More sharing options...
revraz Posted January 6, 2008 Share Posted January 6, 2008 Yes Revraz, Do I need a sbmit button with your code ? how does it work upon a change in the drop down list ? Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/#findComment-431602 Share on other sites More sharing options...
gasma1975 Posted January 6, 2008 Author Share Posted January 6, 2008 Good it works ! One more thing, how do you fix the value in the Drop down list to the value that you have selected ? I mean, if I click the option 2 french, after I submit the value french is loaded, but in the drop down box I see english, I would like to fix it to french... thank you for helping gasma1975 Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/#findComment-431610 Share on other sites More sharing options...
chronister Posted January 6, 2008 Share Posted January 6, 2008 <FORM NAME="Langform" method="post"> <SELECT NAME="language"> <OPTION VALUE= 1 <?php if($_SESSION['language']==1){echo 'selected="selected"';} ?> >English <OPTION VALUE= 2 <?php if($_SESSION['language']==2){echo 'selected="selected"';} ?>>French </SELECT> <input type="submit" name="changeLanguage" value="Submit"> </form> that should work. Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/#findComment-431614 Share on other sites More sharing options...
gasma1975 Posted January 6, 2008 Author Share Posted January 6, 2008 This code works fine...(sometimes not sure why I have to submit twice for the language to take effect) But still, I would like to fix the value in the dropdown list to the selected value. This code is in my page.php <?php session_start (); ?> <form name="Langform" method="post"> <select name="lang"> <option value = 1 >English <option value = 2 >French </select> <input type="submit" name="ChangeLanguage" value="Go"> </form> <?php $_SESSION['lang']=$_POST['lang']; if ($_SESSION['lang'] == 1) { //English picked, load English page echo 'ENGLISH !'; } elseif ($_SESSION['lang'] == 2) { //French picked, load French page echo 'FRENCH !'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/#findComment-431619 Share on other sites More sharing options...
chronister Posted January 6, 2008 Share Posted January 6, 2008 I would start with valid HTML. Give the form an action. Close the option tags, wrap the attributes in " " and the following code should be wrapped in the if statement I gave before. Every time this page loads, it is looking for $_POST['lang']. This could cause errors and most certainly causes a warning if error reporting is set to E_ALL. <?php $_SESSION['lang']=$_POST['lang']; if ($_SESSION['lang'] == 1) { //English picked, load English page echo 'ENGLISH !'; } elseif ($_SESSION['lang'] == 2) { //French picked, load French page echo 'FRENCH !'; } ?> Try it like this.. <?php if(isset($_POST['ChangeLanguage'])) { unset($_SESSION['lang']); //unset this so we can set it to what we want with no conflicts $lang= $_POST['lang']; // make the var a more friendly name to work with if ($lang == 1) { //English picked, load English page $_SESSION['lang']=1; } elseif ($lang == 2) { //French picked, load French page $_SESSION['lang']=2; } } ?> This will only run when the submit button is clicked. I would do it this way as, you want to check to see if the form has been submitted, then you want to compare the submitted value to 1 or 2 and then depending on what was submitted, set the session var. You then use the session var to determine what language has been selected. As far as making the selected language appear in the list once selected.... I gave you that code already... <OPTION VALUE= "1" <?php if($_SESSION['lang']==1){echo 'selected="selected"';} ?> >English</option> You have to compare the session var to the option value, if they match, echo a selected="selected" attribute. Nate Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/#findComment-431644 Share on other sites More sharing options...
gasma1975 Posted January 6, 2008 Author Share Posted January 6, 2008 Thx a lot... Now it works perfectly.. gasma1975 Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/#findComment-431828 Share on other sites More sharing options...
revraz Posted January 6, 2008 Share Posted January 6, 2008 Mark as Solved please Quote Link to comment https://forums.phpfreaks.com/topic/84685-solved-drop-down-list-set-_session-language-upon-selection-howto/#findComment-431870 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.