【问题标题】:PHP and HTML form mail send with javascript alert not working [duplicate]带有javascript警报的PHP和HTML表单邮件不起作用[重复]
【发布时间】:2016-09-10 11:12:40
【问题描述】:

我的知识非常有限。我尽力去理解。我想从联系表格发送邮件,成功后显示警报消息并保持在同一页面上。邮件没有发送,似乎我的代码中有一些错误。请帮忙。

<form action="" method="post">
    <div class="col-md-6">
        <input name="name" type="text" class="form-control" placeholder="Name">
    </div>
    <div class="col-md-6">
        <input name="email" type=“text” class="form-control" placeholder="Email">
    </div>
    <div class="col-md-12">
       <input name="subject" type="text" class="form-control" placeholder="Subject">
    </div>
    <div class="col-md-12">
        <textarea name="message" class="form-control" placeholder="Message" rows="4"></textarea>
    </div>
    <div class="col-md-8">
        <input type="submit" class="form-control text-uppercase" name="send" value="submit" />
    </div>
</form>

<?php

if ($_POST['send']) { 
    $ToEmail = 'info@autonomousdata.com'; 
    $EmailSubject = $_POST['subject']; 
    $mailheader = "From: ".$_POST['email']."\r\n"; 
    $mailheader .= "Reply-To: ".$_POST['email']."\r\n"; 
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n" .
                                            "X-Mailer: PHP/" . phpversion(); 
    $MESSAGE_BODY = "Name: ".$_POST['name']."\n"; 
    $MESSAGE_BODY .= "Email: ".$_POST['email']."\n"; 
    $MESSAGE_BODY .= "Comment: ".$_POST['message'].""; 

    if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader)!==true) {
?>
    <script type='text/javascript'> alert('failed to send the mail'); </script>
<?php
        die('Fail to send');
    } else{
?>
    <script type='text/javascript'>alert('Thank you for contacting us! All information received will always remain confidential. We will contact you as soon as we review your message.');
    </script>

<?php       
    }           
}
?>

【问题讨论】:

  • 您是从本地服务器 (localhost) 还是实时服务器发送电子邮件?
  • 您在 html 表单中使用了错误的 email 引号
  • 不是本地主机。直播服务器。该网站托管在服务器中。他们支持 php 和电子邮件。我问他们,他们说服务器端的 php 配置是正确的。问题是我的编码技巧...... :(
  • @RamRaider 感谢您的关注。我猜我的文本编辑器有一些设置。但问题不止于此。我一加载页面,JS 警报就会显示,然后其他警报就会显示!这个表单代码是更好的方法吗?

标签: javascript php html email


【解决方案1】:

HTML 表单在 type='text' 周围有可疑的引号,我修改了 php 以添加一些基本的变量检查和清理~希望它会有所帮助。

<form action="" method="post">
    <div class="col-md-6">
        <input name="name" type="text" class="form-control" placeholder="Name">
    </div>
    <div class="col-md-6"><!-- // incorrect quotation used on `text` //-->
        <input name="email" type='text' class="form-control" placeholder="Email">
    </div>
    <div class="col-md-12">
       <input name="subject" type="text" class="form-control" placeholder="Subject">
    </div>
    <div class="col-md-12">
        <textarea name="message" class="form-control" placeholder="Message" rows="4"></textarea>
    </div>
    <div class="col-md-8">
        <input type="submit" class="form-control text-uppercase" name="send" value="submit" />
    </div>
</form>

<?php

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['subject'],$_POST['email'],$_POST['name'],$_POST['message'] ) ){

        $to         = 'info@autonomousdata.com';
        $name       = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING );
        $subject    = filter_input( INPUT_POST, 'subject', FILTER_SANITIZE_STRING );
        $message    = filter_input( INPUT_POST, 'message', FILTER_SANITIZE_STRING );
        $email      = filter_var( filter_input( INPUT_POST, 'email', FILTER_SANITIZE_STRING ), FILTER_VALIDATE_EMAIL );

        $headers=array();
        $headers[]="From: {$email}";
        $headers[]="Reply-To: {$email}";
        $headers[]="Content-type: text/html; charset=iso-8859-1";
        $headers[]="X-Mailer: PHP/" . phpversion();
        /* generate final headers string */
        $headers=implode( "\r\n", $headers );


        $message=array();
        $message[]="Name: {$name}";
        $message[]="Email: {$email}";
        $message[]="Comment: {$message}";
        /* generate final message string */
        $message=implode( "\n", $message );


        $result=@mail( $to, $subject, $message, $headers );

        switch( $result ){
            case true:
                $msg='Thank you for contacting us! All information received will always remain confidential. We will contact you as soon as we review your message.';
            break;
            case false:
                $msg='failed to send the mail';
            break;
        }

        echo "<script type='text/javascript'>alert('{$msg}');</script>";
    }
?>

整个测试页面,我设置它来测试脚本的基本功能。由于这一切都是在本地运行的,我无法确认是否会发送电子邮件 - 但它看起来是正确的。

<?php

?>
<!doctype html>
<html>
    <head>
        <title>Basic Contact Form - investigating email send failure.</title>
        <script type='text/javascript' charset='utf-8'></script>
        <style type='text/css' charset='utf-8'>
            form{
                width:50%;
            }
            form,output,input[type='text'],textarea,input[type='button'],input[type='submit']{
                display:block;
                box-sizing:border-box;
                padding:0.5rem;
                margin:0.5rem;
            }
            output{
                color:red;
            }
            input[type='text'],textarea{
                width:60%;
            }
        </style>
    </head>
    <body>


        <form action="" method="post">
            <div class="col-md-6">
                <input name="name" type="text" class="form-control" placeholder="Name">
            </div>
            <div class="col-md-6"><!-- // incorrect quotation used on `text` //-->
                <input name="email" type='text' class="form-control" placeholder="Email">
            </div>
            <div class="col-md-12">
               <input name="subject" type="text" class="form-control" placeholder="Subject">
            </div>
            <div class="col-md-12">
                <textarea name="message" class="form-control" placeholder="Message" rows="4"></textarea>
            </div>
            <div class="col-md-8">
                <input type="submit" class="form-control text-uppercase" name="send" value="submit" />
            </div>
            <output id='msgs'></output>
        </form>


        <?php
            if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['subject'],$_POST['email'],$_POST['name'],$_POST['message'] ) ){

                $to         = 'info@autonomousdata.com';
                $name       = filter_input( INPUT_POST, 'name', FILTER_SANITIZE_STRING );
                $subject    = filter_input( INPUT_POST, 'subject', FILTER_SANITIZE_STRING );
                $message    = filter_input( INPUT_POST, 'message', FILTER_SANITIZE_STRING );
                $email      = filter_var( filter_input( INPUT_POST, 'email', FILTER_SANITIZE_STRING ), FILTER_VALIDATE_EMAIL );

                if( !$to or !$name or !$subject or !$message or !$email ){

                    echo "Error: one or more required variables are not available.";

                } else {

                    $headers=array();
                    $headers[]="From: {$email}";
                    $headers[]="Reply-To: {$email}";
                    $headers[]="Content-type: text/html; charset=iso-8859-1";
                    $headers[]="X-Mailer: PHP/" . phpversion();
                    /* generate final headers string */
                    $headers=implode( "\r\n", $headers );


                    $message=array();
                    $message[]="Name: {$name}";
                    $message[]="Email: {$email}";
                    $message[]="Comment: {$message}";
                    /* generate final message string */
                    $message=implode( "\n", $message );


                    $result=@mail( $to, $subject, $message, $headers );


                    switch( $result ){
                        case true:
                            $msg='Thank you for contacting us! All information received will always remain confidential. We will contact you as soon as we review your message.';
                        break;
                        case false:
                            $msg='Failed to send the mail';
                        break;
                    }



                    /* Let the user know the status of the form submission somehow */
                    echo "
                        <script type='text/javascript'>
                            document.getElementById('msgs').innerHTML='{$msg}';
                            /*
                                alert('{$msg}');
                            */
                        </script>";
                }
            }
        ?>
    </body>
</html>

【讨论】:

  • 哇,很好的例子。可悲的是没有工作。最后一行 echo "";似乎给出了一些语法错误。删除该行进行修复,但 info@autonomousdata.com 仍未收到该消息。真的不明白哪里错了。
  • 我刚刚打开了一个测试页面,但没有发现任何错误 - 无法确认是否会像仅在本地主机上测试那样发送电子邮件...
  • 我可以发送屏幕截图。可以添加吗?使用 echo 行,浏览器会在联系表单附近显示这个刺痛 - alert('{$msg}');"; } ?>。这意味着一些错误。但是,当我删除该行时,该字符串不存在还有。可能我在某个地方还有其他错误吗?
猜你喜欢
  • 1970-01-01
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
相关资源
最近更新 更多