0

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);

1 Answers1

2

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

Array ( [reply1] => Single [reply2] => Multiple )     

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] and reply[1]. PHP knows to makes values named like into to an array.

<?php

//This is what your values will look like. Notice that they are in array format now
print_r($_POST);

foreach($_POST['reply'] as $reply) {
    //Now you can loop through your replies correctly.
}

?>

<!-- Quick Example of how to name the radio buttons -->
<html>
<head></head>
<body>
<form method="post">
Row 1<br />
<input type="radio" value="Single" name="reply[0]" /> Single 
<input type="radio" value="Multiple" name="reply[0]" /> Multiple

<br />Row 2<br />
<input type="radio" value="Single" name="reply[1]" /> Single 
<input type="radio" value="Multiple" name="reply[1]" /> Multiple
<br />
<input type="submit" value="Submit" />
</form>
</body>


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.

<?php

$insertquestion = array();

foreach($_POST['reply'] as $reply) {

    switch ($reply){

    case "Single": 
    $selected_reply = "Single";
    break;

    case "Multiple": 
    $selected_reply = "Multiple";
    break;

    default:
    $selected_reply = "";
    break;

    $replyquery = "SELECT ReplyId FROM Reply WHERE (ReplyType = '". mysql_real_escape_string($selected_reply)."')";
    $replyrs = mysql_query($replyquery);
    $replyrecord = mysql_fetch_array($replyrs);
    $insertquestion[] = $replyrecord['ReplyId'];       
}

$questionsql = "INSERT INTO Question (ReplyId) 
    VALUES (" . implode('), (', $insertquestion) . ")";

echo($questionsql);

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.

<?php

$insertquestion = array();

for($i = 0; $i < count($_POST['reply']), $i++) {

    switch ($_POST['reply'][$i]){

    case "Single": 
    $selected_reply = "Single";
    break;

    case "Multiple": 
    $selected_reply = "Multiple";
    break;

    default:
    $selected_reply = "";
    break;

    $replyquery = "SELECT ReplyId FROM Reply WHERE (ReplyType = '". mysql_real_escape_string($selected_reply)."')";
    $replyrs = mysql_query($replyquery);
    $replyrecord = mysql_fetch_array($replyrs);
    $insertquestion[] = $replyrecord['ReplyId'];       
}

$questionsql = "INSERT INTO Question (ReplyId) 
    VALUES (" . implode('), (', $insertquestion) . ")";

echo($questionsql);
Gohn67
  • 10,608
  • 2
  • 29
  • 35
  • Can I ask you one question, where I am appending the rows into a table, do I change the name attribute for the radio buttons there as well or only in the form? In other words where it says `name='reply"+count"`? and in the dorm itself do I put [0] at the end of the name attribute "reply" – user1304328 Apr 09 '12 at 01:50
  • Yes the name of the radio buttons must be reply[0],reply[1] etc to use the values as an array in PHP So yes, instead of `name='reply"+count"` do `'reply["+count+"]'`. There are also ways to do it in the reply1, reply2 format, but it requires some more logic on the php side. – Gohn67 Apr 09 '12 at 02:06
  • If I change `name='reply"+count" do 'reply["+count+"]'` then if I select a radio button on top and then append it into a table row, it then deselects the selected radio button. I updated fiddle so you can see this – user1304328 Apr 09 '12 at 02:20
  • Yeah, it may be this line `input[name=reply"+count+"]"`, you'll notice that it doesn't have the open bracket and close brackets around count, which I guess may clash with the jquery syntax? – Gohn67 Apr 09 '12 at 02:26
  • Kind of ugly, but may want to look at this http://stackoverflow.com/questions/2364982/jquery-selector-for-inputs-with-square-brackets-in-the-name-attribute – Gohn67 Apr 09 '12 at 02:28
  • Eh, nvm, just change this line `$replies.find("input[name=reply"+count+"]").eq` to `$replies.find('input[name="reply['+count+']"]').eq` I haven't tried it but I believe that should work. – Gohn67 Apr 09 '12 at 02:32
  • Ok, I edited your fiddle, it looks like it works now -> http://jsfiddle.net/fPtMq/32/ – Gohn67 Apr 09 '12 at 02:37
  • Unfortunately it didn't save. Bummer. – Gohn67 Apr 09 '12 at 02:37
  • Ok I can see that is working thanks, I will get back to you if I get the insert values working. Once that is working I will mark your answer but I need to get that working, give me 10 mins to get it working :) – user1304328 Apr 09 '12 at 02:43
  • I keep getting a blank value inserted for ReplyId, what am I doing wrong? Code is updated in question above to show my updated php code – user1304328 Apr 09 '12 at 02:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/9840/discussion-between-gohn67-and-user1304328) – Gohn67 Apr 09 '12 at 02:59
  • I'm in the chat room. It will be much faster that way. But initially I noticed that `switch ($_POST['reply']){` should be `switch ($reply){` And also you are looping through the replies in another loop.. I'm not sure why. – Gohn67 Apr 09 '12 at 03:07