2017-09-24 48 views
0

所以我有我的網站example.com/pp.php窗體的行爲pp.php,因爲該腳本不是一些外部文件,但在該頁面內。問題是我需要把標題(「位置:http://example.com/pp.php#contactForm」);因爲按下發送按鈕後,我想重新加載頁面的確切位置是/pp.php#contactForm。但標題位置不工作...我是新的PHP非常抱歉愚蠢的問題。標題位置沒有轉讓

 <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> 

這是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 = "[email protected]"; 
    $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"); 
    ?> 
+0

您可以設置標題前不輸出任何東西。你的邏輯需要重新思考 – charlietfl

回答

0

試試這個:

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

並確保您此行之前不輸出任何HTML標記通過反正。 像echo $success;

2

你不能header()重定向輸出到DOM後:

記住header()函數發送任何實際輸出之前必須調用,無論是普通的HTML標籤,文件中的空白行或來自PHP的空行。

因此,你需要刪除的行echo聲明:

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");