Submit to same page and return to same section

1

Friends,

I have a one-page PHP site that at the bottom of the page has a contact form that submits to the same page.

I wanted to submit the form after returning to the same section to show the message was sent.

I tried to do this with redirect with this code below, but it did not work because when redirected it does not show the message.

$redir = '#contact';
header("location:$redir");

Following sample code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>My Site</title>
    <style type="text/css">
        *{margin: 0; padding: 0}
        #home, #work, #contact{
            height: 500px;
            border: solid 1px #000;
        }
        .resultado{
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <section id="home">
        <p>Home Section</p>
    </section>
    <section id="work">
        <p>Work Section</p>
    </section>
    <section id="contact">
        <div class="resultado">
            <?php
            if (isset($_POST['nome'])) {
                $nome = $_POST['nome'];
                echo '<p>Mensagem enviada!</p>';
                echo '<p>'.$nome.'</p>';
            }
            ?>
        </div>
        <form action="index.php" method="post">
            <input type="text" name="nome">
            <input type="submit" value="Enviar">
        </form>
    </section>
</body>
</html>
    
asked by anonymous 30.04.2017 / 18:39

1 answer

1

Use indicator in form action="index.php#nomequalquer"

and where you want to direct <a name="nomequalquer"></a>

<!DOCTYPE html>
 html>
  <head>
    <meta charset="utf-8">
     <title>My Site</title>
     <style type="text/css">
    *{margin: 0; padding: 0}
    #home, #work, #contact{
        height: 500px;
        border: solid 1px #000;
    }
    .resultado{
        background-color: lightblue;
    }
    </style>
  </head>
 <body>
<section id="home">
    <p>Home Section</p>
</section>
<section id="work">
    <p>Work Section</p>
</section>
<section id="contact">
    <div class="resultado">
     <a name="nomequalquer"></a>
        <?php
        if (isset($_POST['nome'])) {
            $nome = $_POST['nome'];
            echo '<p>Mensagem enviada!</p>';
            echo '<p>'.$nome.'</p>';
        }
        ?>
    </div>
    <form action="index.php#nomequalquer" method="post">
        <input type="text" name="nome">
        <input type="submit" value="Enviar">
    </form>
    </section>
  </body>
 </html>
    
30.04.2017 / 18:51