On select one option don't send text box value

html select option value null
html select empty option value
option select
select option css
html select default
html <select value
html select remove blank option
select placeholder

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

How to set the default value for an HTML <select> element , If you don't want to write CSS and expect the same behaviour of the <option hidden disabled selected value> -- select an option -- </option>. ‘Options Trading For Newbies’ is written for beginners with small accounts. Learn how to trade options in the simplest way possible by downloading this free report.

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);

how to change a selections options based on another select option , Remove Select Features When There's Only One Option Left (14% Don't). Presenting users with drop-down, radio button, or other selectors,  Remove Select Features When There’s Only One Option Left (14% Don’t) Remove Select Features When There’s Only One Option Left. (14% Don’t) Presenting users with drop-down, radio button, or other selectors, when there’s no selection to be made, leads to unnecessary clicks and user frustration. Our large-scale Checkout usability testing reveals that 14% of e-commerce sites, at some point in the checkout process, present users with what appears to be a selectable option, only to

<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
  }

How to style the option of an html "select" element?, You can include a selected attribute on an <option> element to make it However, these properties don't produce a consistent result across  Option elements don't have event "onchange", but Select elements do. I've quickly wrote a code snippet below. You may add more select items. When you choose an option in one of those select elements, you shouldn't choose options at same index in other select elements.

default select option as blank, If you don't know what an element is or how you must use it, I recommend you read The options in a select list are provided with the option element and can be To make an option default, we just need to declare the selected attribute for it. ((not X, X), (not L, not L)) Question 14 Correct 1.00 points out of 1.00 Flag question Question text Two Firms A and B are playing the following game Identify the SPE Select the correct option. Select one: a. ((Don’t Fight, Fight, Fight), Accommodates) b. ((Don’t Enter, Exit, Fight), Fight) c. ((Enter, Exit, Fight), Accommodates) d.

Remove Select Features When There's Only One Option Left (14 , Convenient Form Entry. The selected attribute allows you to set one of your <​option> lines as the default. This is a good technique to  The selected attribute allows you to set one of your <option> lines as the default. This is a good technique to speed up data entry if the majority of visitors are likely to select the same option. This is a good technique to speed up data entry if the majority of visitors are likely to select the same option.

<select>, If your first option were non-empty, the browser would display this instead of the placeholder. Multi-select placeholders. For multi-selects, you must not have an  First, set multiple="multiple" as a property of the select box. Second, put [ ] at the end of the name. Finally, we don't really need a "blank" option in this select box, because we can simply check to make sure the user selected something or not. To select multiple values, use the shift or ctrl buttons when clicking.

Comments
  • Possible duplicate of javascript show hidden div when select option is selected
  • Here I am asking how to send mail with and without custom text box value.
  • thank you so much @Zainul Abideen. This solution worked for me