View Full Version : Remove anchor hash from URL

03-05-2010, 10:51 AM
I have a search page containing a form. When submitted the results are displayed on the same page and displayed in rows.

Each row has a title with a hyperlink. The hyperlink goes to the detail page, carrying across the id e.g. detail.php?id=123 along with the other search parameters in a session.
The link also uses the id as the anchor name e.g. <a href="detail.php?id=123" name="hash123">The Row Title</a>

I then use the id at detail.php to generate a 'back to results' link, with the #hash appended e.g. search.php?param1=abc&param2=def#hash123
This then scrolls the page down so that the user is taken back to where they left off.

The problem here is if the user goes back to the search page (from details.php) and changes the filters/keyword, runs the search again - the #123 stays in the URL. I need to remove this when the form is submitted again.

Does anyone know how I can do this?

03-05-2010, 07:16 PM
I had to do something similar not long ago. This is probably not the "best practices" way to do it, but I just manipulated the query string (accessed it via $_SERVER['QUERY_STRING']) before I reloaded the next page. I did something like:

echo "<a href=\"mbthreads.php?".str_replace("&m=dec", "", $_SERVER['QUERY_STRING'])."\">\n";