Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-22-2011, 06:02 PM   PM User | #1
inchecksolution
Regular Coder

 
Join Date: Jul 2011
Location: Toronto, ON
Posts: 102
Thanks: 12
Thanked 1 Time in 1 Post
inchecksolution is an unknown quantity at this point
Assign url to php variable ex. mysite.com/#234 - how do i assign 234 to a variable?

Hi guys,

I have link on my site that get sent to people in the following format: http://www.mysite.com/#234

This way when they come to my site the page will automatically scroll to the div with the id 234.

Is there a way to assign whatever number is in the url (in this case 234) to a php variable?

Last edited by inchecksolution; 07-22-2011 at 06:22 PM..
inchecksolution is offline   Reply With Quote
Old 07-22-2011, 06:14 PM   PM User | #2
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
This is an HTML question, but I'll answer it anyway :P. You need to apply an anchor to the content you want to scroll down to. Use the name attribute to set an anchor, #234 asks the browser to locate an element with the name #234. Your div will look something like this: <div id="234" name="234">. id and name can be two different values if required.
BluePanther is offline   Reply With Quote
Old 07-22-2011, 06:16 PM   PM User | #3
inchecksolution
Regular Coder

 
Join Date: Jul 2011
Location: Toronto, ON
Posts: 102
Thanks: 12
Thanked 1 Time in 1 Post
inchecksolution is an unknown quantity at this point
Hi BluePanther.

Thanks for the response, but I already have that done. If you look at my original post, my question pertains to how I can assign '234' to a php variable. Any thoughts??

Thanks again!
inchecksolution is offline   Reply With Quote
Old 07-22-2011, 06:31 PM   PM User | #4
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Oh, um, not sure about that one. Why though?
PHP Code:
$foo explode("#"$_SERVER['REQUEST_URI']);
$bar $foo[1]; // $bar should equal 234 I think. 
I would obviously be careful with $bar, sanitize it before using it for things like include's or query's or anything a malicious user can break.
BluePanther is offline   Reply With Quote
Users who have thanked BluePanther for this post:
inchecksolution (07-22-2011)
Old 07-22-2011, 06:38 PM   PM User | #5
inchecksolution
Regular Coder

 
Join Date: Jul 2011
Location: Toronto, ON
Posts: 102
Thanks: 12
Thanked 1 Time in 1 Post
inchecksolution is an unknown quantity at this point
Thanks, I'll do some testing with that. The reason I want the number is so I can use some javascript to highlight div that has the id/name 234. If you can think of a better way to do this, by all means -I'm all ears.

Thanks for your help!
inchecksolution is offline   Reply With Quote
Old 07-22-2011, 07:10 PM   PM User | #6
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Anything after a hash or pound sign(anchors), is not sent to the server. They are used by the browser, and only after the page is loaded. If you want to pass a variable in the URL you prefix it with a question mark and use the $_GET array or $_SERVER['QUERY_STRING'].
PHP Code:
 // http://yoursite.com/index.php?id=1234
$myID $_GET['id']; 
Inigoesdr is offline   Reply With Quote
Old 07-22-2011, 07:21 PM   PM User | #7
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
Quote:
Originally Posted by Inigoesdr View Post
Anything after a hash or pound sign(anchors), is not sent to the server. They are used by the browser, and only after the page is loaded. If you want to pass a variable in the URL you prefix it with a question mark and use the $_GET array or $_SERVER['QUERY_STRING'].
PHP Code:
 // http://yoursite.com/index.php?id=1234
$myID $_GET['id']; 
Very true, but in the OP's case he's wanting to capture the anchor point used. Technically speaking, he could use ?id=1234#1234 but he is not using the $_GET to collect any information or display information (to the best of my knowledge).
BluePanther is offline   Reply With Quote
Old 07-22-2011, 07:24 PM   PM User | #8
inchecksolution
Regular Coder

 
Join Date: Jul 2011
Location: Toronto, ON
Posts: 102
Thanks: 12
Thanked 1 Time in 1 Post
inchecksolution is an unknown quantity at this point
I Inigoesdr. Yes I am very familiar with passing variable via URL. It looks like I am going to have to combine both methods which will look somewhat stupid in the URL, but it seems to be the only way ex. http://www.mysite.com?id=234#234.

This way the page will automatically scroll to <div id=234> and I can assign id=234 to a variable in order to use in a query.

Thanks for your help!
inchecksolution is offline   Reply With Quote
Old 07-22-2011, 07:27 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The problem is still the same though, hashes are not sent to the server for processing, so #234 would never be available for php to use.
In PHP, there is no way to retrieve the has from an input URL, as it is not included in any server variable. If you're talking about a string with a url in it, then you can use parse_url to retrieve the hash.
The hash itself I believe is still retrievable in Javascript anyways, since it is a client side language. So you can still use Javascript to highlight this id.
Fou-Lu is offline   Reply With Quote
Old 07-22-2011, 08:52 PM   PM User | #10
inchecksolution
Regular Coder

 
Join Date: Jul 2011
Location: Toronto, ON
Posts: 102
Thanks: 12
Thanked 1 Time in 1 Post
inchecksolution is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
so #234 would never be available for php to use.
That is true, but if I use ?id=234#234 then I get the best of both.
inchecksolution is offline   Reply With Quote
Old 07-22-2011, 09:19 PM   PM User | #11
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
If you're wanting some sort of highlighting in javascript, can you not just do this?
Code:
function getAnchor() {
var url = window.location;
var anchor = url.split('#');
alert(anchor[1]);
}
The alert should give you the info in the #. I THINK haha not too great with javascript tbqh, but competent. There might be something wrong with that.
BluePanther is offline   Reply With Quote
Old 07-23-2011, 02:12 AM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by BluePanther View Post
If you're wanting some sort of highlighting in javascript, can you not just do this?
Code:
function getAnchor() {
var url = window.location;
var anchor = url.split('#');
alert(anchor[1]);
}
The alert should give you the info in the #. I THINK haha not too great with javascript tbqh, but competent. There might be something wrong with that.
Me neither, and this is much what I was thinking.
If its purely for a JS highlight, I wouldn't worry about passing this to PHP at all.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:02 PM.


Advertisement
Log in to turn off these ads.