There are three languages ​​on the site, everything works fine, BUT when switching the language, it always flips to the main page, how to fix it?

<a href="index.php?lang=ua"><img src="images/header/h-ua.png" data-tooltip="українська"></a> <a href="index.php?lang=ru"><img src="images/header/h-ru.png" data-tooltip="російська"></a> <a href="index.php?lang=en"><img src="images/header/h-en.png" data-tooltip="англійська"></a> 
  • one
    I'm even afraid to make a mistake, but maybe you should change the link? - andreyqin
  • @ LIAKAJI, why should it be dropped somewhere else if index.php is explicitly indicated in the link? - user6550
  • I wrote instead of the index $ _SERVER ['REQUEST_URI'], but then the parameter & lang was assigned as many times as it is clicked - LLIAKAJI
  • Instead, write $ _SERVER ['SCRIPT_NAME'] - andreyqin
  • 2
    In fact, it suffices to remove index.php, leaving only ? Lang = language . <a href="?lang=en"> <img src = "images / header / h-en.png" data-tooltip = "english"> </a> The transition will be on the same page, but with the addition of a parameter. - Deonis

2 answers 2

The problem is that the script does not remember the current link. Take $ _SERVER ['SCRIPT_NAME'], take $ _GET, parse get, replacing $ _GET ['lang'] with what you need. Then with the help of http_build_url () you create a new link and it is already stuffed into

 <a href" 
  • A slightly simpler way, but it is rather a crutch $ uri = preg_replace ('/ lang = [az] {2} /', '', $ _ SERVER ['REQUEST_URI']); if (strpos ($ uri, '?')! == false) {$ uri. = '& lang ='. $ LANG_OF_CURRENT_LINK; } else {$ uri. = '? lang ='. $ LANG_OF_CURRENT_LINK; } <a href="<?php print $uri; ?> "> <img src =" images / header / h-ua.png "data-tooltip =" <? php print $ current_lang_name;?> "> </ a> - knes

It may be worth storing the language in the session, and switch to AJAX?

 <div class="link" id="ru"><img src="images/header/h-ru.png" data-tooltip="російська"></div> $('.link').click(function(){ var lang=$(this).attr('id'); jQuery.ajax({type:'POST',url:'ajax/lang.php',data:{'lang':lang}}); }); 

ajax / lang.php

 $lang=$_POST[lang]; $_SESSION[lang]=$lang; // update page 

langs

 $lang=array( 'ru'=>array( 'welcome'=>'Добро пожаловать', ), 'ua'=>array( 'welcome'=>'Москаляку на гиляку, кто ни скачит той москаль', ), 'en'=>array( 'welcome'=>'Welcome', ), ); 

index

 <title><?php echo $lang[$_SESSION[lang]][welcome]; ?></title>