我试图在我的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 函数不获取值并传递它们。
最佳答案
从表单标记中删除 enctype="text/plain" 并进行检查。
关于PHP 表单发布空白值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27673947/