duanliang5051 2017-08-17 14:49
浏览 346

php验证表单不发送电子邮件

I am trying to make an HTML form work.

My frontend code is this:

<form action="/form_validation.php" target="_blank">
    <p><input class="w3-input w3-padding-16" type="text" placeholder="name" required name="name"></p>
    <p><input class="w3-input w3-padding-16" type="text" placeholder="email" required name="email"></p>
    <p><input class="w3-input w3-padding-16" type="text" placeholder="subject" required name="subject"></p>
    <p><input class="w3-input w3-padding-16" type="text" placeholder="comment" required name="comment"></p>
    <p>
        <button class="w3-button w3-light-grey w3-padding-large" type="submit">
            <i class="fa fa-paper-plane"></i> SEND MESSAGE
        </button>
    </p>
</form>
<p><?php echo $feedbackmsg;?></p>

and the file form_validation.php contains this:

<?php

$nameErr = $emailErr = $commentErr = $subjectErr = "";
$name = $email = $comment = $subject = "";
$feedbackmsg = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['submit'])) {
        if (empty($_POST["name"])) {
            $nameErr = "*Name is required";
        } else {
            $name = validate($_POST["name"]);
            if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
                $nameErr = "*Only letters and white space allowed";
            }
        }

        if (empty($_POST["subject"])) {
            $subjectErr = "*Subject is required";
        } else {
            $subject = validate($_POST["subject"]);
            if (!preg_match("/^[a-zA-Z ]*$/", $subject)) {
                $SubjectErr = "*Only letters and white space allowed";
            }
        }

        if (empty($_POST["email"])) {
            $emailErr = "*Email is required";
        } else {
            $email = validate($_POST["email"]);
            if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
                $emailErr = "*Invalid email format";
            }
        }

        if (empty($_POST["comment"])) {
            $commentErr = "*Write Me something";
        } else {
            $comment = validate($_POST["comment"]);
        }

        if (empty($nameErr) && empty($emailErr) && empty($commentErr)) {
            $to = "myEmail@gmail.com";
            $subject = $name;
            mail($to, $subject, $comment, $email);
            $feedbackmsg = "Your message has sent,<br> thanks";
        }
    }
}

So when action triggered returns back a blank page with the following url with my summury: mysite.com/form_validation.php?name=anastasios&email=myemail%40hotmail.com&subject=mysubject&comment=ena+dio+ena+dio

I think this shoyld be normal. but why I dont have any email if it works properly? Any thoughts? thanks a lot

  • 写回答

1条回答 默认 最新

  • duan7772 2017-08-17 14:52
    关注

    All of your logic is wrapped in this condition:

    if ($_SERVER["REQUEST_METHOD"] == "POST")
    

    And this condition (as well as subsequent conditions therein, if the code got to them) is false because you're using GET instead of POST. Specify the POST method in your form:

    <form action="/form_validation.php" target="_blank" method="post">
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)