簡體   English   中英

如何讓我的PHP表單重定向工作?

[英]How do I get my PHP form redirect to work?

我創建了一個包含以下html和php代碼的表單,基本上它運行檢查以查看垃圾郵件問題是否已成功完成,在這種情況下它應該處理表單,發送電子郵件並將用戶重定向到thankyou .html頁面。 但是,目前,成功時,表單只是重新加載相同的頁面,並沒有重定向到感謝頁面,但是我收到了確認電子郵件...

在下面的代碼中是否有一些我錯過/出錯的東西來阻止這個行動?

<form id="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table>
        <tr>
            <td align="right"><label for="name">Name:<span class="error"><?php echo $nameErr;?></span></label></td>
            <td><input type="text" name="name" id="name" /></td>
        </tr>
        <tr>
            <td align="right"><label for="email">Email:<span class="error"><?php echo $emailErr;?></span></label></td>
            <td><input type="text" name="email" id="email"  /></td>
        </tr>
        <tr>
            <td align="right"><label for="telephone">Telephone:<span class="error"><?php echo $telErr;?></span></label></td>
            <td><input name="telephone" type="text" id="telephone" /></td>
        </tr>
        <tr>
            <td align="right"><label for="field">Anti-spam question:<span class="error"><?php echo $spamErr;?></span></label></td>
            <td><input name="field" type="text" id="field" value="Complete the Beatles lyric: all you need is...?"  onfocus="if(this.value==this.defaultValue) this.value='';"  onblur="if(this.value == ''){this.value='Complete the Beatles lyric: all you need is...?';}" /></td>
        </tr>
        <tr>
            <td align="right"><label for="message">Message:</label></td>
            <td><textarea name="message"></textarea></td>
        </tr>
        <tr>
            <td colspan="2" align="right"><img src="images/contactdots.gif" width="338" height="10" alt="" /></td>
        </tr>
        <tr>
            <td colspan="2" align="right"><input id="button" type="Submit" value="SUBMIT" alt="submit" /></td>
        </tr>
    </table>
<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){

// Pick up the form data and assign it to variables
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$tel = $_POST['telephone'];
$comments = stripslashes($_POST['message']);
$field = strtolower($_POST['field']);
$spam_check = 'love';
if($field == $spam_check){
// Build the email (replace the address in the $to section with your own)
$to = 'my@email.com';
$subject = "The Vintage Affair Web Quote enquiry";
$comments = "Name: $name \nEmail: $email \nTelephone: $tel \n\nDetails: $comments";
$headers = "From: my@email.com" . PHP_EOL . "Reply-To: my@email.com";

// Send the mail using PHPs mail() function
mail($to, $subject, $comments, $headers);

// Redirect
header("Location: thankyou.html");
}
else{ echo '<div class="spam">*You got the Beatle\'s lyric wrong, please try again*</div>'; }
}

?>
</form>

問題是你在調用header()之前輸出數據,它不會像這樣工作。

將所有PHP放在表單上方。

header()必須在ANY輸出之前發送,如果它是空格或字符串。

您必須先檢查表單提交,如下所示:

<?php

if( isset($_POST['myform'] )
{
 if( spam_check($_POST['myform']) === true )
 {
   header("Location: thankyou.html");
 }
 else 
 { 
  echo '<div class="spam">*You got the Beatle\'s lyric wrong, please try again*</div>';
 }
}
else
{
  display_form();
}
?>

dispay_form()是一個顯示所有HTML的函數,可能是HTML文件的file_get_contents()

我遇到了同樣的問題,我找到了這個解決方案:

if (!headers_sent()){
    header('Location: thankyou.html');
}
else {
    echo '<script>';
    echo 'window.location.href="thankyou.html";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0; url=thankyou.html" />';
    echo '</noscript>';
}

我希望它對你有用

試試吧:

header("Location: thankyou.html");
exit();

好吧,問題是你在運行支票之前實際輸出了屏幕上的內容。 在您輸入HTML代碼的任何輸出之前發送標頭。

使用ob_start(); ob_end_flush()函數; 在文件的開頭配對的東西

<?php 
   ob_start(); // this stores the output in a buffer for later use
?>

html code here

<?php



if($_SERVER['REQUEST_METHOD'] == "POST"){

    // Pick up the form data and assign it to variables
    $name = stripslashes($_POST['name']);
    $email = stripslashes($_POST['email']);
    $tel = $_POST['telephone'];
    $comments = stripslashes($_POST['message']);
    $field = strtolower($_POST['field']);
    $spam_check = 'love';

    if($field == $spam_check){
        // Build the email (replace the address in the $to section with your own)
        $to = 'my@email.com';
        $subject = "The Vintage Affair Web Quote enquiry";
        $comments = "Name: $name \nEmail: $email \nTelephone: $tel \n\nDetails: $comments";
        $headers = "From: my@email.com" . PHP_EOL . "Reply-To: my@email.com";

        // Send the mail using PHPs mail() function
        mail($to, $subject, $comments, $headers);

        // Redirect
        header("Location: thankyou.html");
   }
    else echo '<div class="spam">*You got the Beatle\'s lyric wrong, please try again*</div>';
}

ob_end_flush(); // this is for printing the buffered output
?>

ob_end_flush()函數; 在末尾

把表格放在php代碼下面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM