dongzhi7641 2018-02-07 08:27
浏览 101
已采纳

如何在电子邮件中接收单选按钮按钮和其他值

I use multiple tab forms in HTML and use javascript and PHP to send all values to an email. Please review the code below. I receive this email:

Zipcode: Question1: Question2: Question3:

I think think there is some javascript or PHP errors. I'm using forms in multiple tabs and send value to an email on submit.

<form id="regForm" action="" method="post">
  <div class="main-form">

  <!-- One "tab" for each step in the form: -->
  <div class="tab"><h4>Zip Code</h4>
    <p><input type="text" class="text1" placeholder="Enter zip code..." oninput="this.className = ''" name="zipcode"></p>
    <!--<p><input placeholder="Last name..." oninput="this.className = ''" name="lname"></p> -->
  </div>
 <div class="tab"><h4>What type of home are you looking to buy?</h4>
    <p><input type="radio" name="rdb" value="SingleFamilyHouse"  class="option-input radio" checked/> Single Family House</p>
    <p><input type="radio" name="rdb" value="Condo" class="option-input radio"> Condo</p>
    <p><input type="radio" name="rdb" value="Townhouse" class="option-input radio"> Townhouse</p>
    <p><input type="radio" name="rdb" value="Multiunits" class="option-input radio"> Multi-units </p>

  </div>
  <div class="tab"><h4>How many bedrooms?</h4>
    <p><input type="radio" name="rdb1" value="studio" class="option-input radio"  checked/> Studio</p>
    <p><input type="radio" name="rdb1" value="1-2bedrooms" class="option-input radio"> 1-2 Bedrooms</p>
    <p><input type="radio" name="rdb1" value="3-4bedrooms" class="option-input radio"> 3-4 Bedrooms</p>
    <p><input type="radio" name="rdb1" value="5ormore" class="option-input radio"> 5 Or More </p>

  </div>
  <div class="tab"><h4>When do you plan to buy?</h4>
    <p><input type="radio" name="rdb2" value="1-3months" class="option-input radio" checked/> 1-3 months</p>
    <p><input type="radio" name="rdb2" value="4-6months" class="option-input radio"> 4-6 months</p>
    <p><input type="radio" name="rdb2" value="morethan6months" class="option-input radio"> more than 6 months</p>

  </div>
  <div class="tab"><h4>Do you need to sell before buying?</h4>
    <p><input type="radio" name="rdb3" value="yes" class="option-input radio"  checked/> Yes</p>
    <p><input type="radio" name="rdb3" value="No" class="option-input radio"> No</p>

  </div>
  <div class="tab"><h4>How much house are you looking at?</h4>
    <p><input type="radio" name="rdb4" value="1" class="option-input radio" checked/> $300,00-$500,000</p>
    <p><input type="radio" name="rdb4" value="2" class="option-input radio"> $550,000-$750,000</p>
    <p><input type="radio" name="rdb4" value="3" class="option-input radio"> $800,000-$1,000,000</p>
    <p><input type="radio" name="rdb4" value="4" class="option-input radio"> $1 million up</p>

  </div>
    <div class="tab"><h4>How much down payment did you plan for?</h4>
    <p><input type="radio" name="rdb5" value="lessthan5%" class="option-input radio" checked/> Less than 5%</p>
    <p><input type="radio" name="rdb5" value="10%" class="option-input radio"> 10%</p>
    <p><input type="radio" name="rdb5" value="20%" class="option-input radio"> 20%</p>
    <p><input type="radio" name="rdb5" value="morethan30%" class="option-input radio"> More than 30%</p>
    <p><input type="radio" name="rdb5" value="buyingcash" class="option-input radio"> Buying cash</p>

  </div>

    <div class="tab"><h4>How long have you been in your work or business?</h4>
    <p><input type="radio" name="rdb6" value="lessthan2years" class="option-input radio" checked/> Less than 2 Years</p>
    <p><input type="radio" name="rdb6" value="2years4 years" class="option-input radio"> 2 Years - 4 Years</p>
    <p><input type="radio" name="rdb6" value="5yearsormore" class="option-input radio"> 5 Years or More</p>

  </div>

    <div class="tab"><h4>Give us an idea of the borrower credit score.</h4>
    <p><input type="radio" name="rdb7" value="Needsimprovement" class="option-input radio" checked/> Needs improvement (below 620)</p>
    <p><input type="radio" name="rdb7" value="Fair" class="option-input radio"> Fair (620-670)</p>
    <p><input type="radio" name="rdb7" value="Good" class="option-input radio"> Good (671-710)</p>
    <p><input type="radio" name="rdb7" value="Excellent" class="option-input radio"> Excellent (711-up)</p>

  </div>

    <div class="tab"><h4>How much is the combined annual income of the borrower/s?</h4>
    <p><input type="radio" name="rdb8" value="Less" class="option-input radio" checked/> Less than $30,000</p>
    <p><input type="radio" name="rdb8" value="$31" class="option-input radio"> $31,000-$60,000</p>
    <p><input type="radio" name="rdb8" value="$61" class="option-input radio"> $61,000-$90,000</p>
    <p><input type="radio" name="rdb8" value="$91" class="option-input radio"> $91,000-$120,000</p>
    <p><input type="radio" name="rdb8" value="More" class="option-input radio"> More than $120,000</p>

  </div>

    <div class="tab"><h4>Do you have a home loan pre-approval?</h4>
    <p><input type="radio" name="rdb9" value="yes" class="option-input radio" checked/> Yes</p>
    <p><input type="radio" name="rdb9" value="No" class="option-input radio"> No</p>

  </div> 

  <div class="tab"><h4>You are almost done!</h4>
    <p><input type="text" placeholder="Full name..." oninput="this.className = ''" name="Name"></p>
    <p><input type="email" placeholder="E-mail..." oninput="this.className = ''" name="email"></p>
    <p><input type="text" placeholder="Phone..." oninput="this.className = ''" name="phone"></p>
  </div>


  <div class="prevBtn-btm">
    <div class="btn-bottom">
      <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button>
      <button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button>
    </div>
  </div>
  <!-- Circles which indicates the steps of the form: -->
  <div class="step-btn">
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>

  </div> 
</div>
</form>
var currentTab = 0; // Current tab is set to be the first tab (0)
showTab(currentTab); // Display the crurrent tab

function showTab(n) {
  // This function will display the specified tab of the form...
  var x = document.getElementsByClassName("tab");
  x[n].style.display = "block";
  //... and fix the Previous/Next buttons:
  if (n == 0) {
    document.getElementById("prevBtn").style.display = "none";
  } else {
    document.getElementById("prevBtn").style.display = "inline";
  }
  if (n == (x.length - 1)) {
    document.getElementById("nextBtn").innerHTML = "Submit";
  } else {
    document.getElementById("nextBtn").innerHTML = "Next";
  }
  //... and run a function that will display the correct step indicator:
  fixStepIndicator(n)
}

function nextPrev(n) {
  // This function will figure out which tab to display
  var x = document.getElementsByClassName("tab");
  // Exit the function if any field in the current tab is invalid:
  if (n == 1 && !validateForm()) return false;
  // Hide the current tab:
  x[currentTab].style.display = "none";
  // Increase or decrease the current tab by 1:
  currentTab = currentTab + n;
  // if you have reached the end of the form...
  if (currentTab >= x.length) {
    // ... the form gets submitted:
    document.getElementById("regForm").submit();
    return false;
  }
  // Otherwise, display the correct tab:
  showTab(currentTab);
}

function validateForm() {
  // This function deals with validation of the form fields
  var x, y, i, valid = true;
  x = document.getElementsByClassName("tab");
  y = x[currentTab].getElementsByTagName("input");
  // A loop that checks every input field in the current tab:
  for (i = 0; i < y.length; i++) {
    // If a field is empty...
    if (y[i].value == "") {
      // add an "invalid" class to the field:
      y[i].className += " invalid";
      // and set the current valid status to false
      valid = false;
    }
  }
  // If the valid status is true, mark the step as finished and valid:
  if (valid) {
    document.getElementsByClassName("step")[currentTab].className += " finish";
  }
  return valid; // return the valid status
}

function fixStepIndicator(n) {
  // This function removes the "active" class of all steps...
  var i, x = document.getElementsByClassName("step");
  for (i = 0; i < x.length; i++) {
    x[i].className = x[i].className.replace(" active", "");
  }
  //... and adds the "active" class on the current step:
  x[n].className += " active";
}
</script>
<?php
$EmailFrom = "test@gmail.com";
$EmailTo = "parmdeep87@gmail.com";
$Subject = "Test";

$zipcode = $_POST['zipcode'];
$rdb_value = trim(stripslashes($_POST['rdb']));
$rdb_value1 = trim(stripslashes($_POST['rdb1']));
$rdb_value2 = trim(stripslashes($_POST['rdb2']));
$rdb_value3 = trim(stripslashes($_POST['rdb3']));
$rdb_value4 = trim(stripslashes($_POST['rdb4']));
$rdb_value5 = trim(stripslashes($_POST['rdb5']));
$rdb_value6 = trim(stripslashes($_POST['rdb6']));
$rdb_value7 = trim(stripslashes($_POST['rdb7']));
$rdb_value8 = trim(stripslashes($_POST['rdb8']));
$rdb_value9 = trim(stripslashes($_POST['rdb9']));
$Name = trim(stripslashes($_POST['Name']));
$email = trim(stripslashes($_POST['email']));
$phone = trim(stripslashes($_POST['phone']));


        // prepare email body text
$Body = "";
$Body .= "zipcode: ";
$Body .= $zipcode;
$Body .= "
";
$Body .= "What type of home are you looking to buy?: ";
$Body .= $rdb_value;
$Body .= "
";
$Body .= "How many bedrooms?: ";
$Body .= $rdb_value1;
$Body .= "
";
$Body .= "When do you plan to buy?: ";
$Body .= $rdb_value2;
$Body .= "
";
$Body .= "Do you need to sell before buying?: ";
$Body .= $rdb_value3;
$Body .= "
";
$Body .= "How much house are you looking at?: ";
$Body .= $rdb_value4;
$Body .= "
";
$Body .= "How much down payment did you plan for?: ";
$Body .= $rdb_value5;
$Body .= "
";
$Body .= "How long have you been in your work or business?: ";
$Body .= $rdb_value6;
$Body .= "
";
$Body .= "Give us an idea of the borrower credit score.: ";
$Body .= $rdb_value7;
$Body .= "
";
$Body .= "How much is the combined annual income of the borrower/s?: ";
$Body .= $rdb_value8;
$Body .= "
";
$Body .= "Do you have a home loan pre-approval?: ";
$Body .= $rdb_value9;
$Body .= "
";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "
";
$Body .= "Email: ";
$Body .= $email;
$Body .= "
";
$Body .= "Phone No.: ";
$Body .= $phone;
$Body .= "
";


// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
  • 写回答

1条回答 默认 最新

  • duan01203 2018-02-07 09:03
    关注

    your javascript validation logic is checking for empty value. for radios need to use .checked.

    your code:

    y = x[currentTab].getElementsByTagName("input");
      // A loop that checks every input field in the current tab:
      for (i = 0; i < y.length; i++) {
        // If a field is empty...
        if (y[i].value == "") {
          // add an "invalid" class to the field:
          y[i].className += " invalid";
          // and set the current valid status to false
          valid = false;
        }
      }
    

    new code:

    y = x[currentTab].getElementsByTagName("input[type=text]");
          // A loop that checks every input field in the current tab:
          for (i = 0; i < y.length; i++) {
            // If a field is empty...
            if (y[i].value=="") {
              // add an "invalid" class to the field:
              y[i].className += " invalid";
              // and set the current valid status to false
              valid = false;
            }
      }
    z = 
    x[currentTab].getElementsByTagName("input[type=radio]");
    
    for(i=0;i<z.length;i++){
        if(z[i].checked){
         radioval[] = z[i].value;
       }
    }
    

    if tagname isnt pulling up the radios try id or classname. you can add some simple validation as well .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退