douyi3833 2017-10-03 08:18
浏览 23

我的联系表单有什么问题,一旦我点击提交按钮就会一直说错误! 请再试一次

<?php

    $to = 'marivelcoresis@gmail.com';  // please change this email id

    $errors = array();
    // print_r($_POST);

    // Check if name has been entered
    if (!isset($_POST['name'])) {
        $errors['name'] = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errors['email'] = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!isset($_POST['message'])) {
        $errors['message'] = 'Please enter your message';
    }

    $errorOutput = '';

    if(!empty($errors)){                                                        

        $errorOutput .= '<div class="alert alert-danger alert-dismissible" role="alert">';
        $errorOutput .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';

        $errorOutput  .= '<ul>';

        foreach ($errors as $key => $value) {
            $errorOutput .= '<li>'.$value.'</li>';
        }

        $errorOutput .= '</ul>';
        $errorOutput .= '</div>';

        echo $errorOutput;
        die();
    }



    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = $email;
    $subject = 'Contact Form : Texas Lawers Responsive HTML5 Template';

    $body = "From: $name
 E-Mail: $email
 Message:
 $message";


    //send the email
    $result = '';
    if (mail ($to, $subject, $body)) {
        $result .= '<div class="alert alert-success alert-dismissible" role="alert">';
        $result .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
        $result .= 'Thank You! I will be in touch';
        $result .= '</div>';

        echo $result;
        die();
    }

    $result = '';  
    $result .= '<div class="alert alert-danger alert-dismissible" role="alert">';
    $result .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
    $result .= 'Something bad happend during sending this message. Please try again later';           
    $result .= '</div>';

    echo $result;
    die();


?>
  • 写回答

1条回答 默认 最新

  • douhandie6615 2017-10-03 08:30
    关注

    At first glance and with the info provided, my suggestion is to change

    $email = $_POST['email'];
    

    with

    $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
    

    In addition to this it will be a good idea to filter all $_POST variables, since the checks are not sufficient to clear any malicious code hidden in the text.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序