我的联系表单看起来像this
根据用户在&#34上选择的内容,系统会显示不同的表单字段;我希望<#34; 选择框。
我想实现我在此网站上找到的solution。基本上,php只发布填写的字段,不发布空字段。它使用以下代码来实现它
foreach ($_POST as $Field=>$Value) {
if($Value != ''){
$body .= "$Field: $Value\n";
}
}
我不熟悉PHP并且无法应用所提出的解决方案。如果有人可以帮助我的代码实现解决方案,使其发布非空表单元素,包括文本字段,选择框和广播组,将不胜感激。
我的PHP表单处理器的代码如下所示
<?php
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$p_prefered_day = $_POST['p_prefered_day']; (radio button)
$session_time = $_POST['session_time']; (select box)
$p_message = $_POST['p_message'];
$treatment = $_POST['treatment']; (select box)
$therapist = $_POST['therapist']; (select box)
$b_pref_day = $_POST['b_pref_day']; (radio button)
$b_pref_time = $_POST['b_pref_time']; (radio button)
$b_message = $_POST['b_message'];
$to = 'info@zenwebcreative.com.au';
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nCompany: $company\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
我的表单的HTML代码如下所示
<form name="sentMessage" id="contactForm" novalidate>
<div class="form-group">
<label>Full Name *</label>
<input type="text" class="form-control" id="name" required>
<p class="help-block text-danger"></p>
</div><!-- end form-group -->
<div class="form-group">
<label>Email Address*</label>
<input type="email" class="form-control" id="email" required>
<p class="help-block text-danger"></p>
</div><!-- end form-group -->
<div class="form-group">
<label>Contact Number</label>
<input type="text" class="form-control" id="phone">
</div> <!-- end form group -->
<div class="form-group">
<label>I would like to *</label>
<select id="select_question" class="form-control">
<option selected="selected">Please select</option>
<option value="question">Ask a question</option>
<option value="pilates">Attend a pilates class</option>
<option value="booking">Schedule a booking</option>
</select>
</div><!-- end form-group -->
<div class="form-group box question">
<label>Message</label>
<div class="input-group">
<textarea id="message" class="form-control" rows="7" required></textarea>
<span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
</div><!-- end input group -->
<p class="help-block text-danger"></p>
</div><!-- end form-group -->
<div class="box pilates">
<div class="form-group">
<label>Prefered day of the week *</label><br>
<label class="radio-inline">
<input type="radio" name="p_prefered_day" id="Tue" value="Tuesday">Tuesday
</label>
<label class="radio-inline">
<input type="radio" name="p_prefered_day" id="Thu" value="Thursday">Thursday
</label>
</div> <!-- end form group -->
<div class="form-group">
<label>Prefered Session you would like to attend? *</label>
<select class="form-control" id="session_time">
<option value="4:30 pm">4:30 pm</option>
<option value="5:30 pm">5:30 pm</option>
<option value="6:30 pm">6:30 pm</option>
</select>
</div><!-- end form-group -->
<div class="form-group">
<label>Additional Comments</label>
<div class="input-group">
<textarea id="p_message" class="form-control" rows="7" required></textarea>
<span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
</div><!-- end input group -->
<p class="help-block text-danger"></p>
</div><!-- end form-group -->
</div><!-- end box pilates -->
<div class="box booking">
<div class="form-group">
<label>What Service Are You Interested In? *</label>
<select class="form-control" id="services">
<option value="Manual Therapy">Manual Therapy</option>
<option value="Sports Physiotherapy">Sports Physiotherapy</option>
<option value="Dry Needling">Dry Needling</option>
<option value="Post and Pre operative care">Post and Pre operative care</option>
<option value="Real Time Ultrasound Imaging">Real Time Ultrasound Imaging</option>
<option value="Balance and Vestibular treatment">Balance and Vestibular treatment</option>
<option value="Sports Specific Rehabilitation">Sports Specific Rehabilitation</option>
<option value="Motor Vehicle Accident">Motor Vehicle Accident</option>
<option value="Workers Compensation">Workers Compensation</option>
</select>
</div><!-- end form-group -->
<div class="form-group">
<label>Which therapist would you like to see*</label>
<select class="form-control" id="therapist">
<option selected="selected">No Preference</option>
<option value="Kristen">Kristen</option>
<option value="Tara">Tara</option>
</select>
</div><!-- end form-group -->
<div class="form-group">
<label>Prefered day of the week *</label><br>
<label class="radio-inline">
<input type="radio" name="b_pref_day" id="b_Mon" value="Mon-">Mon
</label>
<label class="radio-inline">
<input type="radio" name="b_pref_day" id="b_Tue" value="Tue">Tue
</label>
<label class="radio-inline">
<input type="radio" name="b_pref_day" id="b_Wed" value="Wed">Wed
</label>
<label class="radio-inline">
<input type="radio" name="b_pref_day" id="b_Thu" value="Thu">Thu
</label>
<label class="radio-inline">
<input type="radio" name="b_pref_day" id="b_Fri" value="Fri">Fri
</label>
</div> <!-- end form group -->
<div class="form-group">
<label>Prefered time of the day</label><br>
<label class="radio-inline">
<input type="radio" name="b_pref_time" id="morning" value="Morning">Morning
</label>
<label class="radio-inline">
<input type="radio" name="b_pref_time" id="afternoon" value="Afternoon">Afternoon
</label>
</div> <!-- end form group -->
<div class="form-group">
<label>Addtional Comments</label>
<div class="input-group">
<textarea id="b_message" class="form-control" rows="7" required></textarea>
<span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
</div><!-- end input group -->
<p class="help-block text-danger"></p>
</div><!-- end form-group -->
</div> <!-- end box booking -->
<div class="clearfix"></div>
<div id="success"></div>
<button id="send_btn" type="submit" class="btn cpcbutton">Send Message</button>
<p class="help-block"> <span class="glyphicon glyphicon-asterisk green"></span> Required Fields </p>
</form>
提前致谢。
答案 0 :(得分:1)
试试这个:
$body = "";
foreach ($_POST as $Field=>$Value) {
if($Value != ''){
$body .= "$Field: $Value\n\n";
}
}
$to = 'info@zenwebcreative.com.au';
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details: $body";
$headers = "From: noreply@yourdomain.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
我将所有从$ _POST数组中分配变量的代码替换为您提供的代码段。这基本上和你以前的代码做的一样。
答案 1 :(得分:0)
你的foreach
- 循环按原样工作,但使用empty()
时代码看起来更清晰,就像这样(但是你已经做了同样的工作!):</ p>
foreach ($_POST as $field=>$value) {
if (!empty($value)) {
$body .= "$field: $value\n\n";
}
}
这会将全局$_POST
- 非空数组中的所有变量分配到$body
- 变量中。使用此功能,您不需要其他$_POST
语句(例如$message = $_POST['message'];
)。唯一的问题是打印$_POST
- 数组中的所有内容(即使是提交按钮值!)。
然后你就把它放在mail
- 这样的函数中:
$email_body = "You have received a new message from your website contact form.\n\n";
$email_body .= "Here are the details:\n\n";
$email_body .= "$body";
mail($to, $email_subject, $email_body, $headers);
另一种方法是明确检查每个字段,尽管这会生成更多代码。
if (!empty($_POST['name']))
$body .= "Name: ".$_POST['name']."\n\n";
if (!empty($_POST['email']))
$body .= "Email: ".$_POST['email']."\n\n";
if (!empty($_POST['phone']))
$body .= "Phone: ".$_POST['phone']."\n\n";
此处与您如何将其添加到邮件功能相同。
当然还有其他的做事方式,在这两种做法中,我建议使用foreach
- 循环,因为它的代码要少得多,而且代码更清晰,应该这样做同样的工作。如果它打印&#34;提交&#34;也可以添加一段跳过全局$_POST
数组元素的代码。
您应该有一个语句,阻止每次有人直接访问此页面时运行脚本,例如if ($_POST['submit'] == 'submit')
(其中submit
是您的提交按钮的名称和值HTML表单)声明包装所有与邮件相关的代码。这样,邮件只会在提交表单后发送。
答案 2 :(得分:0)
您应该创建已接受密钥的白名单数组:
$whitelist = array('name', 'email', 'phone');
然后循环遍历数组:
foreach($whitelist as $key){
if(isset($_POST[$key]) && $_POST[$key] != ''){
$body .= ucfirst($key) . ": " . htmlspecialchars($_POST[$key], ENT_QUOTES) . "\n";
}
}
foreach
循环遍历数组,isset
检查字段是否已设置,ucfirst
通过大写第一个字符使电子邮件中的关键字显示,htmlspecialchars
使用ENT_QUOTES
标志可确保为电子邮件正确转换字符。
您应始终为此类型的循环创建白名单,不仅是出于安全目的,还因为您将循环遍历不必要或不相关的键,例如:
<button type="submit" name="submit" value="1">Submit</button>
您的电子邮件将会显示:
Submit: 1
这对接收者毫无意义。