提交表单时停止页面重定向到php脚本文件

时间:2014-09-27 04:05:48

标签: php html forms

这是一个名为sendmail.php的php文件,用于验证并发送联系表单中的电子邮件(包括在下面)

我遇到的麻烦是点击提醒时它将页面重定向到sendemail.php,因为我假设,表单动作= sendmail.php

如何解决这个问题,以便它保留在联系页面上,用户可以从中断的地方继续?我已经尝试过使用标题来感谢页面,但无济于事!

if (isset($_POST["submit"])) {
    $validate = validateInput($_POST["name"], $_POST["message"], $_POST["subject"]);
    if ($validate==FALSE) {
        $error_msg = "Please fill out all information";
        echo '<script type="text/javascript">
            alert("'.$error_msg.'");
        </script>';
    } else {
        $mailcheck = spamcheck($_POST["email"]);
        if ($mailcheck==FALSE) {
            $error_msg = "Invalid email address";
            echo '<script type="text/javascript">
            alert("'.$error_msg.'");
        </script>';
        } else {
        $email = $_REQUEST['email'] ;
        $message = $_REQUEST['message'] ;
        $subject = $_REQUEST['subject'] ;

        mail( "email@hotmail.com", $subject, $message, "From: $email" );
        header( "Location: http://www.thankyou.html" );
        }
    }
}

<form method="post" action="sendmail.php">
    <input type="text" name="name" maxlength="50" placeholder="Name" class="contact-standard" /></br></br>
    <input type="email" name="email" placeholder="Email" class="contact-standard"></input></br></br>
    <input type="text" name="subject" placeholder="Subject" class="contact-standard"></input></br></br>
    <textarea name="message" placeholder="Message" class="contact-message"></textarea></br></br>
    <input type="submit" name="submit" value="Send Message" class="contact-submit"></input></br></br>
</form>

4 个答案:

答案 0 :(得分:1)

我没有使用php的经验,但我认为你需要在没有验证表单时回复false。

 if ($validate==FALSE) {
    $error_msg = "Please fill out all information";
    echo '<script type="text/javascript">
        alert("'.$error_msg.'");
    </script>';
   return false;//Form is not validated stop from being submitted.
}

答案 1 :(得分:1)

使用jQuery验证表单BEFORE用户提交表单,制作&#34;谢谢你&#34;在成功提交时发生在同一页面上。这样您就可以使用PHP来处理电子邮件发送。

<?php
    if(isset($_POST["submit"])) {
            $email      =   $_POST['email'] ;
            $message    =   strip_tags($_POST['message']);
            $subject    =   strip_tags($_POST['subject']);

            if(mail("email@hotmail.com", $subject, $message, "From: $email")) { ?>
            <h3>Thank you, come again!</h3>
        <?php   }
            else { ?>
            <h3>An error occurred. My bad!</h3>
            <?php }
        } ?>

<form method="post" action="contact.php" id="emailer">
    <input type="text" name="name" maxlength="50" placeholder="Name" class="contact-standard" /></br></br>
    <input type="email" name="email" placeholder="Email" class="contact-standard"></input></br></br>
    <input type="text" name="subject" placeholder="Subject" class="contact-standard"></input></br></br>
    <textarea name="message" placeholder="Message" class="contact-message"></textarea></br></br>
    <input type="submit" name="submit" value="Send Message" class="contact-submit"></input></br></br>
</form>

<style>
.error { color: red; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>

<script>
$(document).ready(function() {
    // On submit, validate below fields.
    $("#emailer").validate({
        rules: {
            name: "required",
            email: {
                    required: true,
                    email: true
                },
            subject: "required",
            message: "required"
        },
        messages: {

            name: "required",
            email: {
                    required: "Required",
                    email: "must be valid email"
                },
            subject: "required",
            message: "required"
        }
    });
});
</script>

答案 2 :(得分:1)

试试这个并阅读评论。 评论:每次验证失败时添加此行 window.location =“Your-Form-File-Name.php”;

if (isset($_POST["submit"])) {
    $validate = validateInput($_POST["name"], $_POST["message"], $_POST["subject"]);
    if ($validate==FALSE) {
        $error_msg = "Please fill out all information";
        echo '<script type="text/javascript">
            alert("'.$error_msg.'");
                window.location= "Your-Form-File-Name.php"; **//This line added by me**
****
        </script>';
    } else {
        $mailcheck = spamcheck($_POST["email"]);
        if ($mailcheck==FALSE) {
            $error_msg = "Invalid email address";
            echo '<script type="text/javascript">
            alert("'.$error_msg.'");
                window.location= "Your-Form-File-Name.php"; ////This line added by me
**//Added above line**
        </script>';
        } else {
        $email = $_REQUEST['email'] ;
        $message = $_REQUEST['message'] ;
        $subject = $_REQUEST['subject'] ;

        mail( "email@hotmail.com", $subject, $message, "From: $email" );
        header( "Location: http://www.thankyou.html" );
        }
    }
}

答案 3 :(得分:0)

这是sendmail.php,对吧?如果你想重定向到contact.php只是header()到那里。您将表单操作设置为sendmail.php,这意味着您也设置为此文件,如果您从表单标记中删除操作prop,它将执行相同的操作。只需将标题值更改为contact.php或

即可

print'location.href =“'。$ url_to_contact_php。'”';