View Full Version : Get Variable out of URL
cli_man
10-26-2003, 06:11 PM
I have a URL Like below:
www.somewhere.com/page.html?u=3
How can I take the u varible and make something like below?
<script language="JavaScript" src="www.somewhereelse.com/page.php?u=3">
</script>
What I have is a php page that needs variables to be passed to it, and I don't know how to do it very easy in the html.
To give you an example, The finished page in php is here:
http://www.threadaweb.com/pictures/login.php?i=88
I am trying to get the page running on hosts that can only use html and javascript and I have a demo I am working on at:
http://www.threadaweb.com/pictures/java.html
It works fine all except the page numbers, I need to find a way to pass what page I am on to the php script.
Willy Duitt
10-26-2003, 07:34 PM
This should get you started:
<script type="text/javascript">
<!--
theUrl ="http://www.somewhere.com/page.html?u=3";
sptUrl = theUrl.split("?");
phpUrl = "http://www.somewhereelse.com/page.php?";
newUrl = phpUrl+sptUrl[1];
document.write(newUrl);
//-->
</script>
....Willy
n8thegreat
10-26-2003, 08:45 PM
this will parse the url and make a variable for each variable in the URL
function parseURL(){
URL = new String( document.location );
vars = URL.split("?");
if( vars.length > 1 ) {
vars = vars[1];
vars = vars.split("&");
for( var num = 0; num < vars.length; num++ ) {
vars[num] = vars[num].split("=");
}
for( var num = 0; num < vars.length; num++ ) {
eval( vars[num][0] +" = '"+ vars[num][1] +"'" );
}
}
}
so if you have a URL thats like page.html?bob=wassup it will make a javascript variable called bob with the value wassup. so if you run that function and have that query string, you could do alert(bob) and it would alert "wassup"
hope that helps :)
cli_man
10-27-2003, 07:26 AM
Ok the answer from Willy Duitt gives me the web address to pass to php, now how do I do another:
<script language="JavaScript" src="newUrl">
</script>
What is the syntax of putting newUrl into the src of the statement?
Horus_Kol
10-27-2003, 08:00 AM
if you use PHP, then you can get a list of variables being passed to it with $_GET.
eg,
http://www.threadaweb.com/pictures/login.php?i=88&x=abc
$ID = $_GET['i'];
$name = $_GET['x'];
echo $ID; // outputs 88
echo $name; // outputs abc
n8thegreat
10-27-2003, 09:23 AM
he cant do that, the page can only have html and javascript
if you want to pass on the query string to a javascript src you would have to do something like
document.write("<scr"+"ipt type='text/javascript' src='"+ newURL +"'></scr"+"ipt>");
Horus_Kol
10-27-2003, 09:30 AM
what? I thought he was sending the information to a PHP page? why can't he?
Willy Duitt
10-27-2003, 10:04 AM
I'm still not sure what you are trying to do. }:-)
Try something like this: Placed in the head of your
document you wished passed. And putting a value in the
form action="" it should do what you are asking.
But then again, what do I know. I'm quessing! :D
<script type="text/javascript">
<!--
theUrl = document.location;
sptUrl = theUrl.split("?");
phpUrl = "http://www.somewhereelse.com/page.php?";
newUrl = phpUrl+sptUrl[1];
html = '<form name="pHp" method="get" enctype="text/plain" action="">';
html += '<input type="hidden" name="sndUrl" value="'+newUrl+'">';
html += '</form>';
document.write(html);
alert(document.pHp.sndUrl.value)
window.onLoad = document.pHp.submit();
//-->
</script>
Remove the alert once you verify
it is passing the correct data.
.....Willy
Edit: Repaired theUrl syntax error
n8thegreat
10-27-2003, 10:11 AM
what? I thought he was sending the information to a PHP page? why can't he?
well, he said he was trying to get it to work on hosts that dont have php, so only using html and javascript
also, im not very sure either as to what he is trying to accomplish because weve gotten so many different replies, can you tell us exactly?
Horus_Kol
10-27-2003, 10:18 AM
if his host is not running php, why is he sending to a php page? i am so confused here now...
n8thegreat
10-27-2003, 10:27 AM
so am i :confused:
Willy Duitt
10-27-2003, 12:48 PM
I'm still as confused as everyone else. :confused:
But, although the previously posted script worked on my
local machine, I noticed that it threw an error when
uploaded to my server.
FWIW: Here are the ammended codes....
<script type="text/javascript">
<!--
thePage=location.href
theUrl=thePage.substring(thePage.lastIndexOf("?")+1,thePage.length+1)
phpUrl = "http://www.somewhereelse.com/page.php?";
newUrl = phpUrl+theUrl;
html = '<form name="pHp" method="get" enctype="text/plain" action="">';
html += '<input type="hidden" name="sndUrl" value="'+newUrl+'">';
html += '</form>';
document.write(html);
alert(document.pHp.sndUrl.value)
window.onLoad = document.pHp.submit();
//-->
</script>
An online example can be found here (http://www.transload.net/~cyberrite/Willy/Codes/test/AppendUrl.shtml).
....Willy
cli_man
10-27-2003, 08:07 PM
Ok here is some more info to clear things up.
I do a good bit of custom programming, the php pictures page I have is called THREAD-A-PICTURES, I am currently selling it as a service people can use to trade pictures with there friends and family. All of this is working fine, one feature I wanted to add was: If say a realitor wanted to use my service but wanted to have all there pictures show up on there site I wanted them to just be able to plug in a couple lines of code and then anything they put in my THREAD-A-PICTURES would show up on their website.
Since I do not know where people would be hosting there sites I wanted to have it just use html and javascript as that will work with any host.
That said, the problem I have is when I try to add in pages, since I have no way to pass variables to the php script as the JavaScript src is in a static page so what I have now is:
<script language="JavaScript" src="http://www.threadaweb.com/pictures/javascript.php?i=88&u=http://www.threadaweb.com/pictures/java.html" type="text/javascript">
Where i == user_id
And u == the page that the script is being called from
I want to add a variable of: p == current page
So now saying that my client who only has the ability to use html and javascript can have there page at:
http://www.threadaweb.com/pictures/java.html
And then I can have the link for page 2 to be:
http://www.threadaweb.com/pictures/java.html?p=2
Then I could have javascript pass that variable to the php page on my website.
I hope this will help clear things up a bit, I am sorry to not have given enough detail in the beginning.
cli_man
10-27-2003, 08:18 PM
Oh also the direction I think I need to go with this is to have to <script> tags the one will be:
<script type="text/javascript">
<!--
sptUrl = document.URL.split("?");
phpUrl = "http://www.threadaweb.com/pictures/javascript.php?";
newUrl = phpUrl+sptUrl[1];
//-->
</script>
<script type="text/javascript" src="newUrl">
</script>
I do not know the syntax for calling the newUrl as a src tag
Willy Duitt
10-27-2003, 11:35 PM
Marcus;
Since you are hosting the pages on your server
why can't you just supply them with a link to
their hosted pictures.
<A HREF="http://www.threadaweb.com/pictures/login.php?i=88">
My Pictures (http://www.threadaweb.com/pictures/login.php?i=88)</A>
Or use an inline frame.
<IFRAME name="phpSrc" src="http://www.threadaweb.com/pictures/login.php?i=88">
.....Willy
cli_man
10-28-2003, 05:11 AM
They will not all be on my server, I will probably have people with sites on geocities and places like that. I actualy have a demo setup on angelfire that I am working with. http://www.angelfire.com/pa/lostboy1/threadaweb.html
cli_man
10-28-2003, 06:13 AM
Well I think I have figured it out, the way I have it working now I didn't have to use any extra JavaScript, I am much better at PHP than javascript, (as you have probably noticed)
I found that when I call my PHP script from any html page the $HTTP_REFERRER just happens to be the html page that I need to get the variable from, so I just wrote a quick PHP script to decode the $HTTP_REFERRER and I can pass any variable I want to php, I will post the code I used:
$detect = $HTTP_REFERER;
$split_url = explode("?", $detect);
$begin_var = explode("&", $split_url[1]);
$all_done = explode("=", $begin_var[0]);
$page = $all_done[1];
With this I can pass a page of: http://www.angelfire.com/pa/lostboy1/threadaweb.html?p=2 and the script above will set the $page number to be 2 from the $p variable I gave it.
Now I just have to make that script a good bit more robust with some error checking, but I can do all that on the PHP side.
Thank you for all your help, I am sorry to have confused you all so much :-) You guys got me going down the right path.
Willy Duitt
10-28-2003, 08:49 AM
Glad to hear you are working it out.
I may have just begun to understand what you are trying
to do. But I'm still confused, and it doesn't seem to
make sense. :confused:
If they want to use the images hosted on your server,
why can't they just hotlink them? If they want to use
the entire page, why can't they use layers/iframes using
your hosted page as the source? How would they use your
example?
Regardless:
I mashed this together. It's far from perfect and needs
work to make it fully functional. For example, it uses
the images.length property and it should use getElementByID
or some other way to reference the images. But it is an
example of splitting the document.location and appending
your URL to the image .src
<script type="text/javascript">
<!--//
function chkUrl(){
for(i=0;i<document.images.length;i++){
theUrl = document.images[i].src
sptUrl = theUrl.split("_");
phpUrl = "http://www.threadaweb.com/pictures/pics/thumb_";
newUrl = phpUrl+sptUrl[1];
document.images[i].src = newUrl;
}
}
//-->
</script>
</HEAD>
<BODY onLoad="chkUrl()">
An online example can be found here (http://www.transload.net/~cyberrite/Willy/Codes/test/RepalceImageUrlTest.html).
.....Willy
cli_man
10-28-2003, 09:22 AM
To give an example, I have a realitor that has to put up pictures of their houses they have for sale, none of them at that company has a clue about web design, they cannot even use frontpage. They also have there website hosted by geocities so I cannot setup a nice php application for them to use on their site.
What I have done now is they can use my THREAD-A-PICTURES which I host and add their pictures without any knowledge of web design. It is easier to add in pictures into my THREAD-A-PICTURES than it is to send an email.
Now after they put in one line of code into their website on geocities their geocities will show all their pictures that are in their THREAD-A-PICTURES.
If they are not smart enought to install the one line, then I charge them a small fee and put it into their website and then they can just pay me a monthly fee to use my service.
The only problem I had with getting all this to work is being able to pass variables to my php script to display the pictures, what the php script does is select all the data it needs out of my THREAD-A-PICTURES database and then create the html for it and do a whole mess of document.write's to the geocities page or wherever they want it to show up.
If you look at http://www.angelfire.com/pa/lostboy1 I have it working there, if you look all the pictures are actually hosted at: http://www.threadaweb.com/pictures/pics/ and the code just has a javascript include file.
This is a really slick program that I hope will save me alot of time with different customers that have to update their own website but do not know enought to do it. You can see a bit more about the THREAD-A-PICTURES at http://www.threadaweb.com/pictures/features.html
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.