Reload page and change $_SESSION variable

Cesar Aracena
Cesar Aracena used Ask the Experts™
on
Hi all,

I am a complete stranger to JS so forgive me please.

I wanted to include an image + text dropdown (combo box) on a page for language selection and I did using jquery and a plugin called msDropDown. What I don't know is how to make the page reload altering the $_SESSION["lang"] at the same time.

So far I've achieved something usefull like this:

<select name="languageSelector" id="languageSelector" style="width:150px;" onchange="window.location.href='<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>?lang='+this.value;">
                    <option <?php if($_SESSION["lang"] == "en") echo "selected"; ?> value="en" title="../images/flags/lang-eng.gif">English</option>
                    <option <?php if($_SESSION["lang"] == "es") echo "selected"; ?> value="es" title="../images/flags/lang-esp.gif">Espa&ntilde;ol</option>
                </select>

Open in new window


but I preffer not to have that ?=lang=xx appended to the URL so I'm working just with $_SESSION and cookies. How should I change my "onchange" event to both reload the page and change the value in $_SESSION?

BTW here's my code for the plugin. I left it unaltered as I want it only to show the nice dropdown but maybe the events should be handled in there?

<script language="javascript" type="text/javascript">
$(document).ready(function(e) {
	$("#languageSelector").msDropDown();
});
</script>

Open in new window


Any help would be appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
What I would probably do is create a form around that drop down that does a POST to the same page that you're currently on.  Should be able to just do <form method="post" id="languageForm"> and that will post to the current page.  Then, add some PHP to the top of the page that checks to see if a POST has occurred, and if it has, then get the value of the drop down list and set the appropriate session variable.  You can bind an onChange event to the drop down list so that as soon as it changes it will post the form.

Let me know if you need more detail / examples.

<script language="javascript" type="text/javascript">
$(document).ready(function(e) {
  $("#languageSelector").msDropDown();
  $("#languageSelector").onChange($("#languageForm").submit());
});
</script>

Open in new window

Cesar AracenaPHP Enthusiast

Author

Commented:
All right! Thanks a lot ;)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial