PHP / HTML联系人表格-空白字段

时间:2019-04-14 10:48:33

标签: php html contact-form

我已经使用PHP和HTML制作了一个自定义联系表单,但是在发送电子邮件时出现了一些空白字段。

实际上,我已经制作了一个包含4个表,包括“名称”,“电子邮件”,“主题”和“消息”,但“主题和消息”等字段却被发送为空。

我将不胜感激。

谢谢。

HTML代码:

<form action="mail.php" method="post">
    <div class="form-block clearfix">
        <input type="text" placeholder="name*" id="name" />
        <input type="text" placeholder="email*" id="email" />
    </div>
    <div class="form-block clearfix">
        <input type="text" placeholder="subject*" id="sub" />
    </div>
    <div class="form-block">
        <textarea cols="1" rows="1" placeholder="Message*" id="message" ></textarea>
    </div>
    <div class="submit-btn">
        <input type="button" id="submit" value="submit" class="detail-submit"/>
    </div>
</form>

PHP:

<?php

$to = "My email";
$from = "";
$cc = "";

$subject = "Contact us form";

$errmasg = "";

$name = htmlentities(trim($_POST['name']));
$email = htmlentities(trim($_POST['email']));
$sub = htmlentities(trim($_POST['sub']));
$message = htmlentities(trim(nl2br($_POST['message'])));

if($email){
    $message = "<table border='0' cellpadding='2' cellspacing='2' width='600'>
    <tr><td>Name: ".$name." </td></tr>
    <tr><td>Email: ".$email."</td></tr>
    <tr><td>Subject: ".$sub."</td></tr>
    <tr><td>Message:".$message."</td></tr>
    </table>";
}else{
    $errmasg = "No Data";
}

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";


$headers .= 'From:'.$from . "\r\n";
$headers .= 'Cc:'.$cc . "\r\n";

if($errmasg == ""){
    if(mail($to,$subject,$message,$headers)){
         echo 1;
    }else{
        echo 'Error occurred while sending email';
    }
}else{
    echo $errmasg;
}
?>

2 个答案:

答案 0 :(得分:1)

您需要在NAMEsubject字段中添加元素message

只需将表单代码替换为以下代码:

<form action="mail.php" method="post">
    <div class="form-block clearfix">
        <input type="text" placeholder="name*" id="name" />
        <input type="text" placeholder="email*" id="email" />
    </div>
    <div class="form-block clearfix">
        <input type="text" name="sub" placeholder="subject*" id="sub" />
    </div>
    <div class="form-block">
        <textarea cols="1" rows="1" name="message" placeholder="Message*" id="message" ></textarea>
    </div>
    <div class="submit-btn">
        <input type="button" id="submit" value="submit" class="detail-submit"/>
    </div>
</form>

答案 1 :(得分:1)

提交表单时希望在POST数组数据中显示的每个表单元素(因此可以使用$ _POST ['fieldname']来使用)都需要一个name属性。 ID属性是可选的,但在许多情况下用途有限-在这种传统表单提交中当然不需要。

输入按钮submit不会提交表单,除非您使用Javascript提交。最好使用以下提交按钮。

<form action="mail.php" method="post">
    <div class="form-block clearfix">
        <input type="text" placeholder="name*" name="name" />
        <input type="text" placeholder="email*" name="email" />
    </div>
    <div class="form-block clearfix">
        <input type="text" placeholder="subject*" name="sub" />
    </div>
    <div class="form-block">
        <textarea cols="100" rows="1" placeholder="Message*" name="message" ></textarea>
    </div>
    <div class="submit-btn">
        <input type="submit" name="submit" value="Submit" class="detail-submit"/>
    </div>
</form>

不确定您为什么会遇到问题〜也许以下内容会对您有所启发。经过测试,它无法发送电子邮件(目前在开发机上没有本地邮件服务器),并且是一个“多页显示”演示,其中PHP模拟了原始表单操作mail.php

<?php

    /* this emulates mail.php */
    error_reporting( E_ALL );

    /* use a session variable */
    session_start();

    /* for testing single page demo */
    $singlepage=true;

    if( $_SERVER['REQUEST_METHOD']=='POST' ){

        $to = "My email";
        $from = $cc = '';

        $subject = "Contact us form";
        $errmasg = '';

        /* filter POST data */
        $args=array(
            'name'      =>  FILTER_SANITIZE_STRING,
            'email'     =>  FILTER_SANITIZE_EMAIL,
            'sub'       =>  FILTER_SANITIZE_STRING,
            'message'   =>  FILTER_SANITIZE_STRING
        );
        $_POST=filter_input_array( INPUT_POST, $args );
        /* assign as variables */
        extract( $_POST );


        $name = htmlentities(trim($name));
        $email = htmlentities(trim($email));
        $sub = htmlentities(trim($sub));
        $message = htmlentities(trim(nl2br($message)));


        if( $email ){
            $message = "<table border='0' cellpadding='2' cellspacing='2' width='600'>
                <tr><td>Name: ".$name." </td></tr>
                <tr><td>Email: ".$email."</td></tr>
                <tr><td>Subject: ".$sub."</td></tr>
                <tr><td>Message:".$message."</td></tr>
            </table>";
        }

        # REMOVE THIS LINE or COMMENT IT OUT FOR REAL USAGE
        #exit( sprintf("<pre>%s\n%s</pre>",$message, print_r( $_POST,true ) ) );

        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= 'From:'.$from . "\r\n";
        $headers .= 'Cc:'.$cc . "\r\n";

        if($errmasg == ""){
            if( mail( $to, $subject, $message, $headers ) ){
                 $_SESSION['mailsent']=1;
            }else{
                $_SESSION['mailsent']=2;
            }
        }else{
            $_SESSION['mailsent']=3;
        }

        /* 
            If you are using mail.php then use a `header` to redirect the user 
            back to the contact page - assumed to be called `contact.php`
        */
        if( !$singlepage ) header( 'Location: contact.php' );

    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>POST to email</title>
    </head>
    <body>
        <!-- removed attribute action as this works on same page here -->
        <form method="post">
            <?php

                if( !empty( $_SESSION['mailsent'] ) ){
                    switch( $_SESSION['mailsent'] ){
                        case 1:$message='Your message was sent successfully';break;
                        case 2:$message='Sorry - we had a problem sending your email';break;
                        case 3:$message='Bogus - no data';break;
                    }
                    printf( '<h1>%s</h1>', $message );
                    unset( $_SESSION['mailsent'] );
                }

            ?>
            <div class="form-block clearfix">
                <input type="text" placeholder="name*" name="name" /><!-- element has a NAME -->
                <input type="text" placeholder="email*" name="email" />
            </div>
            <div class="form-block clearfix">
                    <input type="text" placeholder="subject*" name="sub" />
            </div>
            <div class="form-block">
                    <textarea cols="100" rows="1" placeholder="Message*" name="message" ></textarea>
            </div>
            <div class="submit-btn">
                <input type="submit" class="detail-submit" /><!-- a SUBMIT button -->
            </div>
        </form>
    </body>
</html>

用于调试的典型输出

Array
(
    [name] => fred flintstone
    [email] => fred@bedrock.com
    [sub] => betty had better bake a cake
    [message] => hey betty
)