santa
santa

Reputation: 12512

change URL based on ancor tag with PHP

I would like to be able to change different content on a page based on an anchor tag:

http://mydomain.com/mypage#conten1
http://mydomain.com/mypage#conten2

Can it be done with PHP? I've tried print_r($_GET) to see if I can get it that way, but certainly it does not work. Shall I use something like:

if (ereg("#content1",$REQUEST_URI)) {
   // show content 1
}

...or is there a better way?

Thanks.

Upvotes: 0

Views: 258

Answers (2)

powtac
powtac

Reputation: 41050

# can not be accessed by a server, it is not transmitted by browsers to the server!

What you propably want to do is: to jump to a section on a page, you can simply do this with plain HTML:

<a href="#section_1">Show 1</a> 
<a href="#section_2">Show 2</a> 

<a name="section_1" /> Section 1
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />

<a name="section_2" /> Section 2

An other solution would be to catch the link via JavaScript and trigger a request to the server. This is how facebook does it.

Upvotes: 3

Capsule
Capsule

Reputation: 6159

If you want to switch content on the server-side, use ?content1 or ?content2 in URL:

<?php
switch (true) {
 case isset($_GET['content1'):
 // content 1 actions
 break;

 case isset($_GET['content2'):
 // content 2 actions
 break;

 default:
 // default actions
 break;
}
?>

Or, more classic, ?content=1 or ?content=2 in URL:

<?php
switch ($_GET['content']) {
 case '1':
 // content 1 actions
 break;

 case '2':
 // content 2 actions
 break;

 default:
 // default actions;
 break;
}

?>

Upvotes: 1

Related Questions