Submit to the same page and return to the same page

Asked

Viewed 1,353 times

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 after submitting the form to return to the same Action to show the message was sent.

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

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

Follow the example 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>

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.