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 6689991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:35:04+00:00 2026-05-26T05:35:04+00:00

I have an input form that allows me to insert a new quiz into

  • 0

I have an input form that allows me to insert a new quiz into a database. The form looks something like this:

 Question Title
    Question #1
        is_correct_1
        choice#1
        is_correct_2
        choice#2
        is_correct_3
        choice#3
        is_correct_4
        choice#4
    Question #2
    .
    .
    .

Different quizzes will having varying amounts of questions (although each question will always have 4 possibilities). I determine how many questions it will have before the form is constructed. In order to accomplish this I generate the form using a couple for loops. I also initialize the names of different input fields in the same manner. See below:

// Grab number of questions from Admin page
    $num_of_Qs = $_POST['num_of_Qs'];
// Produce form by using for loops
    echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
    echo '<fieldset><legend>New Quiz Details</legend><label for="title">Quiz Title</label>';
    echo '<input type="text" id="title" name="title" value="" /><br /><br />';
    for ($i = 1; $i <= $num_of_Qs; $i++) {
        echo '<label for="question_'.$i.'">Question #'.$i.'</label>';
        echo '<input type="text" id="question_'.$i.'" name="question_'.$i.'" value="" /><br /><br />';
        for ($x = 1; $x <= 4; $x++) {
            echo '<label for="is_correct_'.$i.'_'.$x.'">is_correct_'.$x.'</label>';
            echo '<input type="text" id="is_correct_'.$i.'_'.$x.'" name="is_correct_'.$i.'_'.$x.'" value="" /><br />';
            echo '<label for="choice_'.$i.'_'.$x.'">Choice #'.$x.'</label>';
            echo '<input type="text" id="choice_'.$i.'_'.$x.'" name="choice_'.$i.'_'.$x.'" value="" /><br /><br />';
        }
    }
    echo '</fieldset><input type="hidden" name="num_of_Qs" value="'.$num_of_Qs.'" />';
    echo '<input type="submit" value="Create" name="create" /></form>';

So, the variables end up looking something like this:

$title
$question_1
is_correct_1_1
choice_1_1  // first question, first choice
is_correct_1_2
choice_1_2 // first question, second choice
...

When I go to store those variables by grabbing it using the $_POST function, I’m having trouble. Here is my code:

// If user has submitted New Quiz data
    if (isset($_POST['create'])) {
        $num_of_Qs = $_POST['num_of_Qs'];
        $title = $_POST['title'];
        for ($i = 1; $i <= $num_of_Qs; $i++) { 
            $question_$i = $_POST['question_'.$i.''];
            for ($x = 1; $x <= 4; $x++) {
                $is_correct_$i_$x = $_POST['is_correct_'.$i.'_'.$x''];
                $choice_$i_$x = $_POST['choice_'.$i.'_'.$x.''];
            }
        }
        print_r($title);
        print_r($question_1);

        exit();
    }

I’m wondering if there is a way to grab the values from the form based on the structure I have determined for my variable names. The specific problem lies in $question_$i = .... Can I salvage this code or do I need to rethink the way I am naming these variables? Thanks!

  • 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-05-26T05:35:05+00:00Added an answer on May 26, 2026 at 5:35 am

    To answer your question, you can reference variables with strings like

    $var_1 = "hello";
    echo ${"var_1"};
    // or
    $str = "var_1";
    echo $$str;
    

    BUT DON’T DO THAT

    You want to store these values in an array.

    $question[$i] = $_POST['...'];
    $is_correct[$i][$x] = $_POST['...'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input
I have a web page that allows users to insert form input objects with
I have a form that allows users to paste input, like on StackOverflow. But
I have a dijit.form.NumberTextBox input field that starts out with these parms: new dijit.form.NumberTextBox({
I have a typical ADO.NET EF-driven form that allows the user to input a
Hi I have a simple form that allows a user to input a name,
I would like to create a form that allows the user to input any
Basically, I have this form that allows user to upload to my server: <form
I have a form that allows for multiple file uploads. <input name=uploadedfile[] type=file multiple=true/>
I have a website form that requires a US phone number input for follow

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.