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

  • Home
  • SEARCH
  • 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 7749043
In Process

The Archive Base Latest Questions

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

My code below is supposed to display each question ($_POST[questionText]). For each question, it

  • 0

My code below is supposed to display each question ($_POST[questionText]). For each question, it displays it’s SessionId, QuestionId, QuestionContent and OptionId. But instead it is only displaying 1 question. Why is it only displaying 1 question and how can I get it to display all of the questions?

I use an echo to text the output with the INSERT VALUES.

foreach($_POST['questionText'] as $i => $question)
{

      $insertquestion = array();


$options[] = $_POST['gridValues'];

switch ($options[$i]){

    case "3": 
    $selected_option = "A-C";
    break;

    case "4": 
    $selected_option = "A-D";
    break;

    default:
    $selected_option = "";
    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']; 


    $insertquestion[] = "'". mysql_real_escape_string($_SESSION['id'] ) . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". mysql_real_escape_string( $_POST['num_questions'] ) ."','".  mysql_real_escape_string( $question ) ."','".  mysql_real_escape_string( $optionid ) ."'";

    $questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId) 
    VALUES (" . implode('), (', $insertquestion) . ")";

    $i++;

    }

echo($questionsql);

Below is the javascript code and form code. How it works is the user types in a question in the textarea ('name='questionText') and types in an option (name='gridValues') and then they append them two in a table row (table in the form which id='qandatbl'). This is the question 1. Then they do the same again for second question, then third and etc. Please look at this carefully, it is easy to follow.

<script>

    function insertQuestion(form) {   

    var context = $('#optionAndAnswer');

    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
    var $question = $("<td class='question'></td>");
    var $options = $("<td class='option'></td>");
    var $questionType = '';

    $('#questionTextArea').each( function() {

    var $this = $(this);
    var $questionText = $("<textarea class='textAreaQuestion'></textarea>").attr('name',$this.attr('name')+"[]")
                   .attr('value',$this.val())

    $question.append($questionText);

    });

    $('.gridTxt', context).each( function() {

     var $this = $(this);
     var $optionsText = $("<input type='text' class='gridTxtRow maxRow' />").attr('name',$this.attr('name'))
                     .attr('value',$this.val())

    $options.append($optionsText);
    $questionType = $this.val();

    });

    $tr.append($question);
    $tr.append($options);    
    $tbody.append($tr); 

    }

</script>

    <form id="QandA" action="insertQuestion.php" method="post" >

    <h1>SESSION (<?php echo $_SESSION['id'] ?>)</h1>

    <table>
    <tr>
        <td rowspan="3">Question:</td> 
        <td rowspan="3">
            <textarea id="questionTextArea" rows="5" cols="40" name="questionText"></textarea>
        </td>
    </tr>
    </table>

    <table id="optionAndAnswer" class="optionAndAnswer">
    <tr class="option">
    <td>Option Type:</td>
    <td>
    <div>
        <input type="text" name="gridValues" class="gridTxt maxRow" readonly="readonly" />
    </div>
    </td>
    </tr>
    </table>

    <table id="qandatbl" align="center">
    <thead>
    <tr>
        <th class="question">Question</th>
        <th class="option">Option Type</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
    </table>

    </form>
  • 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-01T10:56:33+00:00Added an answer on June 1, 2026 at 10:56 am

    first. can you please change you java script to:

    var $optionsText = $("<input type='text' class='gridTxtRow maxRow' />").attr('name',$this.attr('name')+"[]")
                         .attr('value',$this.val())
    

    then your php code to:


    UPDATE

    $i = 0;
    $c = count($_POST['gridValues']);
    
    print_r($_POST['questionText']);
    print_r( $_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;
        }      
    
        $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']; 
    
        $insertquestion[] = "'". mysql_real_escape_string($_SESSION['id'] ) . 
                        ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". 
                        mysql_real_escape_string( $_POST['num_questions'] ) ."','".  
                        mysql_real_escape_string( $_POST['questionText'][$i] ) ."','".  
                        mysql_real_escape_string( $optionid ) ."'";
    
    }
    
     $questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId) 
        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

my code below is supposed to display N lines per inch. instead i get
I'm having a (probably super simple) issue. The code below is supposed to _POST
I suppose button should has Close title in the code below, but it has
Ok another question in VHDL. Below is my code. Suppose that I want my
The code below is supposed to prompt the user, step-by-step, for information. Currently, however,
I have the code below which is supposed to check the database for entries
I have this javascript code below that uses jquery, it is suppoed to be
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
Code below is working well as long as I have class ClassSameAssembly in same

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.