[英]On select one option don't send text box value
我的網站上有一個表格,其中添加了選擇下拉列表。 我選擇選項,如果我們單擊最后一個選項,將打開一個文本框。 如果我們選擇其他選項,則此文本框將被隱藏。 在發送郵件時,如果用戶單擊其他選項(最后一個選項除外),則郵件正在發送,但也采用該自定義文本框值。 僅當我單擊最后一個選項時,此值才應發送。 否則它將隱藏。 怎么做,請幫幫我。 這是我的代碼
<form action="inc/contact.php" method="POST" class="theme-form-one form-validation" autocomplete="off">
<input type="text" placeholder="Name *" name="name">
<input type="text" placeholder="Phone *" name="phone">
<input type="email" placeholder="Email *" name="email">
<select id="contact-services" name="services" onchange='CheckServices(this.value);'>
<option value=""> Services Interested</option>
<option value="3D Modeling">3D Modeling</option>
<option value="3D Rendering">3D Rendering</option>
<option value="3D Animation">3D Animation</option>
<option value="Custom">Custom Requirement</option>
</select>
<input type="text" name="service" id="service" style='display:none;'/>
<textarea placeholder="Message" name="message"></textarea>
<button class="theme-button-four btn3">SEND MESSAGE</button>
</form>
這是我的javascript
<script type="text/javascript">
function CheckServices(val){
var element=document.getElementById('service');
if(val=='Services Interested'||val=='Custom')
element.style.display='block';
else
element.style.display='none';
}
</script>
這是我發送郵件的php代碼
<?php
$to = "abc@mail.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$headers = "From: $from";
$subject = "subject";
$fields = array();
$fields{"name"} = "Name";
$fields{"email"} = "Email";
$fields{"phone"} = "Phone";
$fields{"services"} = "Services";
$fields{"service"} = "Service";
$fields{"message"} = "Message";
$body = "Message:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$send = mail($to, $subject, $body, $headers);
?>
$fields = array();
$fields{"name"} = "Name";
$fields{"email"} = "Email";
$fields{"phone"} = "Phone";
$fields{"services"} = "Services";
if(isset($_POST["service"]) && !empty($_POST["service"])) $fields{"service"} = "Service";
$fields{"message"} = "Message";
這可能對您有幫助。 代碼的問題是即使service
為空,您也要推送數組。 這就是為什么它包含在您的郵件正文中。 因此,您只需要放置一個if
條件,以確保service
的值不為空
您要做的是檢查services
文本框中是否包含特定值。如果包含,則我們可以包含它,也可以不包含它。 我還整理了一下您的代碼。
嘗試這個:
$to = "abc@mail.com";
$from = $_POST['email'];
$name = $_POST['name'];
$headers = "From: $from";
$subject = "subject";
if(isset($_POST['services']) && $_POST['services'] != 'Custom'){
unset($_POST['service']);
}
$body = "Message:\n\n";
foreach($_POST as $a => $b){
$body .= sprintf("%20s: %s\n", ucfirst($a), $_POST[$a]);
}
$send = mail($to, $subject, $body, $headers);
<script type="text/javascript">
function CheckServices(val){
var element=document.getElementById('service');
if(val=='Services Interested'||val=='Custom')
element.style.display='block';
else
element.style.display='none';
element.value = ''; // reset element value
}
</script>
Check element on php code
if(!empty($_POST['service']) {
// post data in mmail
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.