表单在页面加载空白和pa上提交

时间:2013-10-23 00:56:18

标签: javascript php html5 forms

我的网站http://www.team2457.us/contact_us/sponsorship.php上有一个表单。我第一次填写表单它工作正常,PHP脚本做我想要的。但是现在每次加载页面时都会显示一个弹出窗口,表示表单已经成功完成,即使表单尚未加载,我甚至没有点击顶峰按钮。所以我想知道1)为什么它会像我点击顶峰按钮一样? 2)为什么表单通过验证我使用?

这是我的代码:

<!DOCTYPE HTML>
<html>
    <head>
        <base href="http://www.team2457.us" >
        <title>Team 2457 The Law</title>
        <link rel="stylesheet" type="text/css" href="../styles/navstyle.css">
        <link rel="stylesheet" type="text/css" href="../styles/style.css">
        <style>
            label {
                display:block;
                margin-top:20px;
                letter-spacing:2px;
            }
            form {
                margin:0 auto;
                width:459px;
            }
            input, textarea {
                width:439px;
                height:27px;
                background:#efefef;
                border:1px solid #dedede;
                padding:10px;
                margin-top:3px;
                font-size:0.9em;
                color:#3a3a3a;
                border-radius:5px;
                -moz-border-radius:5px;
                -webkit-border-radius:5px;
            }
            textarea {
                height:213px;
                background:#efefef;
            }
            input:focus, textarea:focus {
                border:1px solid #97d6eb;
            }
            #submit {
                width:127px;
                height:38px;
                background: #efefef;
                border:none;
                margin-top:20px;
                cursor:pointer;
            }

        </style>

    </head>

    <body>
        <div class="wrapper">
            <header id="banner">
                <img src="imgs/banner.png" align="center" alt="banner"/>
            </header>
            <nav>
                <?php include '../nav.php'; ?>
            </nav>

            <section id="content">
                <?php
                    $name = $_POST['name'];
                    $email = $_POST['email'];
                    $message = $_POST['message'];
                    $from = 'From: potential sponsor'; 
                    $to = 'myemail@gmail.com'; 
                    $subject = 'Sponsor';

                    $body = "From: $name\n E-Mail: $email\n Message:\n $message";


                    if (mail ($to, $subject, $body, $from)) { 
                        echo '<script>alert("The form was completed succesfuly.");</script>';
                        } 
                    else { 
                        echo '<script>alert("Something whent wrong, try again.");<script>'; 
                        }
                ?>

                <form method="post" action="/contact_us/sponsorship.php">

                    <label>Name</label>
                    <input name="name" placeholder="Type Here" value="">

                    <label>Email</label>
                    <input name="email" type="email" placeholder="Type Here" value="">

                    <label>Message</label>
                    <textarea name="message" placeholder="Type Here" value=""></textarea>

                    <input id="submit" name="submit" type="submit" value="Submit">

                </form>
            </section>

            <footer>

            </footer>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

每次加载页面时都会调用您的邮件功能。您应该考虑在if(isset())函数中放置处理$ _POST变量和mail函数的PHP逻辑,如下所示:

if(isset($_POST["name"])){
    //Code to handle input validation and email sending goes here
}
相关问题