Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

looping through a form

General discussions related to php

Moderators: macek, egami, gesf

looping through a form

Postby tony09uk » Tue Nov 27, 2012 12:33 pm

I am creating a quiz builder as part of an exercise. It allows the user to make a multiple choice quiz. They will be able to add a title, choose the number of questions they want and how many options they want for each question by inputting the values into a html form (all questions will have the same number of options). The code I have made for this is:

Code: Select all
        <form method="post" action="quiz_form.php">     

        Quiz name<br/><input type="text" name="name" value="" maxlength="" /><br/>
        Choices<br/><input type="text" name="choices" value="" maxlength="3" /><br/>
        Questions<br/><input type="text" name="questions" value="" maxlength="3" /><br/>

        <input type="submit" name="submit" value="SUBMIT" />


    </form>


The next part of the quiz maker is to generate the html for the quiz and allow the user to input their questions in to that HTML. The code I have made for this is:
Code: Select all
 if (isset($_POST['submit'])) {//form submitted
$errors = array();

if (isset($_POST['choices'])) {//Number of choices per question

    if (is_numeric($_POST['choices'])) {//it is a number

        if (strlen($_POST['choices']) <= 3) {//there are no more than three charaters

            $choices = $_POST['choices'];

        } else {//add to errors array

            $errors[] = "<b>CHOICES: </b>You may only go upto 999 questions";
        }

    } else {//add to errors array

        $errors[] = "<b>CHOICES: </b>Must be a number";

    }
} else {//add to errors array

    $errors[] = "<b>CHOICES: </b>Select number of choices";

}

if (!empty($_POST['name'])) {//quiz name added

    $quizname = $_POST['name'];

} else {//add to errors array

    $errors[] = "<b>Quiz name: </b>Add quiz name";

}

if (isset($_POST['questions'])) {//number of questions specified

    if (is_numeric($_POST['questions'])) {//it is a number

        if (strlen($_POST['questions']) <= 3) {//there are no more than three charaters

            $questions = $_POST['questions'];

        } else {//add to errors array

            $errors[] = "<b>QUESTIONS: </b>You may only go upto 999 questions";

        }

    } else {//add to errors array

        $errors[] = "<b>QUESTIONS: </b>Must be a number";

    }

} else {//add to errors array

    $errors[] = "<b>QUESTIONS: </b>Select the number of questions";

}

if (empty($errors)) {//display choices

    if(isset($questions) && isset($choices)){//choices and question number selected

        echo "<h1>Change the values in each box</h1>\n";//headline

        echo "Quiz title: " . $quizname; //users quiz title

        echo "<form method=\"post\" action=\"addToDb.php\">\n";//start form

        for($q = 1; $q <= $questions; $q++){//loop through number of questions required

            echo "  <input name='questions[]' type='text' value='question $q' /><br/>\n";

            for($opt = 1; $opt <= $choices; $opt++){//loop through number of options required

               echo "   <input name='choice[]' type='text' value='option $opt' /><br/>\n";

            }

        }

        echo "  <input name='submit' type='submit' value='SUBMIT' />\n";//submit form
        echo "</form>";//close form

    }

} else {//echo errors

    foreach ($errors AS $v) {
        echo $v . "<br/>";
    }
}

} else {//end $_POST['submit']
echo "fill out form";
}


Finally I am trying to show what the user has created (This is where my first problem arises!). The code I have made for this is:

Code: Select all
if(isset($_POST['questions']) && isset($_POST['choice'])){

$q = $_POST['questions'];
$c = $_POST['choice'];

foreach($q AS $v){
    echo $v . "<br/>\n";
    foreach($c AS $value){
        echo "  " . $value . "<br/>\n";
    }       

}

}else{
    echo 'not set';
}



When I run the above program I get a problem at the final stage. That problem is that the options do not seem to be linked to the questions i.e.

What I expect (Assuming 2 questions and 2 options are chosen):

question 1
option 1
option 2

question 2
option 1
option 2

What I get (Assuming 2 questions and 2 options are chosen):

question 1
option 1
option 2
option 1
option 2

question 2
option 1
option 2
option 1
option 2

a previous answer has suggested using the count() function and I have tried putting that advice into practice, but cant without success. I have considered using sessions, but I am sure that sessions would not be the right way to go.

I assume that the loop is not linking the questions and choices array together for some reason and need to work out a way to get the code to link the two in order to get the output I expect.

Please can someone tell me what I am missing.

PS I know there are security problems at the moment, and will address them once I can complete the basics of this task.
tony09uk
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Nov 05, 2012 2:11 pm

Return to PHP General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.