简体   繁体   中英

How to insert selected radio buttons?

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

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

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