Jump to content

Usage of function newLocation()


kikilis8

Recommended Posts

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>";

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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>"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 &.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!!!! :)

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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) . '&';

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...