Anchor after submit with $_server['self']

Hello all,
I’m having issues trying to implement this simple (apparently) behavior.

The point is to, after form submission, put the page on a given position.

I have seen here:

this code:

<form method='post' action='". $_SERVER['PHP_SELF'] ."#ebu_1'> 

But this doesn’t work. Maybe a missing ’ or " somewhere?
Anyway this, very close, to what I’m trying to accomplish.

The only difference being that, on my form tag, I have something more:


<form action="<?php echo filter_var($_SERVER['PHP_SELF'], FILTER_SANITIZE_STRING); ?>" method="post">

and the anchor is like this:

<a id="ancoraBaixo"></a>

Note:
If I type the URL with #ancoraBaixo at the end, it will work.

So what I’m asking is:
How can we add ancoraBaixo to the url immediately after form submission?

Thanks in advance,
Márcio

Hi katierosy,

A solution has been found on my previous post. :wink:
But thanks for the input. :slight_smile:

Kind regards,
Márcio

If you want to achieve a Behavior different than of what you see now. It will be achieved easily by yourself only.

Please read/focus more on anchor tags. You may be able to do it, if anchor tags works like, what you want to achieve. I do not emphasize on this, I simply use jquery form submit, to submit the form in place.

I am getting one idea. Just submit the form to submit.php another page, which redirects to the form page with the #anchor tags on the url.

Hope this will help.

So, I should keep on the FORM the submission using PHP Self and, on the php included add a redirect to that same page and add an anchor ?

Or, should I forget about the PHP Self on the form and use only the redirect ?

I see quite common the use of SELF for self submitting forms, and less and less I do see redirects. What you are suggesting is to use a redirect on this case, because the request is to have a anchor on your URL ?

Can you please help me out clarifying this points?
Thanks a lot,

Márcio

Ok.

  1. Modus Operandi:
    Once the form as been submitted, and success (or not) message will be show to the user, and the form will not be displayed.

I have this nice function:

/**
 * Função redirecciona:
 */
 function redireciona($pURL)
 {
        if (strlen($pURL) > 0)
        {
            if (headers_sent())
            {
                echo "<script>document.location.href='".$pURL."';</script>\
";
            }
            else
            {
               //o espaço a seguir a Location é importante. cf. HTTP/1.1 standard, section 4.2
               header("Location: " . $pURL);
            }
               exit();
        }
 }

I will call it here:

if($mailer->Send())
 {
      redireciona('MyFormPage.php#ancoraBaixo');
      $resultado['sucesso'] = 'Pedido enviado. Obrigado.';
}

So I’m actually redirecting to the page were my form resides (and were I included the php form logic), then, I’m filling in the array. (Not sure about this order, maybe I should set the success message and only after this redirect).

  1. I have a header.php file included on MyFormPage.php and I believe this should be related to the issue I’m facing when trying to apply your suggestion:

I’m getting redirected to a blank page where the only code I have is:

<!doctype html>
<html lang="pt">
  <head>

    <script>document.location.href='MyFormPage.php#ancoraBaixo';</script>

Any glue about what could be the reason for such a behaviour?

Sorry for all those questions,
Márcio

The behavior is because you are exit()ing immediately after printing <script…

In 3, your code $resultado[‘sucesso’] = ‘Pedido enviado. Obrigado.’; will never execute because redireciona() exits. Aside from that, you need to store variables in the session if you want them to be available after a redirect - redirect sends the browser to a new page (even if it’s the same uri), and http is stateless, so the new page has no idea the old page ever existed.

Perhaps this psuedo code helps (submit and redirect to page.php, which is the form page)


if(isset($_POST['some var like submit'])) {
    // process form
    // if find error set $error flag, or create array of errors etc
    
    if(!$error) {
        $_SESSION['message'] = 'yay, submitted and saved';
        header('Location: /page.php');
        exit(); // do this to stop script executing, despite what browser is up to
    }
}

// html starts now

if(isset($_POST['some var like submit']) && !$error) {
    echo $_SESSION['message']; // yay ...
} else {
    if(isset($error)) { #this works because of no redirect on fail
        // display error messages
    }
    // now show form
}

After you process the form you should use a redirect, it doesn’t matter what your action value is.
header(‘Location: /page.php#anchor’);
So you just need to build that string: /page.php#anchor’. Echo things out till you can see how to do that.

Typically, what you should do is:

  1. Submit the form to a processing script (self or otherwise)
  2. Process/handle errors
  3. Redirect somewhere (self or otherwise) on success

The reason for 3 is to prevent the user resubmitting the data. E.g. someone reads your blog, posts a comment, leaves the page open, and refreshes later to see if any other comments were made.

So in your case, you just need to build the url to redirect on successful submission. You seem to already know what the anchor is? so it should be easy. :slight_smile:

Precious info. A lot to study there. :slight_smile: thanks.

I use it like that, the only difference is that session usage.

hash :smiley:
Well - this will do it :slight_smile: on our form element declaration:

action="<?php echo filter_var($_SERVER['PHP_SELF'], FILTER_SANITIZE_STRING); ?>#ourAnchor"

We just need to MAKE SURE the anchor is not inside:nono: our form tags, because if we use php to display the form only when it as not been submitted, then having an anchor between our form tags will be of no use at all. :x

Thanks a lot for you help into this.
K. Regards,
Márcio