Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Location
    Toronto, ON
    Posts
    102
    Thanks
    12
    Thanked 1 Time in 1 Post

    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.

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Location
    Toronto, ON
    Posts
    102
    Thanks
    12
    Thanked 1 Time in 1 Post
    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!

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.

  • Users who have thanked BluePanther for this post:

    inchecksolution (07-22-2011)

  • #5
    Regular Coder
    Join Date
    Jul 2011
    Location
    Toronto, ON
    Posts
    102
    Thanks
    12
    Thanked 1 Time in 1 Post
    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!

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    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']; 

  • #7
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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).

  • #8
    Regular Coder
    Join Date
    Jul 2011
    Location
    Toronto, ON
    Posts
    102
    Thanks
    12
    Thanked 1 Time in 1 Post
    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!

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #10
    Regular Coder
    Join Date
    Jul 2011
    Location
    Toronto, ON
    Posts
    102
    Thanks
    12
    Thanked 1 Time in 1 Post
    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.

  • #11
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •