0

I have form in my site example.com/pp.php and that form´s action is pp.php because that script is not some external file, but inside of that page. The problem is I need to put header("Location: http://example.com/pp.php#contactForm"); because after pressing Send button I want to reload page on exact position which is /pp.php#contactForm. But header Location is not working.

        <form action="pp.php" method="post">
        <label>Name:</label>
        <input type="text" name="name" value="<?php if($_POST['name']) { 
        echo $_POST['name']; } ?>" />
        <label>Email:</label>
        <input type="text" name="email" value="<?php if($_POST['email']) 
        { echo $_POST['email']; } ?>" />    
        <label>Message:</label><br />
        <textarea name="message" rows="20" cols="20"><?php 
        if($_POST['message']) { echo $_POST['message']; } ?></textarea>         
        <label><img src="captcha.php"></label>
        <input type="text" name="code"> <br /> 
        <input type="submit" class="submit" name="submit" value="Send 
        message" /> 
        </form>

This is php

<?php
    if (isset($_POST['submit'])) {
    $error = "";

    if (!empty($_POST['name'])) {
    $name = $_POST['name'];
    } else {
    $error .= "You didn't type in your name. <br />";
    }

    if (!empty($_POST['email'])) {
    $email = $_POST['email'];
      if (!preg_match("/^[a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*
(\.[a-z]{2,3})$/i", $email)){ 
      $error .= "The e-mail address you entered is not valid. <br/>";
      }
    } else {
    $error .= "You didn't type in an e-mail address. <br />";
    }

    if (!empty($_POST['message'])) {
    $message = $_POST['message'];
    } else {
    $error .= "You didn't type in a message. <br />";
    }

    if(($_POST['code']) == $_SESSION['code']) { 
    $code = $_POST['code'];
    } else { 
    $error .= "The captcha code you entered does not match. Please try 
again. <br />";    
    }

    if (empty($error)) {
    $from = 'From: ' . $name . ' <' . $email . '>';
    $to = "mail@gmail.com";
    $subject = "New contact form message";
    $content = $name . " has sent you a message: \n" . $message;
    $success = "<h3>Thank you! Your message has been sent!</h3>";
    mail($to,$subject,$content,$from);
    }
    }
    ?>
        <?php
        if (!empty($error)) {
        echo '<p class="error"><strong>Your message was NOT sent<br/> The 
following error(s) returned:</strong><br/>' . $error . '</p>';
        } elseif (!empty($success)) {
        echo $success;
        }
        header("Location: http://example.com/pp.php#contactForm");
    ?>
Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

2

You can't redirect with header() after outputting to the DOM:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

As such, you'll need to remove the echo statements in your lines:

if (!empty($error)) {
  echo '<p class="error"><strong>Your message was NOT sent<br/> The 
following error(s) returned:</strong><br/>' . $error . '</p>';
} elseif (!empty($success)) {
  echo $success;
}

Before calling:

header("Location: http://example.com/pp.php#contactForm");
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
0

Try this:

 header('Location: pp.php#contactForm');

And make sure you do not output any html tag through anyway before this line. like the echo $success;

M0ns1f
  • 2,705
  • 3
  • 15
  • 25