954,598 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Variables in the URL - and how to get them

Morning everyone --

Can anyone help me with what is probably very simple?

I have a page where I want to get a variable from the URL and display it... so if my url is http://www.mysite.com/page.html?variable=hi

I want to get that "hi" out of there and print it out to the page.

Thanks in advance!

2ndPlatform
Junior Poster in Training
61 posts since Nov 2004
Reputation Points: 15
Solved Threads: 0
 

Nevermind - I got it.

2ndPlatform
Junior Poster in Training
61 posts since Nov 2004
Reputation Points: 15
Solved Threads: 0
 

how?

please tell

melefire
Newbie Poster
6 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

It depends. It can be done in Javascript or using a serverside language.

Which would you rather do?

For reference, that part of the URL is called the 'query string'.

Server side langauges often have simplified interfaces to access the 'variables' in a query string. Javascript has no such interface; but it is possible to get reference to the entire URL of a page (address + query string), and to split that down into its component parts.

An example is here:

http://www.activsoftware.com/code_samples/code.cfm/CodeID/59/JavaScript/Get_Query_String_variables_in_JavaScript

(That's a somewhat wasteful procedure if you want to repeatadly get at the different variables in a query string; but its not too bad if you have alot of potential variables and are only interested in a few of them)

MattEvans
Veteran Poster
Moderator
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
 

Um, the java script lokos hard, could you please tell me how i would retrive the variable text, just the text, onto a page using php? or another server side language

melefire
Newbie Poster
6 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

With PHP you simple use the
$_GET['variable'];

Variable. For example if you had a link

www.daniweb.com/fakeurl.html?id=12

Your PHP would be:

<?php
 
$id = $_GET['id'];
 
echo $id;
 
?>


this would output 12

GliderPilot
Junior Poster in Training
86 posts since Sep 2006
Reputation Points: 8
Solved Threads: 4
 

cool, thanks

melefire
Newbie Poster
6 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

You can do it only with php.

X123
Newbie Poster
4 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 
You can do it only with php.


You can do it with any serverside language, and any client side script should be able to get the query string indirectly from the page URL.

PHP does it through Zend, which does it through C++; and it's delivered into an application's environment by the server software; so clearly, you can access it using other means thanonly PHP.

I'm sure you meant something different; and I don't mean to bite your head off; but there's a full wealth of ways in which you can access that data, PHP is one way to do it.

MattEvans
Veteran Poster
Moderator
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
 

Hi,

I have a website say www.test.com.
How do i give the variables linked URL to external website in PHP.

For instance, i have a variable $xyz in php code. $xyz indicates a url say www.yahoo.com, then it opens the page as www.xyz.com/www.yahoo.com

My code is as below:

test

maddiv
Newbie Poster
2 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

Hi,

I have a website say www.test.com.
How do i give the variables linked URL to external website in PHP.

For instance, i have a variable $xyz in php code. $xyz indicates a url say www.yahoo.com, then it opens the page as www.xyz.com/www.yahoo.com

My code is as below:

test

maddiv
Newbie Poster
2 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 
www.xyz.com/www.yahoo.com


doesn't make much sense to me.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,796 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

Hi, I'm making a 404, and I want it to display the current URL, but I don't want it to be server side. I don't even care about the variable after the URL, because I won't be using them, but it might be good to display it with the rest of the address. I noticed the link in the first post had that, but it was server side. Thanks!

keavon
Light Poster
30 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Hi
This thread was started in 2004!
Maybe you should start another thread of your own?

Zagga
Posting Whiz in Training
257 posts since Dec 2009
Reputation Points: 22
Solved Threads: 54
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You