$_GET echo language in Javascript file

Hello,

I have a website with diffrent languages.

That is working perfect but I got a problem with a form…

I have a form that reloads on change and then the URL is different:

In my index I use this code:

[PHP]if(!isset($_GET[‘lang’]))
{
include “talen/nl.php”;
$lang = “nl”;
}
else
{
include “talen/”.$_GET[‘lang’].".php";
$lang = $_GET[‘lang’];
}[/PHP]

I have a Javascript file that is working like this:

"reloadForm" : function(form, merk, model) { var val = form.options[form.options.selectedIndex].value, data = ""; if(merk != '') { data = data + "&merk=" + merk; if(model != '') { data = data + "&model=" + model; data = data + "&type=" + val; } else { data = data + "&model=" + val; } } else { data = data + "&merk=" + val; } window.location = "index.php?lang=nl&p=9&search=wiel" + data + "#keuze"; }

Everywhere I can change language with onclick and href like this for example:

[PHP]

<a href="?lang=<?php echo $lang; ?>[/PHP]

No in my Javescript file above I manualy entered the language (lang=nl)

But I want to have this set dynamicly like the rest of my website…

How can I realise this because I’m stuck now

Thanks in advance!

Assuming the javascript is called after you have set $lang, the following should work:[php] “reloadForm” : function(form, merk, model)
{
var val = form.options[form.options.selectedIndex].value,
data = “”;
if(merk != ‘’)
{
data = data + “&merk=” + merk;
if(model != ‘’)
{
data = data + “&model=” + model;
data = data + “&type=” + val;
}
else
{
data = data + “&model=” + val;
}
}
else
{
data = data + “&merk=” + val;
}
window.location = “index.php?lang=<?php echo $lang; ?>&p=9&search=wiel” + data + “#keuze”;
}[/php]

Please let me know if it doesn’t…

jay

Hi,

Thanks for the reply!:slight_smile:

I already tried that but that didn’t work…

I get this in my adressbar:

http://www.website.nl/index.php?lang=<?php echo $lang; ?>&p=9&search=wiel&merk=CI#keuze

And this error at the top of my page:

Warning: include(talen/<?php echo $lang; ?>.php) [function.include]: failed to open stream: No such file or directory in /var/www/html/website.nl/index.php on line 16

Warning: include() [function.include]: Failed opening ‘talen/<?php echo $lang; ?>.php’ for inclusion (include_path=’.:/usr/share/pear:/usr/share/php’) in /var/www/html/website.nl/index.php on line 16

I wanted to clarify the order that this must appear in your script.

You must be setting the variable in php above the point that your javascript function appears in order for it work.

This will work fine:[php]<?php $lang = 'NL'; ?>

[/php]

However, this will not work:[php]

<?php $lang = 'NL'; ?>[/php]

The important thing to note in this case is that the actual flow of your code is not relevant here, only the position within the script itself.

Hope this helps.

Thanks for the tip!

But my javascript and my php are not in the same file…

Javascript is in a seperate .js file and php in a seperate .php file…

How can I realise what you just said in my .js file?

OK, that explains why it wasn’t working for you.

What you could do is create another argument for your javascript function and then pass the $lang variable to it along with the other arguments. Something like:

“reloadForm” : function(form, merk, model, lang)
{
var val = form.options[form.options.selectedIndex].value,
data = “”;
if(merk != ‘’)
{
data = data + “&merk=” + merk;
if(model != ‘’)
{
data = data + “&model=” + model;
data = data + “&type=” + val;
}
else
{
data = data + “&model=” + val;
}
}
else
{
data = data + “&merk=” + val;
}
window.location = “index.php?lang=” + lang + “&p=9&search=wiel” + data + “#keuze”;
}

I think I’m on the right track…

When I use this:

"reloadWielForm" : function(form, merk, model, lang) { var lang = "nl" var val = form.options[form.options.selectedIndex].value, data = ""; if(merk != '') { data = data + "&merk=" + merk; if(model != '') { data = data + "&model=" + model; data = data + "&type=" + val; } else { data = data + "&model=" + val; } } else { data = data + "&merk=" + val; } window.location = "index.php?lang=" + lang + "&p=9&search=wiel" + data + "#keuze"; }

But cant get the language dynamic from my php file…

Sponsor our Newsletter | Privacy Policy | Terms of Service