I have a form like below
I need to create an array for each radio button using JavaScript and I need to post to a PHP script using Ajax
<form id="what" name="what" >
<input type="radio" name="colors" id="red" value="red" />Red<br />
<input type="radio" name="colors" id="blue" value="blue" />Blue<br />
<input type="radio" name="colors" id="green" value="green" />Green
<input type="radio" name="animals" id="dog" value="dog" />Red<br />
<input type="radio" name="animals" id="parrot" value="parrot" />Blue<br />
<input type="radio" name="animals" id="horse" value="horse" />Green
<button type="button" >send</button>
</form>
My Ajax Posting Code
var data = 'username=' + username + '&api_password=' + apipassword + '&sender=' + sender + '&to=' + owner + "," + mobile + '&message=' + "Name : " + name +"%0d%0a"+ "Mobile No : " + mobile +"%0d%0a"+ "Address : " + street +" "+ area + " " + formlandmark +"%0d%0a"+ "Notes : " + notes + "%0d%0a" + "Order Id : " + randomnewnewnumber + "%0d%0a" + itemstosmsdata() + '&priority=' + priority;
var adminsubmit = 'name=' + name+'&mobile='+ mobile +'&adds='+ street +" "+ area + " " + formlandmark +'¬es='+ notes+'&orderid='+ randomnewnewnumber+'&orders='+ itemstowebdata()+'&status=opened'+'&time='+time+'&date='+ dates;
$('.text').attr('disabled','true');
$('.loading').show();
$.ajax({
url: "http://something.some.com/appost.php?", // Your Sms Api Url
type: "POST",
data: data,
cache: false,
success: function (html) {
alert("Order Placed");
if (html==1) {
$('.form').fadeOut('slow');
$('.done').fadeIn('slow');
}
}
});
The Data Sould Be Sent Like Below
radio[ { "radiobuttonename" => clicked value of the radio button},{ "radiobuttonename" => clicked value of the radio button}]
This code is to fill your array with all radio values within the form:
If you want to pass the checked attribute with the array, you can do this:
Finally, convert your array to string:
and now you can send your array with ajax