当我按下按钮时,为什么我的按钮会触发PHP?

时间:2017-07-11 14:39:05

标签: php html

我创建了一个PHP脚本,允许我网站上的用户通过输入他们的电子邮件并按下提交按钮来注册我们的新闻通讯。然后,它会通知用户他们已成功注册我们的简报,我会收到一封电子邮件。但是,当您加载页面(https://papaya-os.000webhostapp.com/)时,它会向我发送一封电子邮件,说明有人已注册,但提交按钮从未被按下,并且它没有显示成功通知。最重要的是,当您尝试正确使用表单时,它不起作用。这是我的PHP:

if($_POST["submit"]) {

    if(!$_POST["email"]) {

        $error = 'Please Enter Your Email';

    }
    if ($error) {

        $result='<div class="alert alert-danger"><strong>There were error(s) in submitting the form: ' .$error. '</strong></div>';

    }

} else {
    if(mail("colin.vrugteman@hotmail.com", "Newsletter Sign Up", "Email: ".$_POST['email'])) {

        $result='<div class="alert alert-success">Thank You. You have been signed up for our newsletter!</div>';
    }
}

?>

我尝试在顶部运行PHP,然后运行HTML代码的底部,但都没有运行。任何帮助表示赞赏。谢谢!

4 个答案:

答案 0 :(得分:3)

你在括号中有错误

if($_POST["submit"]) {

        if(!$_POST["email"]) {

            $error = 'Please Enter Your Email';

        }
        if ($error) {

            $result='<div class="alert alert-danger"><strong>There were error(s) in submitting the form: ' .$error. '</strong></div>';



         } else {
            if(mail("colin.vrugteman@hotmail.com", "Newsletter Sign Up", "Email: ".$_POST['email'])) {

              $result='<div class="alert alert-success">Thank You. You have been signed up for our newsletter!</div>';
            }
         }
}

答案 1 :(得分:1)

<?
    //Here you should be checking if submit is set, or `empty()` is my preferred function. You could also use `isset()` but you would want to negate that, so it would be `!isset()`.
    if(empty($_POST["submit"])) {
        //If email is empty, set $error
        if(empty($_POST["email"])) {
            $error = 'Please Enter Your Email';
        }
        //if $error is not empty, set $result
        if (!empty($error)) {
            $result = '<div class="alert alert-danger"><strong>There were error(s) in submitting the form: ' . $error . '</strong></div>';
        }
    //if $_POST["submit"] IS set, send email.
    } else {
        //if $_POST['submit'] is NOT empty, and IS set, send email.
        if(isset($_POST['submit'])) {
            if (mail("colin.vrugteman@hotmail.com", "Newsletter Sign Up", "Email: " . $_POST['email'])) {
                $result = '<div class="alert alert-success">Thank You. You have been signed up for our newsletter!</div>';
            }
        }
    }
?>

试试这段代码,我添加了评论来解释发生了什么。

我个人原本会用错误代码把它写成更动态。

以下是我亲自编写代码的方法。我没有声称这更好或更糟,我只是如何做到这一点。 :)

<?php
    //if submit is empty there was an issue
    if (empty($_POST['submit'])) {

        //create an empty array to store errors
        $errors = array();

        //loop through each _POST element to see what you are missing. If any value is empty or not set, it will be added to the errors.
        foreach ($_POST as $key => $value) {
            //skip key submit because we don't want that to appear in the errors, you could also add more you want to skip here.
            if ($key == "submit")
                continue;
            //If value is empty/not set, add it to the error array.
            if (empty($value)) {
                array_push($errors, "Please provide " . ((in_array($key[0], $vocals)) ? "an" : "a") . " {$key}");
            }
        }
        //check if $errors has any contents
        if (!empty($errors)) {

            //create $results filled with the dynamic errors from above. If there is only 1 error, properly format the sentence.
            $result = '<div class="alert alert-danger"><strong>There '.((count($errors)==1)? "was an error" : "were errors" ).' while submitting the form: ';
            foreach ($errors as $key => $value) {
                $result .= "{$value}";
                if ($key != count($errors) - 1) {
                    $result .= ", ";
                }
            }
            $result .= '</strong></div>';
        }
    } else {
        //if if $_POST['submit'] is not empty and IS set, send email.
        if(isset($_POST['submit'])) {
            $to      = "test@email.com";
            $subject = "Newsletter Sign Up";
            $message = "Email: {$_POST['email]}";
            if (mail($to, $subject, $message)) {
                $result = '<div class="alert alert-success">Thank You. You have been signed up for our newsletter!</div>';
            }
        }
    }
?>

答案 2 :(得分:0)

你的结构是错误的,所以总是碰到那个块。如果你在错误的地方,你有一个结束括号。试试这个。

<?php
if($_POST["submit"]) {
    if(!$_POST["email"]) {
        $error = 'Please Enter Your Email';     
    }
    if ($error) {
        $result='<div class="alert alert-danger"><strong>There were error(s) in submitting the form: ' .$error. '</strong></div>';
    } else {
        if(mail("colin.vrugteman@hotmail.com", "Newsletter Sign Up", "Email: ".$_POST['email'])) {
            $result='<div class="alert alert-success">Thank You. You have been signed up for our newsletter!</div>';
        }
    }
}
?>

答案 3 :(得分:-1)

如果您无法访问调试器,则打印$ _POST [“submit”]的值非常有用