PHP表单发布空白值

时间:2014-12-28 05:38:20

标签: php html forms

我正在尝试在我的index.html文件中创建一个简单的提交表单,该表单将发送包含值的电子邮件,但在使用$ _POST获取它们后它们总是空白。这是我的表格:

<form id="contact-form" action="/rtp/php/submit.php" method="POST" enctype="text/plain">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="name">
                            Name</label>
                        <input type="text" name="theName" class="form-control" id="theName" placeholder="Enter name" required="required" />
                    </div>
                    <div class="form-group">
                        <label for="email">
                            Email Address</label>
                        <div class="input-group">
                            <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
                            </span>
                            <input type="email" name="theEmail" class="form-control" id="theEmail" placeholder="Enter email" required="required" /></div>
                    </div>
                    <div class="form-group">
                        <label for="subject">
                            Subject</label>
                        <select id="subject" name="theSubject" id="theSubject" class="form-control" required="required">
                            <option value="na" selected="">Choose One:</option>
                            <option value="service">Criminal Defense</option>
                            <option value="service">Personal Injury</option>
                            <option value="service">Consitutional Law</option>
                            <option value="service">Immigration</option>
                            <option value="suggestion">General Inquiry</option>
                            <option value="suggestion">Scholarship</option>

                        </select>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label for="name">
                            Message</label>
                        <textarea name="message" name="theMessage" id="theMessage" class="form-control" rows="9" cols="25" required="required"
                            placeholder="What can we help you with?"></textarea>
                    </div>
                </div>
                <div class="col-md-12">
                    <button type="submit" class="btn btn-skin pull-right" id="btnContactUs">
                        Send Message</button>
                </div>
            </div>
            </form>

这是我的提交脚本:

 <?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

// Set destination
$email_to = "bali*****l.com";
$email_from = "br******offee";
$email_subject = "Rai*******rship";

// Grab fields
$name = isset($_POST["theName"]) ? $_POST['theName'] : 'Name Not set';
$email = isset($_POST['theEmail']) ? $_POST['theEmail'] : 'Email not set';
$subject = isset($_POST['theSubject']) ? $_POST['theSubject'] : 'subject not set';
$message = isset($_POST['theMessage']) ? $_POST['theMessage'] : 'message not set:(';

$email_message = "Form details below.\n\n";


// Clean up
function clean_string($string) {

$bad = array("content-type","bcc:","to:","cc:","href");

return str_replace($bad,"",$string);

}

// Set string
$email_message .= $name."\n";
$email_message .= $email."\n";
$email_message .= $subject."\n";
$email_message .= $message."\n";


$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  
    print("The message -> $email_message");

?> 

Congratulations, you are entered to win! You should hear from us shortly :)

所有变量都以空白结束。我究竟做错了什么?任何帮助表示赞赏,我一直试图解决这个问题几个小时。我不明白为什么POST函数没有抓取值并传递它们。

2 个答案:

答案 0 :(得分:2)

从表单标记中删除 enctype =&#34; text / plain&#34; 并检查。

答案 1 :(得分:1)

我同意您可以完全删除enctype标记 - 我从来不需要将其包含在通过Post发送的表单上。

另外,除了:

如果网页表格留空,您的意思是变量是空白的吗?

当通过POST提交表单并且字段为空时,它将作为空字符串发送。

因此,例如,如果您将整个表单留空,那么$ _POST ['theName']将作为“”发送。

ISSET将在空字符串上返回true - 因此,所有ISSET测试都返回true。

尝试!空了。例如:

$name = !empty($_POST['theName']) ? $_POST['theName'] : 'No Name Set';

如果即使在表格上输入了某些内容,它也会返回空格,请澄清。

此外,在<select><option>标签中,POST将发送所选内容的值。您目前有多个具有相同值的选项。它不发送<option&gt;之间的内容。标签(换句话说,他们是选择“刑事辩护”还是“人身伤害”$ _POST ['theSubject']将为两者“服务”。