php表单自动提交

时间:2015-03-05 03:57:48

标签: php html forms

您好我有一个页面index.php,其中包含联系表单,问题是当我加载页面时,就像我按下提交按钮而我不知道为什么......我希望成功或错误的警报仅在按下提交按钮后才显示,而不是在页面加载时..这里是代码:

<?php
if($_POST["submit"]){//form was submitted

    if($_POST['email'] != "" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
        $error .= '<br /><strong>Incorrect Email Address</strong>';
    }
}else{
    $result = '<div class="alert alert-danger"><strong>Form Not Submitted</strong></div>';
}

if($error)
    $result .= '<div class="alert alert-danger"><strong>There was some error(s) in the form: </strong>'.$error.'</div>';
else {
    if(mail("carlosd.dev@gmail.com",$_POST['subject'],"
         Name: ".$_POST['name']."
         Email: ".$_POST['email']."
         Message: ".$_POST['message'])){
        $result = '<div class="alert alert-success">Form Submitted We Will Be In Touch Soon!</div>';
    }else{
        $result = '<div class="alert alert-danger">Sorry the form was not submitted, please try again</div>';
    }

}?>

就在这之上我有我的HTML代码:

    <!DOCTYPE html>
<html>
<head>
    <title>PHP | Practicas</title>
    <meta charset="utf-8" />
    <meta http-http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" type="text/css" href="styles.css">

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">

</head>
<body>

    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3" id="emailForm">

                <h2>Contact Form<br /></h2>
                <form action="index.php" method="post">

                    <div class="form-group">
                        <label for="name">Your name: </label>
                        <div class="input-group">
                            <input type="text" name="name" class="form-control" placeholder="Your Name" required="true" value="<?php echo $_POST['name']; ?>"/>
                            <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="email">Your email: </label>
                        <div class="input-group">
                            <input type="email" name="email" class="form-control" placeholder="Your Email" required="true"/>
                            <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="subject">Subject: </label>
                        <div class="input-group">
                            <input type="text" name="subject" class="form-control" placeholder="Subject of message" required="true" value="<?php echo $_POST['subject']; ?>"/>
                            <span class="input-group-addon"><span class="glyphicon glyphicon-tag"></span></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <textarea name="message" class="form-control" placeholder="Enter the message" required="true"><?php echo $_POST['message']; ?></textarea>
                    </div>
                    <input type="submit" name="submit" class="btn btn-success btn-lg" value="Send!" />

                </form>

            </div>
        </div>
    </div>
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
 </body>
 </html>
 </head>

我该怎么做才能解决这个问题?

4 个答案:

答案 0 :(得分:0)

PHP 代码置于顶部,将 HTML 表单置于底部 改变这个

if($_POST["submit"]){//form was submitted

用这个

if(isset($_POST["submit"])){//form was submitted

也改变了这个

if($_POST['email'] != "" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){

使用此

if(!empty($_POST['email']) AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){

也会更改“错误”检查

if($error)

<强>与

if(strlen($error)>0) {

而你的其他人应该是

{ else

所有代码

 <?php
    if(isset($_POST["submit"])){//form was submitted
        if(!empty($_POST['email']) AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
            $error .= '<br /><strong>Incorrect Email Address</strong>';
        }
    } else {
        $result = '<div class="alert alert-danger"><strong>Form Not Submitted</strong></div>';
    }
    if(strlen($error)>0) {
        $result .= '<div class="alert alert-danger"><strong>There was some error(s) in the form: </strong>'.$error.'</div>';
    } else {
        if(mail("carlosd.dev@gmail.com",$_POST['subject'],"
             Name: ".$_POST['name']."
             Email: ".$_POST['email']."
             Message: ".$_POST['message'])){
            $result = '<div class="alert alert-success">Form Submitted We Will Be In Touch Soon!</div>';
        } else {
            $result = '<div class="alert alert-danger">Sorry the form was not submitted, please try again</div>';
        }
    }
    ?>

答案 1 :(得分:0)

同时检查此

添加到HTML表单

<input type="hidden" name="submit" value="true">

PHP检查表单是否已提交

<?php
if($_POST['submit']=='true') {
//true
} esle {
//false
}
?>

这对你有用

答案 2 :(得分:0)

您的网页是一个自我调用的元素。发生的事情是首次加载它试图在email数组中找到$_POST和其他输入。这自然不存在,因此警报 使用<form action='<?php echo $_SERVER['PHP_SELF']?>'' method="post">代替<form action="index.php" method="post">。这很可能会解决问题。

偏离主题的建议:使用filter_input(INPUT_POST,'submit')代替$_POST['index']。它更安全,值得推荐。


您的代码在我的计算机(WAMP服务器)中运行时似乎有很多错误。初始化$error$result变量。这似乎有所帮助。

答案 3 :(得分:0)

使用此isset()

if(isset($_POST['submit'])){

//here your code

}