联系表格正在发送电子邮件,但在提交后没有回复

时间:2017-11-02 21:53:33

标签: javascript php forms

我的联系表单设置在提交后会重定向回我的主页,但由于某种原因它只是停留在同一页面上。我正在尝试在发送电子邮件后收到确认,然后重定向回我的主页。我正在使用php和javascript ...........................................

function create(string $password, $iterations=36000, $algorithm='sha256', $iterations=36000) : string{
    $salt = base64_encode(openssl_random_pseudo_bytes(9));

    $hash = hash_pbkdf2($algorithm, $password, $salt, $iterations, 32, true);

    return 'pbkdf2_' . $algorithm . '$' . $iterations . '$' . $salt . '$' . base64_encode($hash);
}

index2.php    

<div class="col-sm-6 col-sm-offset-3">

    <h3>Send me a message</h3>
    <form role="form" id="contactForm" action="index2.php" method="POST">
     <div class="row">
            <div class="form-group col-sm-6">
                <label for="name" class="h4">Name</label>
                <input type="text" class="form-control" id="name" placeholder="Enter name" required>
        </div>
        <div class="form-group col-sm-6">
            <label for="email" class="h4">Email</label>
            <input type="email" class="form-control" id="email" placeholder="Enter email" required>
        </div>
    </div>
    <div class="form-group">
        <label for="message" class="h4 ">Message</label>
        <textarea id="message" class="form-control" rows="5" 
placeholder="Enter your message" required></textarea>
    </div>
    <button type="submit" id="form-submit" class="btn btn-primary btn-lg 
pull-right ">Submit</button>
<div id="msgSubmit" class="h3 text-center hidden">Message Submitted!</div>
   </form>

JS

 <meta http-equiv="refresh" content="0; url=http://myurl.com/" />


</header>

<?php
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

$EmailTo = "arash281pro@live.com";
$Subject = "New Message Received";

// prepare email body text
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";

$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";

$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success){
   echo "success";
}else{
echo "invalid";
}
?>

2 个答案:

答案 0 :(得分:0)

您实际上并没有重定向到您的主页。 为此,您需要在提交函数后添加类似的内容:

window.location.replace("index2.php");

答案 1 :(得分:0)

不要在index2.php中使用<meta http-equiv="refresh" content="0; url=http://myurl.com/" />,只需在脚本末尾添加header("Location: http://myurl.com/");,而不是“成功”或“无效”消息。在PHP代码启动之前还要删除任何HTML,以防止出现“已经发送的标题,在线开始输出......”等错误。