I have a jsfiddle here. What happens is that if you select a radio button and click on “Add Question”, it will add a table row showing the radio button you have selected. You can change a selection within the row.
Now what I want to do is that I want to insert the selected radio buttons in each in the database by using the INSERT VALUES method.
So what I want to know is how can I correctly do this so that it $_POST the selected radio buttons for each row and then be able to insert them using INSERT VALUES?
Below is the php code I currently have: (The ‘questionText’ is the ‘Question’ column where it actually picks out each row even though I have not included ‘questionText’ in the jsfiddle and the ‘gridValues’ is not in the jsfiddle but that is for each textbox value in each row in the ‘Options’ column, so just imagine there are additional two columns in the table which is ‘Question’ and ‘Options’ column)
$i = 0;
$c = count($_POST['gridValues']);
$insertquestion = array();
for($i = 0; $i < $c; $i++ ){
switch ($_POST['gridValues'][$i]){
case "3":
$selected_option = "A-C";
break;
case "4":
$selected_option = "A-D";
break;
default:
$selected_option = "";
break;
}
foreach($_POST['reply'] as $reply) {
switch ($_POST['reply']){
case "Single":
$selected_reply = "Single";
break;
case "Multiple":
$selected_reply = "Multiple";
break;
default:
$selected_reply = "";
break;
}
}
$optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = '". mysql_real_escape_string($selected_option)."')";
$optionrs = mysql_query($optionquery);
$optionrecord = mysql_fetch_array($optionrs);
$optionid = $optionrecord['OptionId'];
$replyquery = "SELECT ReplyId FROM Reply WHERE (ReplyType = '". mysql_real_escape_string($selected_reply)."')";
$replyrs = mysql_query($replyquery);
$replyrecord = mysql_fetch_array($replyrs);
$replyid = $replyrecord['ReplyId'];
$insertquestion[] = "'".
mysql_real_escape_string( $_POST['questionText'][$i] ) ."','".
mysql_real_escape_string( $optionid ) ."','".
mysql_real_escape_string( $replyid ) ."'";
}
$questionsql = "INSERT INTO Question (QuestionContent OptionId, ReplyId)
VALUES (" . implode('), (', $insertquestion) . ")";
echo($questionsql);
I noticed in your jsfiddle that each new created radio box gets created like reply1 and reply2.
But in your php code, it looks like you’re looping through as if it were an array.
If you do a print_r of your post values, you get something like
So they are not in an array format. Granted if the only values you had in your POST request were the radio buttons, then you could loop through the POST array values.
Anyway here is a possible solution for you to loop through your post requests. I just based it off your example in jsfiddle. You’ll probably have to adapt to fit your actual code. But they key is to note that the name of the radio buttons are named like
reply[0]andreply[1]. PHP knows to makes values named like into to an array.Here is your above code rewritten for only replies. I left the other stuff out since I wasn’t sure how the data was formatted or the data actually was. I am wondering why you don’t just use the id’s for the for radio box values instead of text. That way you don’t have to keep query the database. You can just query for all reply types and then match against the id like that.
Here is basically the same thing except using a for loop. I’m guessing you are not familiar with foreach loops. But also a for loop may serve you better in this case.