Page 1 of 1

passing variable through URL not working

Posted: Thu Nov 10, 2005 6:01 am
by thurstan
Hi

I am trying to pass a variable through a URL, but it doesn't seem to be getting through, its using javscript as a re-direct, here is the code:

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
<!-- 
window.location="http://webpage.com/this/that.asp?id=variableID";
// -->
</script>
I have defined the variable as $variableID= $_POST["variableID"]; at the top of the page.

Can anyone suggest why this value isn't passing through, something perhaps to do with being inside the speech marks?

Thanks

Posted: Thu Nov 10, 2005 6:14 am
by Jenk
The correct JavaScript syntax would be:

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
<!--
window.location('http://webpage.com/this/that.asp?id=' . variableID);
// -->
</script>
Providing that the var variableID has been defined.

However, it appears that you are trying to use a variable that is defined by PHP in JavaScript, which cannot be transfered as easily as this, JavaScript is parsed by the client, PHP is parsed by the Server.

If you just want a simple redirect to be performed by PHP, then use something similar to the following:

Code: Select all

<?

header('Location: http://webpage.com/this/that.asp?id=' . $variableID);
exit();

?>

Posted: Thu Nov 10, 2005 6:46 am
by shiznatix
if the first page is in asp as your redirect is going to then the syntax might be different.

Posted: Thu Nov 10, 2005 6:48 am
by thurstan
thanks for the comments, this seems to work:

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
<!-- 
window.location="http://site.com/this/that.asp?id=<?php echo($variableID) ?>"
// -->
</script>
although i'm not sure how correct it is, but it does work.

Posted: Thu Nov 10, 2005 6:49 am
by shiznatix
thats perfectly correct but what happens if the user disables javascript (as i usually do). if it is possible then you might want to use header() instead of javascript

Posted: Thu Nov 10, 2005 6:52 am
by thurstan
Thanks for that, yes disabling java would be a problem, but, this whole site i've taken on is pretty java heavy, so if it is disabled, then the user wouldn't be able to do anything!

problem with the header() is that i already have one of these in use doing another redirect (thinkning about it i could loose this). thanks.

Posted: Thu Nov 10, 2005 6:53 am
by shiznatix
on a side note: don't confuse java and javascript. java = crazy OOP and javascript is for websites

Posted: Thu Nov 10, 2005 6:54 am
by Jenk
Using the server side equivalent of header() (in PHP's case is header() :p) is more efficient than outputting JavaScript redirects as you only send the header info and no unecessary output.

Remember to use exit() after you send a redirect header though, else the server will continue to parse the rest of the script.