Forum Moderators: coopster

Message Too Old, No Replies

how to change selected item depending on url

         

lindajames

2:19 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



hi,

i have the following selection form in my php page:

<select name="lang">
<option value="german">german</option>
<option value="french">French</option>
<option value="english">English</option>
</select>

basically what i need to do is make one of those values selected depending on what url they are in. for example if the visitor is in mydomain.com/german/ then then above select form should be like this:

<select name="lang">
<option value="german" selected>german</option>
<option value="french">French</option>
<option value="english">English</option>
</select>

and if the user is in mydomain.com/english htne english should be selected, i need it to also work if the user is in lets say mydomain.com/english/contact/form.html

Any suggestions on how this can be achieved would be appreciated.

Cheers
Linda

Robber

4:25 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



If you have a finite number of URLs that you need to test you could have a look in $_SERVER['REQUEST_URI'] and then do a test on this work out which one should be selected.

vincevincevince

6:13 pm on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<?php
$countries=array("german","french","english");
echo "<select name=\"lang\">";
for($a=0;$a<sizeof($countries);$a++)
{
echo "<option value=\"$countries[$a]";
if (strpos($_SERVER['REQUEST_URI'],$countries[$a])) echo " SELECTED";
echo "\">".ucfirst($countries[$a])."</option>";
}
echo "</select>";
?>

lindajames

6:20 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



thanx vincevincevince, i will try it

lindajames

8:12 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



my mistake, i didnt explain properly, the folder names are short initials but the name in the dropdown menu are full names. e.g.

<select name="lang">
<option value="english">English</option>
</select>

english should be selected if the folder name is "en" so myhost.com/en it should also work if its im currently in myhost.com/en/whatever

thanx
linda

vincevincevince

8:28 pm on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$countries=array("german","french","english");
$domainstrings=array("de","fr","en");
echo "<select name=\"lang\">";
for($a=0;$a<sizeof($countries);$a++)
{
echo "<option value=\"$countries[$a]";
if (strpos($_SERVER['REQUEST_URI'],$domainstrings[$a])) echo " SELECTED";
echo "\">".ucfirst($countries[$a])."</option>";
}
echo "</select>";
?>

[keep the $countries and $domainstrings in the same order]

lindajames

8:37 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



it prints the <select name="lang"> tag but the selected thing doesnt work. any ideas?

vincevincevince

9:03 pm on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you run:
echo $_SERVER['REQUEST_URI'];

what do you get?

lindajames

9:06 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



if i run echo $_SERVER['REQUEST_URI']; i get nothing.

vincevincevince

9:28 pm on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



maybe your php is old?

try changing $_SERVER['REQUEST_URI'] into $HTTP_SERVER_VARS['REQUEST_URI']

lindajames

9:34 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



nope it still doesnt work. i have php4. oh by the way its on windows IIS could that be the problem?

lindajames

10:16 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



i just done a phpinfo(); and it seems that under the PHP Variables section nothing is mentioned about _SERVER["REQUEST_URI"] could this be the problem? if so what are the ways around it? how about setting a variable in my php code e.g. $SID = "uk";

cheers
linda

Timotheos

10:25 pm on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about $_SERVER['SCRIPT_FILENAME']?

lindajames

10:29 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



tried $_SERVER['SCRIPT_FILENAME'] aswell and still doesnt work

vincevincevince

10:52 pm on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i don't know what the problem is. where are you running this? your own server? or a remote server? try asking the ISP whether you can access $_SERVER values via php

lindajames

10:55 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



i tried it on my local machine aswell on my ISP's machine. on my local machine i have php4 installed with apache and it still doesnt work and on my ISP's i got PHP4 on IIS

vincevincevince

12:26 am on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



IIS does not have REQUEST_URI I'm afraid :$ Not sure what you can do about it...

How do people get -> to -> your country specific subdirectory? Maybe they can be given a session variable there to store where they are?

How do you alias your script so it responds from the various directories? (Unfamiliar with IIS...) Maybe there's something you can do in there? If it was apache one could use mod_rewrite to change /en/script.php to /script.php?country=en .... is there something similar to mod_rewrite in IIS?

lindajames

12:32 am on Jul 26, 2003 (gmt 0)

10+ Year Member



yeah i can put a parameter into my script e.g. mydomain.com/en/index.php?lang=en

but how will the select menu work then?

vincevincevince

9:52 am on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<?php
$countries=array("german","french","english");
$domainstrings=array("de","fr","en");
echo "<select name=\"lang\">";
for($a=0;$a<sizeof($countries);$a++)
{
echo "<option value=\"$countries[$a]";
if (strpos($_REQUEST['lang'],$domainstrings[$a])) echo " SELECTED";
echo "\">".ucfirst($countries[$a])."</option>";
}
echo "</select>";
?>

lindajames

10:18 am on Jul 26, 2003 (gmt 0)

10+ Year Member



i tried that but it still doesnt work. i even tried it in the following way but still no luck:

<?php
$lang = "en";
$countries=array("german","french","english");
$domainstrings=array("de","fr","en");
echo "<select name=\"lang\">";
for($a=0;$a<sizeof($countries);$a++)
{
echo "<option value=\"$countries[$a]";
if (strpos($lang,$domainstrings[$a])) echo " SELECTED";
echo "\">".ucfirst($countries[$a])."</option>";
}
echo "</select>";
?>

what could be wrong?

vincevincevince

10:33 am on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



tried:


<?php
$countries=array("german","french","english");
$domainstrings=array("de","fr","en");
echo "<select name=\"lang\">";
for($a=0;$a<sizeof($countries);$a++)
{
echo "<option value=\"$countries[$a]";
if ($_REQUEST['lang']==$domainstrings[$a]) echo " SELECTED";
echo "\">".ucfirst($countries[$a])."</option>";
}
echo "</select>";
?>

?

lindajames

10:41 am on Jul 26, 2003 (gmt 0)

10+ Year Member



tried that, still no luck

vincevincevince

10:52 am on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



are you doing something sneaky we don't know about?
when you do these tests - test them in a .php page all on their own - not within your main page... do they work then?

lindajames

10:58 am on Jul 26, 2003 (gmt 0)

10+ Year Member



i created a new .php file and copied the following exactly as it is:

<?php
$countries=array("german","french","english");
$domainstrings=array("de","fr","en");
echo "<select name=\"lang\">";
for($a=0;$a<sizeof($countries);$a++)
{
echo "<option value=\"$countries[$a]";
if ($_REQUEST['lang']==$domainstrings[$a]) echo " SELECTED";
echo "\">".ucfirst($countries[$a])."</option>";
}
echo "</select>";
?>

then i called the test.php?lang=en but still no luck

vincevincevince

11:05 am on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<?php
$countries=array("german","french","english");
$domainstrings=array("de","fr","en");
echo "<select name=\"lang\">";
for($a=0;$a<sizeof($countries);$a++)
{
echo "<option value=\"$countries[$a]\"";
if ($_REQUEST['lang']==$domainstrings[$a]) echo " SELECTED";
echo ">".ucfirst($countries[$a])."</option>";
}
echo "</select>";
?>

sorry, my mistake, got a \" wrong

lindajames

11:10 am on Jul 26, 2003 (gmt 0)

10+ Year Member



thats much better. it works now ;-) thanx alot vince.

vincevincevince

11:29 am on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you're welcome lindajames... sorry it took soo many attempts