Header with a named anchor variable

Hello and i hope that everyone is having a pleasant day,

i am wondering if anyone has had success with a header redirect using a named anchor as a variable? i have tried this today and it fails but maybe there is a reason why it isn’t working for me.

the named anchor works if i use a normal header

header("Location: ../#placeTobe");

the named anchor fails if i add a variable:

$jumper = "dynamicNameFromAformPost";
header("Location: ../#" . $jumper);

i feel like the value of the variable is not being parsed as, say, the result of an echo would be handled. Is it possible to accomplish this task?

is it better to append it to an entire header attached to a variable? of course, i could experiment but i’m seeking wisdom from experience.

Thank you.

Did you have an explicit look at what the variable contains? With var_dump. Because in general that works, quick demo:

<?php

if(!empty($_POST['anchor'])) header('location: '.basename(__FILE__).'#'.$_POST['anchor']);

?>

<form method="post">
	<input type="submit" name="anchor" value="abc" />
	<input type="submit" name="anchor" value="xyz" />
</form>
<?=str_repeat('<br />', 100);?>

<a id="abc">anchor abc</a>
<?=str_repeat('<br />', 100);?>

<a id="xyz">anchor xyz</a>

But at least the browser performs the actual redirect, so it must be seen in the webdevelopers console within the response headers like e.g.

HTTP/1.1 302 Found
Date: Sun, 24 Mar 2019 16:43:43 GMT
location: redirecttoselfanchor.php#abc
1 Like

Hello Christian, I’ve located the problem. First, Thank you for taking time to reply. Excellent post by you with a fine example.

I have an “Einstellungen” properties page for members of my site. I offer a background changer using a form. When a background is selected, then i remove it from the list and place it as the first background. The current background “Aktuell” is unavailable as an option (naturally, because it is all ready selected.)

I decided to place a named anchor to test a jump back to where you were on the page, so that a user doesn’t have to scroll. I don’t want to spend a long time on this page right now, so i just print all options on one page. Later i wish to create a sort of slideshow of options as say version 2 of the feature.

Anyway, like an idiot, i failed to notice that the selection becomes the current selection. Thus, the named anchor is not on the page. I want to make a Homer Simpson Doh! right now :smile:

I’ve added the anchor and it works perfectly. However, my wife doesn’t like jumping. She thinks it is confusing, so i will remove this jump after all. Atleast i know that it works.

Sometimes i feel like i am Proctor character from Police Academy movies
Mauser: “you’re not playing with a full deck, are you?”
Proctor: “oh, i don’t play cards, sir.”

:slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service