Reputation: 568
1st problem
I have a dynamic site where the pages are included in the index page dynamically in the following way:
$page = (empty($_GET["page"])) ? "home" : $_GET["page"];
$page = "folder/".basename($page).".php";
if (is_readable($page)) {
include($page);
} else {
echo 'Site doesn't exist!';
exit;
}
But there is also a language menu which changes the languages with another $_GET parameter:
<form name="switch" id="lang" action="" method="get">
<select name="multilingual" onchange="this.form.submit()">
<option value="lang_it">Italian</option>
<option value="lang_de" >German</option>
<option value="lang_en">English</option>
</select>
</form>
if(!isset ($_COOKIE['multilingual'])){
$lingua = null;
}else{
$lingua = $_COOKIE['multilingual'];
}
if(isset($_GET['multilingual'])){
$lingua = $_GET['multilingual'];
$_SESSION['multilingual'] = $lingua;
setcookie('multilingual', $lingua, time() + (3600 * 24 * 30));
}
switch($lingua){
case 'lang_it': include_once('file_it.php'); break;
case 'lang_de': include_once('file_de.php'); break;
case 'lang_en': include_once('file_en.php'); break;
default: include_once('file_it.php'); break;
}
The second $_GET parameter is passed to the URL but on the pages it will redirect me always to the default home page.
How to change the language without beiing redirected to the home page? In other words, ho to change the language and stay on the same page?
2nd problem
Is it possibile to change the lang_it, lang_de, lang_en parameter in the URL to get redirected to the current language of the page. If this will happen, how to change the language switcher when the page changes to another language?
Upvotes: 0
Views: 407
Reputation: 1548
<form name="switch" id="lang" action="<?php echo $page; ?>" method="get">
<select name="multilingual" onchange="this.form.submit()">
<option value="lang_it" <?php if($_COOKIE['multilingual']=='lang_it') echo "selected='selected'" ?>>Italian</option>
<option value="lang_de" <?php if($_COOKIE['multilingual']=='lang_de') echo "selected='selected'" ?>>German</option>
<option value="lang_en"<?php if($_COOKIE['multilingual']=='lang_en') echo "selected='selected'" ?>>English</option>
</select>
</form>
Upvotes: 1
Reputation: 2670
this.form.submit() not gonna work without page refresh/redirect.
If i understood your problem correctly:
You need to start using one-page JS frameworks
or load language vars dynamically in the background via (again) JS (load laguage vars via json config populate/change/mutate DOM contents)
EDIT
Forgot to mention: be careful with your "dynamic" website. The way you do it is quite unsafe, security wise.
EDIT
Try changing forms action
attibute. Put there current filename
EDIT More secure way is to have an array with pages that are actually exist and safe and after form submit check $_GET parameters against that array
Upvotes: 0