1

I want to create a survey using HTML, PHP, Javascript. So I have my question and answers in a csv file(eventually will be in a SQL server). What I need help is with how can i make my answers radio button(type) so the user can select one of them and I can process it later. The reason i am not hardcoding the questions is because i want to be able to edit them from a file(eventually server).

I have csv file that looks like this:

Question 1,Answer1 A,Answer1 B,Answer1 C,Answer1 D
Question 2,Answer2 A,Answer2 B,Answer2 C,Answer2 D

Now processing it with php the output looks like this:

1. Question 1
Answer1 A
Answer1 B
Answer1 C
Answer1 D
2. Question 2
Answer2 A
Answer2 B
Answer2 C
Answer2 D

The code i used for php is:

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $file = fopen('test.csv','r');
        while(($line = fgetcsv($file)) !== FALSE)
        {
            list($questions[], $optionA[], $optionB[], $optionC[], $optionD[]) = $line;
            //  print_r($line);
        }
        fclose($file);
        $lengthquestion = count($questions);
        $lengthoptionA = count($optionA);
        $lengthoptionB = count($optionB);
        $lengthoptionC = count($optionC);
        $lengthoptionD = count($optionD);

        echo '<ol>';
        for($i=0; $i<$lengthquestion; $i++)
        {
            echo '<li><div>';
            echo $questions[$i] . '<br />';
                echo $optionA[$i] . '<br />';
                echo $optionB[$i] . '<br />';
                echo $optionC[$i] . '<br />';
                echo $optionD[$i] . '<br />';
            echo '</div></li>';
        }
        echo '</ol>';
        ?>
    </body>
</html>

The output i want is, of course radio button on the left

 A) Answer1 A 
 B) Answer1 B 
 C) Answer1 C 
 D) Answer1 D

Update to main question:

Addition to the main question which is answered by @maytham:

On my top block of code i added this after php.

<form name="next" action="submit.php" method="post" id='1'>
    <input type="submit" value="Submit"/>
</form>

My real question is what should be in brackets ([]). Also can I call length question like that. Here is what i think code will look like:

Here is sumbit.php

<?php 
session_start();
$file="FILE.csv";
$fp = fopen($file, "a") or die("could not open");
for($i=0; $i<$lenghtquestion; $i++)
{ $_SESSION["answer' . $i'"] = $_POST["answer' . $i'"];
  $data.=$_SESSION["answer' . $i'"].",";
  fwrite($fp,$data) or die("could not write"); }
fclose($fp);

This doesn't work though

2
  • so, what have you tried? Commented Aug 4, 2015 at 22:18
  • Well I know, that each one of those options need to look like <input type='radio/> <label>$optionA[$i] </label> But dont know how to get there. Any example i saw online it was with a table. Commented Aug 4, 2015 at 22:25

1 Answer 1

1

You can do Radio buttons as group in your for loop:

for ($i = 0; $i < $lengthquestion; $i ++)
{
    echo '<li><div>';
    echo $questions[$i] . '<br />';
    echo '<input type="radio" name="group' . $i . '" value="' . $optionA[$i] . '">' . $optionA[$i] . '<br />';
    echo '<input type="radio" name="group' . $i . '" value="' . $optionB[$i] . '">' . $optionB[$i] . '<br />';
    echo '<input type="radio" name="group' . $i . '" value="' . $optionC[$i] . '">' . $optionC[$i] . '<br />';
    echo '<input type="radio" name="group' . $i . '" value="' . $optionD[$i] . '">' . $optionD[$i] . '<br />';
    echo '</div></li>';
}

Here how it looks like:

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, it works. But now i added a sumbit button to my page. That submit button calls a new php file (used action in form to call it). With that sumbit button i want to select the radio option selected for each quesiton. So my new php code looks like this: <?php session_start(); $_SESSION['group0']=$_POST['group0'] ; ?> all the way till lengthquestion. I will do the write this code neatly in anwer. Let me know what i am interpreting wrong.
OK I am happy it works, but I do not get your message here? could you re-explain in pedagogical way. and maybe show some examples what you want to achieve.
Sorry, i kind of thought of something so started trying that. But check my answer. I think i might be clear there.
I will look at it and come with solution, I am out now and will look at it soon I back . Mean while I have edited your question pls approve and then delete the answer, I have added to your main question.
No worries, I figured it out. Thanks on the first part. Second part was pretty stupid of me because i did not include the <form></form> so it was never calling the second php file. It works and does what i want it to do. Thank You very much for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.