I have a text file:
$string =
'1 The most important feature of spiral model is: requirement analysis. risk management. quality management. configuration management 2 The worst type of coupling is: Data coupling. control coupling. stamp coupling. content coupling 3 One of the fault base testing techniques is: unit testing. beta testing. Stress testing. mutation testing 4 A fault simulation testing technique is: Mutation testing. Stress testing. Black box testing. White box testing 5 RS is also known as: specification of White box testing. Stress testing. Integrated testing. Black box testing 6 what is you name: Collins. Achieng. Ogwethi. Kamanda';

and a php code that reads from the text file and put the values into arrays;

        <b><u> QUESTIONS AND ANSWERS QUIZ</u></b> <br /><>
$openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text       file");
$string = fread($openFile, filesize("questionandanswers.txt"));

//Regex to get from the first number to the string's end, so we ignore the Number Question... bit;
preg_match('/\d.*/', $string, $match);

//We get all the strings starting with a number until it finds another number (which will be the beginning of another question;
preg_match_all('/\d\D*/', $match[0], $results);

$qas = array(); // We prepare an array with all the questions/answers
foreach($results[0] as $result){
    //Separating the answer from the string with all the answers.
    list($question, $all_answers) = explode(':', $result);

    //Separating the different answers
    $answers_array = explode('.', $all_answers);

    //Stuffing the question and the array with all the answers into the previously prepared array;
    $qas[] = array('question' => $question, 'answers' => $answers_array);

//Looping through the array and outputting all the info into a form;
foreach($qas as $k => $v){
    echo "<label>{$v['question']}</label><br/>";
    echo "<select>";

    //we loop through $v['answers'] because its an array within the array with all the answers.
    foreach($v['answers'] as $answer){
        echo "<option>$answer</option>";//the output
    echo "</select>";
    echo "<br/><br/>";


I want to be able to put the selected individual answer in such a manner that they can be posted to a php script for processsing e.g $_POST = "";.

I can't understand the whole stuff. but just a part of it... are you saying to separate the list of answers which was in your text file and display then select each of these answers and it insert it to an array?

or maybe I think that the text file is a questionnaire. hmm which is which?

The text file contains questions and respective multiple answers, the code reads from the text file and puts them into arrays of questions and their respective multiple answers. I am trying to put the questions and answers into a form format which can be selected and and the answers submitted to another php script for processing.

hmm are you saying about multiple choices?

Display to HTML from text file:

1.) Question number 1
Choices A.) answer 1 B.) Answer 2
2.) Question number 2
Choices A.) answer 1 B.) Answer 2

and if once an answer is selected, it will be in a form of an array. only the answers are provided in the array right?

Yes, you got it right...
Something like:
Question 1
a)    b)    c)    d)
Question 2
a)    b)    c)    d)
and the selected answer is put into an array which can be submitted as $_POST.

wait for a little moment. I am now coding what you want. Just for now. because I just finished my Satellite programming stuff here at SMART Communications Philippines

I would appreciate you assistance. Thank you.

as I can see from your text file. I wanna ask a a question is this array of ansswers that was selected like was in the form of...
your answer sheet's answers are
question 1. your answer is 2
question 2. your answer is 4

is this right or not?

I am now at the answers portion to proceed but an obstacle was on my mind so loet me hear from you what will be done in the array of selected answers.

This was the guideline of the text file:

The questions will be finished with a colon(:).
The answers will be finished with a dot(.), all of them but the last one, as we will use the number of the question as separator.
The questions or answers won't contain any numbers [0-9] or it will confuse the regex.

The array of selected answers once submitted can be used for example to add marks for correct answer or add 0 to wrong answer.

this part on was based on displaying your text file to HTML

modifying your HTML by inserting
after opening body tag place this:
<form action="<?php htmlspecialchars($_SERVER['PHP_SELF']);?>" method="POST">

look for your code block which has foreach($qas as $k=>$v)
block and edit it into this:

foreach($qas as $k=>$v){
echo "<label>".$v['question']."</label><br/>";
echo "<select name='selected_answers'>";
foreach($v['answers'] as $answer){
echo "<option value='".$answer."'>".$answer."</option>";
echo "</select>";
echo "<br/><br/>";

before your body closes, place this:

How will I be able to submit the form for processing? Thats the confusing part. Imnot seeing the submit button when I run the code on my local server. Thank again.

How to submitt the answers selected for example to another php file called processor.php that will use the posted information from each question to sum the correct answers and the wrong answer and give final verdict is what I want to achieve.

before your closing form place this
<input type="submit" value="Submit Answers">&nbsp <input type="reset" value ="Clear Answers">

oh so you do have now processor.php for getting all the values of your selected answers.

now next turn is to how do we make the selected answers pass down to array.

the answers is the name attribure of the select tag.
second rename your select tag's name for selecting answers in to <select name="selected_answers[]">
in this manner it will be in a form of an array.

since you have processor.php, kindly replace the form tag into
<form action="processor.php" method="POST">

Thanka a lot. One more thing; at the the form:

<form action="<?php htmlspecialchars($_SERVER['PHP_SELF']);?>" method="POST">
can I use a php page such that:

<form action="<processor.php" method="post"> or it will interfear with the codes? Thank you.

every on call of select tag's selected_answers[] it will have a value in an array.

FYI in using form action which $_SERVER['PHP_SELF'], you dont need to create a new page file unless this web page has multiple forms on one page.

in difference with $_SERVER['PHP_SELF'] with a predefined file. $_SERVER['PHP_SELF'] is used only for self page call refresh which is working only one form per html. unlike your predefined file, you can cast multiple forms in one HTML.

Please remove your "<" in your action attribute. what was inside the action attribute will be considered a character part of the filename if and only if it is not $_SERVER['PHP_SELF']

How will I be able to pass each array element to:
the array elelents will be the answers, and how will I be able to differentiate each answer of each question for the submitted values. Thank you.

okay lets test this.
does the submit button working to you processor.php?

if it is then on the beggining of you processor.php's php code, initialize this variable and print the array using print_r function it


by using this function we have to verify that there are values inserted to selected_answers array name.
kindly tell me if an error ascends to this code.

It does work and prints out the following if i use the above code:

Array ( [0] => configuration management [1] => stamp coupling [2] => Stress testing [3] => Black box testing [4] => Integrated testing [5] => Ogwethi ) 

And there is no error...

good. Job well done. then that is how you look for it. now since we are able to get the values correctly as what you want then this part. elaborate to me right now what will the process.php will do? will it save to the database? what will be it's task?

upon what you said earlier this post you mentioned this

the array elelents will be the answers, and how will I be able to differentiate each answer of each question for the submitted values. Thank you.

you mean you want to display like this?

for example

In my Question Number 1 your answer is... Choice number 2
In my Question Number 2 your answer is... Choice number 1

is that right?

Its task will be to check the answers chosen, among them there is one correct one and others are wrong. Processor.php should compute by adding one for each correct answer and adding zero for the wrong answers and at the end give out the verdict out of the total number of questions.

Thats what I am trying to accomplish.

so I didn't think of that you want an online examination system.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.