1

次の html および php コードを使用してフォームを作成しました。基本的には、スパムの質問が正常に完了したかどうかを確認するチェックを実行します。この場合、フォームを処理し、メールを送信して、ユーザーをサンキューにリダイレクトします。 .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>
4

6 に答える 6

1

header()スペースまたは文字列である場合、すべての出力の前に送信する必要があります。

次のように、事前にフォームの送信を確認する必要があります。

<?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 を表示する関数であり、おそらくfile_get_contents()HTML ファイルの .

于 2013-07-25T13:39:54.560 に答える
1

私は同じ問題に遭遇し、この解決策を見つけました:

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>';
}

お役に立てば幸いです

于 2013-07-25T14:11:22.107 に答える
0

フォームをphpコードの下に置きます

于 2013-07-25T14:26:50.407 に答える
0

これを試してみてください:

header("Location: thankyou.html");
exit();
于 2013-07-25T13:34:56.203 に答える