0

我的网站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>
4

1 回答 1

0

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

if(isset($_POST["name"])){
    //Code to handle input validation and email sending goes here
}
于 2013-10-23T01:06:39.127 回答