kikilis8 0 Posted February 28, 2008 Report Share Posted February 28, 2008 I want to make that changing the language from one to the other it would open home page. But i face one problem that it does not react to the function onclick=\"java script:newLocation()\". It does not show any error, but it does not work... How you can see in my code OnClick i tried in two places: first in <a href, second in <img . It does not work exactly for this code, which is written to change language. I have tried to use the function onclick=\"java script:newLocation()\" for other picture, it forks fine... Where is the problem and how to solve it?Thank you in advance! <script type="text/javascript">function newLocation(){window.location="http://www.jadvyga.lt/?language=en";}</script> echo "<a href=\""; if (count($HTTP_GET_VARS)==0) echo getenv('REQUEST_URI')."?language=lt"; else { echo getenv('REQUEST_URI')."&language=lt"; } echo "\" onclick=\"java script:newLocation()\"><img src=\"images/lt_flag.gif\" border=\"1\"></a> <a href=\""; if (count($HTTP_GET_VARS)==0) echo getenv('REQUEST_URI')."?language=en"; else { echo getenv('REQUEST_URI')."&language=en"; } echo "\"><img src=\"images/en_flag.gif\" border=\"1\" onclick=\"java script:newLocation();\" /></a>"; Quote Link to post Share on other sites
justsomeguy 1,135 Posted February 28, 2008 Report Share Posted February 28, 2008 Don't include the "java script:" in the onclick, just the function name. You might also want to use the href property of the location object.<img onclick="newLocation()">window.location.href = "http://www.jadvyga.lt/?language=en"; Quote Link to post Share on other sites
kikilis8 0 Posted February 29, 2008 Author Report Share Posted February 29, 2008 Don't include the "java script:" in the onclick, just the function name. You might also want to use the href property of the location object.<img onclick="newLocation()">window.location.href = "http://www.jadvyga.lt/?language=en"; I tried your suggestions, but still it doesn't work... How i have mention above, everything works if I apply the code for other image, but for language image it does not work... And I do not have any ideas what could cause the problem. Quote Link to post Share on other sites
Reg Edit 0 Posted February 29, 2008 Report Share Posted February 29, 2008 A couple of points: - don't embed an element with an onclick handler within an anchor; use either onclick or href, not both. - don't append a querystring variable to the full URI since that already contains a querystring. You are appending more and more text such as ?language=en?language=it to the URI.Try the following, which usees PHP_SELF instead of REQUEST_URI, and doesn't need the javascript function since the href takes care of the click event: echo "<a href=\""; if (count($HTTP_GET_VARS)==0) echo getenv('PHP_SELF')."?language=lt"; else { echo getenv('PHP_SELF')."&language=lt"; } echo "\" ><img src=\"images/lt_flag.gif\" border=\"1\"></a> <a href=\""; if (count($HTTP_GET_VARS)==0) echo getenv('PHP_SELF')."?language=en"; else { echo getenv('PHP_SELF')."&language=en"; } echo "\"><img src=\"images/en_flag.gif\" border=\"1\" /></a>" Quote Link to post Share on other sites
kikilis8 0 Posted February 29, 2008 Author Report Share Posted February 29, 2008 if I use PHP_SELF instead of REQUEST_URI, if i use second time the language, it tries to open http://www.jadvyga.lt/&language=lt what means - page not found. It doesn not "keep" the file name.And what would you suggest, if I want to make that after choosing language the location would be changed? Quote Link to post Share on other sites
Reg Edit 0 Posted February 29, 2008 Report Share Posted February 29, 2008 if I use PHP_SELF instead of REQUEST_URI, if i use second time the language, it tries to open http://www.jadvyga.lt/&language=lt what means - page not found. It doesn not "keep" the file name.And what would you suggest, if I want to make that after choosing language the location would be changed?Sorry, I missed that... I think you don't need the if statement any more. So instead of if (count($HTTP_GET_VARS)==0) echo getenv('PHP_SELF')."?language=lt"; else { echo getenv('PHP_SELF')."&language=lt"; } you would just need echo getenv('PHP_SELF')."?language=lt"; and the same for the other if statement.WARNING: php is something I am only just learning myself!!! I mainly use MS stuff. Quote Link to post Share on other sites
kikilis8 0 Posted March 1, 2008 Author Report Share Posted March 1, 2008 Sorry, I missed that... I think you don't need the if statement any more. So instead of if (count($HTTP_GET_VARS)==0) echo getenv('PHP_SELF')."?language=lt"; else { echo getenv('PHP_SELF')."&language=lt"; } you would just need echo getenv('PHP_SELF')."?language=lt"; and the same for the other if statement.WARNING: php is something I am only just learning myself!!! I mainly use MS stuff. I am tiring to learn php too :)My code was OK, it needs else, because otherwise you can change language only once and later - errors. Once it takes "?language=lt", when it goes to else - "&language=lt". So difference is ? and &.Do you have any ideas how to change location after the flag is pressed to change language? Quote Link to post Share on other sites
Reg Edit 0 Posted March 1, 2008 Report Share Posted March 1, 2008 I am tiring to learn php too :)My code was OK, it needs else, because otherwise you can change language only once and later - errors. Once it takes "?language=lt", when it goes to else - "&language=lt". So difference is ? and &.Do you have any ideas how to change location after the flag is pressed to change language?The problem is that by using if/else, you are adding more and more to the query string every time after the first time, so it can become ?language=en&language=lt&language=en&language=lt That's why I suggested PHP_SELF plus ? every time. I think it should work. What problem are you having? Quote Link to post Share on other sites
kikilis8 0 Posted March 1, 2008 Author Report Share Posted March 1, 2008 The problem is that by using if/else, you are adding more and more to the query string every time after the first time, so it can become ?language=en&language=lt&language=en&language=lt That's why I suggested PHP_SELF plus ? every time. I think it should work. What problem are you having? if I leave only echo getenv('PHP_SELF')."?language=lt"; after changing the language second time it gives this warning:"Warning: array_key_exists() The second argument should be either an array or an object"I can not leave with &language echo getenv('PHP_SELF')."&language=lt"; also, because language should start with ? but not with &. Quote Link to post Share on other sites
Reg Edit 0 Posted March 1, 2008 Report Share Posted March 1, 2008 if I leave only echo getenv('PHP_SELF')."?language=lt"; after changing the language second time it gives this warning:"Warning: array_key_exists() The second argument should be either an array or an object"I can not leave with &language echo getenv('PHP_SELF')."&language=lt"; also, because language should start with ? but not with &.I have now seen PHP_SELF used in different ways and also now read that it may not be reliable. I have also seen it as $_SERVER[php_SELF] which is different from what I saw before.As a test, try hard-coding the URL for both hrefs: http://www.jadvyga.lt/?language=lt http://www.jadvyga.lt/?language=en Assuming you get that working, you can then sort out the right function for deriving the URL afterwards. Quote Link to post Share on other sites
kikilis8 0 Posted March 1, 2008 Author Report Share Posted March 1, 2008 I have asked above but i will repeat my question again .How to change location after the flag to change language is pressed? Reg Edit wrote a comment for me: "don't embed an element with an onclick handler within an anchor; use either onclick or href, not both. "So, what is the other way to change location?Please, Help!!!! Quote Link to post Share on other sites
justsomeguy 1,135 Posted March 4, 2008 Report Share Posted March 4, 2008 $url = '?';foreach ($_GET as $k => $v) $url .= urlencode($k) . '=' urlencode($v) . '&';$url .= 'language=lt';echo '<a href="' . $url . '" ><img src="images/lt_flag.gif" border="1"></a>'; You shouldn't use $HTTP_GET_VARS, it is old stuff. $_GET is the better one to use. Quote Link to post Share on other sites
kikilis8 0 Posted March 8, 2008 Author Report Share Posted March 8, 2008 What does variables $k and $v mean? What values should i set? Quote Link to post Share on other sites
Synook 47 Posted March 8, 2008 Report Share Posted March 8, 2008 The line foreach ($_GET as $k => $v) loops through the $_GET array (i.e. URL parameters) and assigns the key to $k and the value to $v. So if your querystring looked like "?en=english&fr=french&dt=german" then the first foreach loop $k would equal "en" and $v would be "english", the second time round $k == "fr" and $v == "french", etc. Quote Link to post Share on other sites
kikilis8 0 Posted March 8, 2008 Author Report Share Posted March 8, 2008 if I use the code like justsomeguy suggested $url = '?';foreach ($_GET as $k => $v) $url .= urlencode($k) . '=' urlencode($v) . '&';$url .= 'language=lt';echo '<a href="' . $url . '" ><img src="images/lt_flag.gif" border="1"></a>'; i have error: Parse error: syntax error, unexpected T_STRINGon line: $url .= urlencode($k) . '=' urlencode($v) . '&'; Quote Link to post Share on other sites
kikilis8 0 Posted March 8, 2008 Author Report Share Posted March 8, 2008 $url = 'http://www.jadvyga.lt/index.php';$url .= '?language=lt';echo '<a href="'.$url.'"><img src="images/lt_flag.gif" border="1"></a>';$url = 'http://www.jadvyga.lt/index.php';$url .= '?language=en';echo '<a href="'.$url.'"><img src="images/en_flag.gif" border="1"></a>'; My code above works fine now. At least it seems like when i change the language it opens home page. Quote Link to post Share on other sites
Synook 47 Posted March 8, 2008 Report Share Posted March 8, 2008 JSG's code has a slight error, it should be $url = '?';foreach ($_GET as $k => $v) $url .= urlencode($k) . '=' . urlencode($v) . '&';$url .= 'language=lt';echo '<a href="' . $url . '" ><img src="images/lt_flag.gif" border="1"></a>'; Quote Link to post Share on other sites
justsomeguy 1,135 Posted March 8, 2008 Report Share Posted March 8, 2008 Oops.The code you have above will always open the index page with the new language. The code that Synook corrected will open the same page and keep whatever other information is on the URL. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.