[英]php email form. Only want to post filled fields and not post empty ones
我有一個接觸的形式,看起來像這樣
根據用戶在“我想”選擇框中選擇的內容,將出現不同的表單字段。
我想實施在此站點上找到的解決方案 。 基本上,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>
提前致謝。
嘗試這個:
$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數組中的變量分配給您提供的代碼段的代碼。 基本上,這和您先前的代碼所做的相同。
您的foreach
-loop仍在按原樣工作,但是使用empty()
時,代碼看起來更簡潔一些(但是您已經做過的工作!):
foreach ($_POST as $field=>$value) {
if (!empty($value)) {
$body .= "$field: $value\n\n";
}
}
這會將全局$_POST
-array中不為空的所有變量分配到$body
-variable中。 使用此方法,您不需要其他$_POST
語句(例如$message = $_POST['message'];
)。 唯一的問題是,將打印$_POST
-array中的所有內容(甚至是commit-buttons值!)。
然后你只需把在你的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
-loop,因為它更少而且更簡潔的代碼也可以完成相同的工作。 如果它也打印“提交”值,則只需添加一段代碼即可跳過全局$_POST
數組的該元素。
您應該具有防止腳本每次有人直接訪問該頁面時運行,就像有一個聲明if ($_POST['submit'] == 'submit')
其中submit
的名稱和您呈交的價值HTML表單中的“按鈕”)包裝所有與郵件相關的代碼。 這樣,僅在提交表單后才發送郵件。
您應該創建一個包含接受密鑰的白名單數組:
$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
通過大寫第一個字符使鍵在電子郵件中看起來可顯示,並且帶有ENT_QUOTES
標志的htmlspecialchars
確保為電子郵件正確轉換了字符。
您應該始終為此循環類型創建白名單,這不僅出於安全目的,還因為您將遍歷不必要或不相關的鍵,例如:
<button type="submit" name="submit" value="1">Submit</button>
然后,您的電子郵件將顯示:
Submit: 1
這對接收者毫無意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.