Jump to content

[SOLVED] Drop down list set $_SESSION language upon selection HowTo


gasma1975

Recommended Posts

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,

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$_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

}

Link to comment
Share on other sites

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

}

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<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.

Link to comment
Share on other sites

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 !';

}

?>

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.