简体   繁体   中英

How to send multiple radio button values in tab form

HTML:

<div class="tab"><h4>Zip Code</h4>
    <p><input type="text" class="text1" placeholder="Enter zip code..." oninput="this.className = ''" name="zipcode"></p>
  </div>
  <div class="tab"><h4>Question1</h4>
    <p><input type="radio" name="rdb" value="1-3 months"  class="option-input radio" checked/> 1-3 months</p>
    <p><input type="radio" name="rdb" value="4-6 months" class="option-input radio"> 4-6 months</p>
  </div>

  <div class="tab"><h4>Question2</h4>
    <p><input type="radio" name="rdb1" value="Single" class="option-input radio"  checked/> Single</p>
    <p><input type="radio" name="rdb1" value="Condo" class="option-input radio"> condo</p>
    <p><input type="radio" name="rdb1" value="Mobile" class="option-input radio">Mobile</p>
  </div>

  <div class="tab"><h4>Question3</h4>
    <p><input type="radio" name="rdb2" value="Moving" class="option-input radio" checked/> Moving</p>
    <p><input type="radio" name="rdb2" value="100" class="option-input radio"> 100</p>
  </div>

  <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button>
  <button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button>

JS

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");
  var rdb = document.getElementsByName('rdb').value;
  var rdb1 = document.getElementsByName('rdb1').value;
  var rdb2 = document.getElementsByName('rdb2').value;

  for (i = 0; i < y.length; i++) {
    if (y[i].value == "") {
      y[i].className += " invalid";
      valid = false;
    }
  }
  if (valid) {
    document.getElementsByClassName("step")[currentTab].className += " finish";
  }
  return valid; // return the valid status
}

PHP

$EmailFrom = "test@gmail.com";
$EmailTo = "xyz@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']));

$Body .= $zipcode;
$Body .= "Question1: ";
$Body .= $rdb_value;
$Body .= "Question2: ";
$Body .= $rdb_value1;
$Body .= "Question3: ";
$Body .= $rdb_value2;

Query is below: I use multiple tab form in HTML and use javascript and PHP to send all values to an email. please review the code above.

I received this email:

Zipcode: Question1: Question2: Question3:

Section one is HTML Code, Section two is Javascript and section three is Php code here!

//Html 
<form method="post" id="submit">
    Your html code goes here 
    <input type="button" onclick="onclick()" value="submit">
</form>

//javascript
function onclick()
{

$.ajax({type:'POST', url: 'email.php', data:$('#submit').serialize(), success: function(response) {
    alert(response); //success or fail from email.php page
}});

}

//php email.php
echo "<pre>";print_r($_POST); //here u can see all submitted data from html

Use document.querySelector('input[name=rdb]:checked').value; to get selected value from radio buttons

 function test(){ var rdb = document.querySelector('input[name=rdb]:checked').value; var rdb1 = document.querySelector('input[name=rdb1]:checked').value; var rdb2 = document.querySelector('input[name=rdb2]:checked').value; console.log(rdb,rdb1,rdb2); }
 .tab{ width:30%; float:left; }
 <form> <div class="tab"><h4>Question1</h4> <p><input type="radio" name="rdb" value="1-3 months" class="option-input radio" checked/> 1-3 months</p> <p><input type="radio" name="rdb" value="4-6 months" class="option-input radio"> 4-6 months</p> </div> <div class="tab"><h4>Question2</h4> <p><input type="radio" name="rdb1" value="Single" class="option-input radio" checked/> Single</p> <p><input type="radio" name="rdb1" value="Condo" class="option-input radio"> condo</p> <p><input type="radio" name="rdb1" value="Mobile" class="option-input radio">Mobile</p> </div> <div class="tab"><h4>Question3</h4> <p><input type="radio" name="rdb2" value="Moving" class="option-input radio" checked/> Moving</p> <p><input type="radio" name="rdb2" value="100" class="option-input radio"> 100</p> </div> <div style='clear:both;'> <button type="button" onclick="test()">Test</button> </div> </form>

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.

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