我的网站上有一个表单,其中添加了选择下拉列表。我选择选项,如果我们单击最后一个选项,将打开一个文本框。如果我们选择其他选项,则此文本框将被隐藏。 在发送邮件时,如果用户单击其他选项(最后一个选项除外),则邮件正在发送,但也采用该自定义文本框值。仅当我单击最后一个选项时,此值才应发送。否则它将隐藏。怎么做,请帮帮我。 这是我的代码
<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);
?>
答案 0 :(得分:1)
onSubmit(){
if (this.data.selected === 'embargoed') {
this.data.date = this.date;
}
}
这可能会对您有所帮助。代码的问题是即使$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
的值不为空
答案 1 :(得分:0)
您要执行的操作是检查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);
答案 2 :(得分:0)
<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
}