简体   繁体   中英

how to send the radio button information in ajax based form in PHP?

I have made an ajax based form for the website, it takes input from the user and sends it to the database. All fields are working fine but there is a problem with the radio buttons in sending the values. My code for the ajax is

 <script type="text/javascript">
 function emp1() {
    .....................
xmlhttp.send("firstname=" + firstname.value + "&lastname=" + lastname.value  + "&company=" + company.value             
    + "&myemail=" + myemail.value
+ "&address=" + address.value
+ "&city=" + city.value
+ "&province=" + province.value
+ "&postalcode=" + postalcode.value
+ "&country=" + country.value
+ "&contact=" + contact.value
+ "&radio1=" + radio1.value
+ "&paymentmethod=" + paymentmethod.value
);

} code for html form for radio buttons is

<input type="radio" name="radio1" id="radio1" value="Free Shipping"  onclick="emp1();"> Free Shipping<br />  
<input type="radio" name="paymentmethod" id="paymentmethod"  value="abc"  onclick="emp1();">abc
<br />
<input type="radio" name="paymentmethod" id="paymentmethod" value="def"  onclick="emp1();" onchange="emp1();">def
<br /> 
 <input type="radio" name="paymentmethod" id="paymentmethod" value="ghi"  onclick="emp1();">ghi

Any help ... .. thnx in advance

You can use something like this to get selected radio button vlaue using javascript inside your emp1() function. it will give you selected radio button value.

var inputs = document.getElementsByName("paymentmethod");
    for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].checked) {
        var paymentmethod = inputs[i].value;
      }
    }

    alert(paymentmethod);

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