Reputation: 1024
my php page contains a form with multiple radio buttons with different names as shown below:
for( $i=1; $i<=$unknownNumber; $i++ ){
echo("<input type=\"radio\" name=\""."radio".$i."\" value=\"cercle\" onchange=\"submit(this.form)\" checked >");
echo("<input type=\"radio\" name=\""."radio".$i."\" value=\"rectangle\" onchange=\"submit(this.form)\" >");
echo("<input type=\"radio\" name=\""."radio".$i."\" value=\"triangle\" onchange=\"submit(this.form)\" >");
}
How can i get the form data via $_POST[" ? "]
from a process php page?
I dont know what i should write into the quote of $_POST["..."]
, because the names of radio buttons are different.
A friend of mine advice me to get these Data just by doing as below,
foreach ($_POST as $answer) {
}
I get them with this method, but i am blocked to use ajax with it.
$(document).ready(function() {
$('#myForm').on('submit', function() {
var radio = $('#....').val(); // I dont know which name i should use here
$.post( "process.php", { radio: ? }, function(data){
alert('Good');
});
return false;
});
});
Upvotes: 1
Views: 1639
Reputation: 479
I modified your code. In php removed onclick event
for( $i=1; $i<=$unknownNumber; $i++ ){
echo("<input type=\"radio\" name=\""."radio".$i."\" value=\"cercle\" checked>");
echo("<input type=\"radio\" name=\""."radio".$i."\" value=\"rectangle\">");
echo("<input type=\"radio\" name=\""."radio".$i."\" value=\"triangle\">");
}
And JS modified to this
$(document).ready(function() {
$('input[name^="radio"]').on('click', function() {
$.post( "process.php", $("#myForm").serialize(), function(data){
alert('Good');
});
});
});
After each click on radio to your PHP script "process.php" will be sent full form. For exapmle here is POST data :
radio1:triangle
radio2:cercle
radio3:triangle
According to this request you will have
$_POST["radio1"]='triangle';
$_POST["radio2"]='cercle';
$_POST["radio3"]='triangle';
Update!!!!
In chrome developer tools you can see smth like this
Here is my process.php
$myArray = $_POST;
var_dump($myArray);
Upvotes: 1