I have a form in my website, in that i have added select dropdown. I select option, if we click on last option, one text box will open. If we select other options, this text box will be hidden. While sending mail, if user clicks on other option(except last option) mail is sending, but it is also taking that custom text box value. This value should send only if I click on last option. Otherwise it should hide. How to do this, please help me. Here is my code
<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>
here is my 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>
Here is my php code for sending mail
<?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";
This may help you. The problem with your code is you are pushing array even if you service
is empty. so that's why it is included in your mail body. so, you just have to put an if
condition to ensure that the value of service
is not empty
What you want to do is check if the services
textbox has a certain value contained it in. If it does then we can include it or not include it. I also cleaned up your code a bit.
Try this:
$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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.