PHP-HTML表单单选按钮值始终为空
我已经设计了一个联系表单,我使用它发送提交的数据到我的电子邮件地址使用PHP邮件。所有其他输入,如“text”、“textarea”、“select”都可以正常工作。但是我在表单中使用的单选按钮总是返回空值。我使用POST方法 这是我的HTML表单代码PHP-HTML表单单选按钮值始终为空,php,html,forms,phpmailer,Php,Html,Forms,Phpmailer,我已经设计了一个联系表单,我使用它发送提交的数据到我的电子邮件地址使用PHP邮件。所有其他输入,如“text”、“textarea”、“select”都可以正常工作。但是我在表单中使用的单选按钮总是返回空值。我使用POST方法 这是我的HTML表单代码 <form action="php/get-a-quote.php" id="contactForm" type="post" > <div class="row"> <div class="form-group"&
<form action="php/get-a-quote.php" id="contactForm" type="post" >
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control" name="name" id="name">
</div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email" id="email">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Company Name</label>
<input type="text" value="" data-msg-required="Please enter your company name." maxlength="100" class="form-control" name="company_name" id="company_name">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Project Description *</label>
<textarea maxlength="5000" data-msg-required="Please enter your project description." rows="10" class="form-control" name="project_description" id="project_description"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Type of website</label>
<select data-msg-required="Please enter the type of website." class="form-control" name="type_of_website" id="type_of_website">
<option value=""></option>
<option value="Sales">Sales</option>
<option value="Portfolio">Portfolio</option>
<option value="Blog">Blog</option>
<option value="Other">Other</option>
</select>
</div>
<div class="col-md-6">
<label>Expected time-frame for completion</label>
<select data-msg-required="Please enter the expected time-frame for completion of your project." class="form-control" name="time_frame" id="time_frame">
<option value=""></option>
<option value="1 week">1 week</option>
<option value="2 weeks">2 weeks</option>
<option value="3 weeks">3 weeks</option>
<option value="more">more</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Need hosting and domain setup?</label>
<label class="radio-inline">
<input type="radio" name="domain_radios" id="inlineRadio1" value="yes" > Yes
</label>
<label class="radio-inline">
<input type="radio" name="domain_radios" id="inlineRadio2" value="no"> No
</label>
</div>
<div class="col-md-6">
<label>Need social pages(fb, twitter, etc)?</label>
<label class="radio-inline">
<input type="radio" name="social_radios" id="inlineRadio3" value="yes" > Yes
</label>
<label class="radio-inline">
<input type="radio" name="social_radios" id="inlineRadio4" value="no" > No
</label>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<input type="submit" name="submit" value="Send Your Request" class="btn btn-primary btn-lg" data-loading-text="Loading...">
</div>
</div>
</form>
然而,这似乎不起作用。我到处都试过,浏览了足够多的帖子。但我没办法弄明白
仅供参考,我使用了一个模板来编辑网站,它是基于引导的,如果有帮助的话。我应该发布什么样的信息?你应该确保你的第二个
标签是closed@user3626796这些输入是否在您的表单中?你能用var\u dump($\u post)的结果更新你的帖子吗
确认您期望的数据正在以POST的形式发送。您可以在不应用任何引导/模板js和css的情况下尝试相同的表单吗?只需将原始HTML放在自己的页面中,然后var\u dump($\u POST)代码>发送POST数据后的结果。很可能是HTML错误,因此当浏览器从HTML创建DOM时,单选按钮不在表单中…不在表单中,不随表单数据一起发送…并尝试将不存在的POST值放入数组中,则会导致值为NULL。(这也会导致PHP通知,因此请在继续之前正确设置PHP错误报告。)
<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
// Enter your email address below.
$to = 'myemailgoeshere';
$subject = $_POST['company_name'];
if($to) {
$name = $_POST['name'];
$email = $_POST['email'];
$fields = array(
0 => array(
'text' => 'Name',
'val' => $_POST['name']
),
1 => array(
'text' => 'Email address',
'val' => $_POST['email']
),
2 => array(
'text' => 'Company Name',
'val' => $_POST['company_name']
),
3 => array(
'text' => 'Project Description',
'val' => $_POST['project_description']
),
4 => array(
'text' => 'Type of Website',
'val' => $_POST['type_of_website']
),
5 => array(
'text' => 'Time Frame',
'val' => $_POST['time_frame']
),
6 => array(
'text' => 'Domain Radios',
'val' => $_POST['domain_radios']
),
7 => array(
'text' => 'Social Radios',
'val' => $_POST['social_radios']
)
);
$message = "";
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}
$headers = '';
$headers .= 'From: ' . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
if (mail($to, $subject, $message, $headers)){
$arrResult = array ('response'=>'success');
} else{
$arrResult = array ('response'=>'error');
}
echo json_encode($arrResult);
} else {
$arrResult = array ('response'=>'error');
echo json_encode($arrResult);
}
?>
array(8) {
[0]=> array(2) { ["text"]=> string(4) "Name" ["val"]=> string(8) "UserName" }
[1]=> array(2) { ["text"]=> string(13) "Email address" ["val"]=> string(15) "email@email.com" }
[2]=> array(2) { ["text"]=> string(12) "Company Name" ["val"]=> string(14) "Sample Company" }
[3]=> array(2) { ["text"]=> string(19) "Project Description" ["val"]=> string(22) "Description goes here." }
[4]=> array(2) { ["text"]=> string(15) "Type of Website" ["val"]=> string(5) "Sales" }
[5]=> array(2) { ["text"]=> string(10) "Time Frame" ["val"]=> string(7) "2 weeks" }
[6]=> array(2) { ["text"]=> string(13) "Domain Radios" ["val"]=> NULL }
[7]=> array(2) { ["text"]=> string(13) "Social Radios" ["val"]=> NULL }
}