0

I'm new to this. I'm studying web development and have to create a php response form for a questionnaire which then inputs into a database. I'm having trouble with the radio buttons. I can't create the right code that makes an array and displays the answers in the response form/page.

This is my code:

<form name="modulequestionnaire" method="post" action="tania.responseform.php" />

<p><i>Rate each question from 6 to 1, six being strongly 
agree and one being strongly disagree.</i></p>

1. I think the module guide/student handbook provided enough information about the 
module content, organisation and assessment.<br/>

6<input type="radio" name="answer[1]" value="6"> 5<input type="radio" name="answer[1]" value="5"> 
4<input type="radio" name="answer[1]" value="4"> 3<input type="radio" name="answer[1]" value="3"> 
2<input type="radio" name="answer[1]" value="2"> 1<input type="radio" name="answer[1]" value="1">
</p>

2.The module was well organised.<br/>

6<input type="radio" name="answer[2]" value="6"> 5<input type="radio" name="answer[2]" value="5"> 
4<input type="radio" name="answer[2]" value="4"> 3<input type="radio" name="answer[2]" value="3"> 
2<input type="radio" name="answer[2]" value="2"> 1<input type="radio" name="answer[2]" value="1"> 
</p>

3.The Learning Resource Centre provided adequate materials for the module.<br/>

6<input type="radio" name="answer[3]" value="6"> 5<input type="radio" name="answer[3]" value="5"> 
4<input type="radio" name="answer[3]" value="4"> 3<input type="radio" name="answer[3]" value="3"> 
2<input type="radio" name="answer[3]" value="2"> 1<input type="radio" name="answer[3]" value="1"> 
</p>

I know the answer can relate to the isset function but I don't know how to code it. Could someone possibly teach or help me out here?

6
  • Can you post your existing PHP attempt? Commented Jan 6, 2014 at 22:05
  • @HC_ I want to create answers for the questions above. Commented Jan 6, 2014 at 22:09
  • @sjagr something like this but it doesnt work.: <?php $answer=$_POST['answer']; print "Answers: "; switch ($answer) { case 1: print "6<br/>"; break; case 2: print "5<br/>"; break; case 3: print "4<br/>"; break; case 4; print "3<br/>"; break; case 5; print "2<br/>"; break; case 6; print "1<br/>"; break;}; ?> Commented Jan 6, 2014 at 22:10
  • Please post your attempts in your question instead of in comment. It's easier for people to read also. @taniakeira Commented Jan 6, 2014 at 22:11
  • @Fred-ii- okay I will, sorry, new to this. Commented Jan 6, 2014 at 22:12

2 Answers 2

7

When you're unsure of how to handle the HTML markup that you've set up, you should var_dump($_POST) the values that are sent to the PHP handler page so you know what the format will look like, that way you can proceed from there.

When I created your HTML and tested it with a var_dump and some random selections, the output was

array(2) { ["answer"]=> array(3) { [1]=> string(1) "5" [2]=> string(1) "3" [3]=> string(1) "4" } ["submit"]=> string(6) "Submit" }

Notice that there is an array within the $_POST['answer'] variable. So you should foreach over each element in that array to handle each respective value:

foreach ($_POST['answer'] as $answer) {
    // do stuff with the answer
}

If you need to work with the answer number that you defined in the POST array, you can foreach with a key:

foreach ($_POST['answer'] as $answerNum => $answer) {
    // do stuff with $answerNum and $answer
}

You can, of course, access your answer by its number directly:

if (!empty($_POST['answer'][1])) { // To ensure that the value is being sent
    // do stuff with $_POST['answer'][1]
}
Sign up to request clarification or add additional context in comments.

Comments

0

I don't imagine that this is quite what you are looking to do. If instead you give the three questions different names:

<form name="modulequestionnaire" method="post" action="tania.responseform.php" />

<p><i>Rate each question from 6 to 1, six being strongly 
agree and one being strongly disagree.</i></p>

1. I think the module guide/student handbook provided enough information about the 
module content, organisation and assessment.<br/>

6<input type="radio" name="answer1" value="6"> 5<input type="radio" name="answer1" value="5"> 
4<input type="radio" name="answer1" value="4"> 3<input type="radio" name="answer1" value="3"> 
2<input type="radio" name="answer1" value="2"> 1<input type="radio" name="answer1" value="1">
</p>

2.The module was well organised.<br/>

6<input type="radio" name="answer2" value="6"> 5<input type="radio" name="answer2" value="5">



4<input type="radio" name="answer2" value="4"> 3<input type="radio" name="answer2" value="3"> 
2<input type="radio" name="answer2" value="2"> 1<input type="radio" name="answer2" value="1"> 
</p>

3.The Learning Resource Centre provided adequate materials for the module.<br/>

6<input type="radio" name="answer3" value="6"> 5<input type="radio" name="answer3" value="5"> 
4<input type="radio" name="answer3" value="4"> 3<input type="radio" name="answer3" value="3"> 
2<input type="radio" name="answer3" value="2"> 1<input type="radio" name="answer3" value="1"> 
</p>

then in php all you will need to do is find the values using the $_POST variable as such

<?php 
    echo $_POST['answer1'];
    echo $_POST['answer2'];
    echo $_POST['answer3'];
?>

2 Comments

This works but derives from the purpose of the lesson that OP needs to learn about creating and handling POST arrays. It is especially useful to code using POST arrays because it means less work to add in a single question if the code is done right.
True, I guess I would personally stay away from post arrays because they can become confusing quickly. I only really use post arrays when I have a varying number of inputs that I am taking in, such as a multiselect, I am not sure why you would want to use an array in this case though. Except for the sake of a lesson... okay, you win this round :)

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.