Php 当我';我在按按钮吗?

Php 当我';我在按按钮吗?,php,html,Php,Html,我创建了一个PHP脚本,允许我网站上的用户通过输入电子邮件并按下提交按钮来注册我们的新闻稿。然后,它会给用户一个通知,说他们已经成功注册了我们的时事通讯,我会收到一封电子邮件。但是,当您加载页面()时,它会向我发送一封电子邮件,告诉我有人注册了,但从未按下提交按钮,并且它不会显示成功通知。最重要的是,当你试图正确使用表单时,它是不起作用的。以下是我的PHP: if($_POST["submit"]) { if(!$_POST["email"]) { $error =

我创建了一个PHP脚本,允许我网站上的用户通过输入电子邮件并按下提交按钮来注册我们的新闻稿。然后,它会给用户一个通知,说他们已经成功注册了我们的时事通讯,我会收到一封电子邮件。但是,当您加载页面()时,它会向我发送一封电子邮件,告诉我有人注册了,但从未按下提交按钮,并且它不会显示成功通知。最重要的是,当你试图正确使用表单时,它是不起作用的。以下是我的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>';
    }
}

?>
if($\u POST[“提交”]){
如果(!$_POST[“email”]){
$error='请输入您的电子邮件';
}
如果($error){
$result='提交表单时出错:'.$error';
}
}否则{
如果(邮件)(“科林。vrugteman@hotmail.com“,”时事通讯注册“,”电子邮件:“.$\u POST['Email'])){
$result='谢谢。您已报名参加我们的时事通讯!';
}
}
?>

我试着在HTML代码的顶部和底部运行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>';
            }
         }
}
if($\u POST[“提交”]){
如果(!$_POST[“email”]){
$error='请输入您的电子邮件';
}
如果($error){
$result='提交表单时出错:'.$error';
}否则{
如果(邮件)(“科林。vrugteman@hotmail.com“,”时事通讯注册“,”电子邮件:“.$\u POST['Email'])){
$result='谢谢。您已报名参加我们的时事通讯!';
}
}
}

你的结构是错误的,所以它总是碰到那个块。您的if的结束括号位于错误的位置。试试这个

<?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>';
        }
    }
}
?>

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


我个人会把它写得更加动态,带有错误代码

下面是我个人编写代码的方式。我并不是说这是好是坏,而是说我会怎么做


表单在哪里?您是否检查了错误日志?通过修复格式/缩进,您应该能够了解原因。似乎,如果未设置提交,它将向Hotmail的某个Colin发送电子邮件。因此,从您的描述来看,这听起来是正确的,如果未按下submit,您将收到一封电子邮件。如果您得到一个对您有帮助的答案,请务必选中答案旁边的小复选标记按钮,以表示这就是答案。您正在呼叫mail(“科林。vrugteman@ho..在if语句中,括号的设置方式是,即使未提交任何内容,也会运行该语句。@BojanBedrač在if语句中调用
mail()
,因为它返回true或false。如果邮件已发送,则返回true,因此给出“谢谢”输出。@GrumpyCrouton谢谢,我不是想说在if语句中调用方法是非法的。我是想指出,
mail()
在对if语句求值时被调用,并且由于括号的结构,每次都被调用。@GrumpyCrouton原始代码中不是这样的。这是唯一一个不会生成警告的语句,即如果所有内容都正确,则未设置$error。我们必须假设大多数情况下所有内容都正确那么为什么每次运行页面时都会生成警告呢?@Andreas如果
$\u POST['submit']
为空或未设置。没有错误时不应生成错误。好吧,这是真的。我通常习惯于删除php解释器创建错误/警告的任何方法。@Andreas这也不应该这样做,因为它会在对变量执行任何操作之前检查变量是否存在。@Andreas哦,谢谢,我误解了您的第一步操作评论:)
<?
    //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>';
            }
        }
    }
?>