PHP question

If I am using PHP to send a web page, is there any way to have it land on the place with a “name” attribute in an anchor tag?

Suppose I have the following HTML being generated by my PHP file:

<p><a name=“colors”><b>Do we all see the same colors?</b></a></p>

Is there any way to cause the browser to scroll down to this line, as if I had loaded a web page using a URL like

www.something.com/index.html#colors

?

You answered your own question.

This link …

<a href=“Something.”>Go Here</a>

will take you to the anchor “colors” on the index page —> <p><a name=“colors”><b>Do we all see the same colors?</b></a></p>.
See here for more info.

You can do this, but PHP code isn’t going to be the best way. Basically, as you said, PHP is a way of sending a web page. Scrolling to a named anchor in a document, though, is done by the receiver, the web browser, and it’s generally not dependent on what is sent, except to identify the anchors. You could think of it, vaguely, like someone asking for a book at a library. PHP is a really fast printing press that can generate a brand new book instead of just handing over an already published one - but you can’t direct the library patron to chapter 9, that’s up to them.

Well - normally you can’t. There are a few ways around it. One would be a redirect, which is more like putting a sign on a door than handing over a book. (Yeah, mixed metaphor I know.) With a PHP redirect, you could say, “what you’re looking for is actually http://www.something.com/index2.php#colors”, and most browsers will automatically go and fetch the redirect URL, including looking up the correct answer. However, you can’t do this at the same URL as you’re providing content.

The other is by javascript, code that runs in the web browser instead of your web server. Something like this:



<script type="text/javascript" language="javascript">
location.hash = "#colors";
</script>


…will instruct the browser to immediately tag #colors onto the end of the current address, which will autoscroll to the <a name=‘colors’> As long as the user hasn’t disabled javascript from running on your website.

Hope that this helps.

I’m guessing that he wants to do it when a user has entered the URL in the browser without the anchor reference. He’s not generating the link.

Apart from special PHP usage, you could simply generate a redirect with the anchor specified. Or, you could have an “onload” in the body tag to scroll to the desired element with javascript.

I think my answer may be buried in there somewhere but I see that I need to be clearer about what I’m doing.

I want a PHP page to generate the page but I don’t know *a priori *what name will specify the location to scroll to.

**Duckster **has the correct response when you’re doing this with a static HTML page, but note that I said “as if I had loaded a web page…”

I am trying to figure out how code *inside *the page can tell the browser to scroll down. I’ll take a closer look at suggestions from **yabob **and **chrisk **and report back.