【问题标题】:Contact form won't show content联系表格不会显示内容
【发布时间】:2016-06-08 21:19:56
【问题描述】:

这实际上是我在这里的第一篇文章。可能是分号问题,但我会试一试,因为我的头疼比埃菲尔铁塔还大。

所以我在我的网站上添加了一个联系表单,但它存在问题,因为它正在发送电子邮件但当我收到它时它是空的。它仅显示 PHP 文件中的文本(名称:、电子邮件:等),但不显示输入表单中的内容。

代码如下所示: HTML

    <div class="contact-form bottom">
                    <h2>Send a message</h2>
                    <form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
                        <div class="form-group">
                            <input type="text" name="name" class="form-control" required="required" placeholder="Name">
                        </div>
                        <div class="form-group">
                            <input type="email" name="email" class="form-control" required="required" placeholder="Email Id">
                        </div>
                        <div class="form-group">
                            <textarea name="message" required class="form-control" rows="8" placeholder="Your text here"></textarea>
                        </div>                        
                        <div class="form-group">
                            <input type="submit" name="submit" class="btn btn-submit" value="Submit">
                        </div>
                    </form>
                </div>

PHP

       <?php

header('Content-type: application/json');

$status = array(

'type'=>'success',

'message'=>'Thank you for contact us. As early as possible  we will contact you '

);


    $name = @trim(stripslashes($_POST['name'])); 

    $email = @trim(stripslashes($_POST['email']));  

    $message = @trim(stripslashes($_POST['message'])); 


    $email_from = $email;

    $email_to = 'a.truta@icloud.com';//replace with your email


    $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Message: ' . $message;


    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');


    echo json_encode($status);

    die;

请帮忙,我在这里要疯了!

【问题讨论】:

  • 你只是假设成功。您从不费心实际检查mail() 返回值,因此即使它爆炸并吐得满地都是,您仍然会发出“成功”。你有没有做过任何基本的调试,比如var_dump($_POST) 看看 PHP 收到了什么?
  • 在调试脚本时不要使用@。您想查看所有警告,因为它们可能表明存在问题。
  • 我知道你在说什么,我不想听起来很愚蠢,但我是这个领域的新手,我的首要任务是前端。这就是为什么我在使用简单的联系表格时遇到问题的原因..

标签: php forms contact


【解决方案1】:

正如 Marc 所说,您应该对表单进行一些验证。这是我很快为你准备的东西。它并不完美,我还没有机会对其进行测试,但它会为您的表单处理提供一个更好的起点。不确定json_encode 在您的代码中的相关性。

<?php

$toemail = "you@email.address";

if (isset($_POST)) {
    $errors = [];

    // Validate name
    if ((isset($_POST['name'])) and (preg_match("/^[a-zA-Z ]+$/", $_POST['name']))) {
        $name = $_POST['name'];
    } else {
        $errors[] = "Name is invalid";
    }

    // Validate email
    if ((isset($_POST['email'])) and (filter_var($email, FILTER_VALIDATE_EMAIL))) {
        $email = $_POST['email'];
    } else {
        $errors[] = "The email is invalid";
    }

    // Validate message
    if ((isset($_POST['message'])) and (preg_match("/^[0-9a-zA-Z _-]+$/", $_POST['message']))) {
        $message = $_POST['message'];
    } else {
        $errors[] = "The message is invalid";
    }

    if ((isset($name)) and (isset($email)) and (isset($message)) {
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= 'From: <' . $email . '>' . "\r\n";
        if(@mail($toemail, 'New contact form email', $message, $headers)) {
            echo "<p>Mail Sent Successfully</p>\n";
        } else {
            echo "<p>Mail Not Sent Successfully</p>\n";
        }
    } else {
        echo "<p>Form validation has failed</p>";
        foreach ($errors as $error) {
            echo "<p>" . $error . "</p>\n";
        }
    }
} 

?>

【讨论】:

  • 好的,这是验证,但是我应该把它放在哪里呢?我应该将它添加到我现有的 php "sendemail" 文件中吗?
  • 不要将它添加到现有的PHP中,只需替换它即可。另外,从技术上讲,您不需要单独的 HTML 和 PHP 文件,您可以将 HTML 表单嵌入到主“if”的“else”中,尽管我个人不喜欢这样做。
【解决方案2】:

嗯,显然问题出在 JS 文件上。如果没有此代码,电子邮件将完全按照发送方式到达,包括内容和所有内容。

JS 文件:

    var form = $('#main-contact-form');
form.submit(function(event){
    event.preventDefault();
    var form_status = $('<div class="form_status"></div>');
    $.ajax({
        url: $(this).attr('action'),
        beforeSend: function(){
            form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
        }
    }).done(function(data){
        form_status.html('<p class="text-success">Thank you for contact us. As early as possible  we will contact you</p>').delay(3000).fadeOut();
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 2018-11-06
    • 2012-10-13
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    相关资源
    最近更新 更多