Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7759085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:32:18+00:00 2026-06-01T13:32:18+00:00

I have a jsfiddle here . What happens is that if you select a

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:32:19+00:00Added an answer on June 1, 2026 at 1:32 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a css transform that happens when a button is clicked shown here
I have a Jsfiddle application here . If you type in a question in
I have a CSS mouseover slider fully functional here http://jsfiddle.net/gU4sw/13/ . When I add
Here's an example of my problem on jsFiddle. I have a table with striped
Have a look at this jsfiddle: http://jsfiddle.net/Dh96F/1/ The Expand button will animate the widening
I have some code here: http://jsfiddle.net/zkRq2/2/ that I am having issues with. If you
I have a fiddle here http://jsfiddle.net/WULsZ/1/ I load jQuery first and the code is
I have a pure CSS mouseover slider fully functional here http://jsfiddle.net/gU4sw/13/ . When I
I have created a jsfiddle of my problem. Here is a link to the
I have a flot bar chart here as a jsFiddle . Now i need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.