passing variable through URL not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

passing variable through URL not working

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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();

?>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

if the first page is in asp as your redirect is going to then the syntax might be different.
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

Post 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.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

on a side note: don't confuse java and javascript. java = crazy OOP and javascript is for websites
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Post Reply