简体   繁体   English

选择一个选项时不发送文本框值

[英]On select one option don't send text box value

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 这是我的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代码

<?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. 代码的问题是即使service为空,您也要推送数组。 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 因此,您只需要放置一个if条件,以确保service的值不为空

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. 您要做的是检查services文本框中是否包含特定值。如果包含,则我们可以包含它,也可以不包含它。 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
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM