Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Luc de Brouwer
seal-mask
.a{fill-rule:evenodd;}techdegree
Luc de Brouwer
Full Stack JavaScript Techdegree Student 17,939 Points

How to use multiple questions with radiobuttons(Yes,No) and save input to Database with PHP

Hi everyone,

I have a webpage that has a form which provides the user with multiple questions per section that they can give answer to with either yes or no through a radio button which if the user presses submit at the end of the form, all of the radio inputs will be stored into a database that I have made.

Now my question is, how can I have multiple questions where the user can answer that with yes or no through radio buttons and will still be able to go to the next question.

Example:

 ---- Section questions one ----
1)  Question one  rb yes/no
2) Question two rb yes/no
3) Question three rb yes/no
4) Question four rb yes/no
5) Question fives  rb yes/no
--- Section questions two ---
1)  Question one  rb yes/no
2) Question two rb yes/no
3) Question three rb yes/no
4) Question four rb yes/no
5) Question fives  rb yes/no```
SUBMIT

question two is, how can I save these input values into a database?

What I have is:
a connection file (include 'connect.php'); to my database
a database with tables that can hold the information

But I'm uncertain how to query it

1 Answer

Hi there Luc!

You can simply store values in separate arrays. Look into input declarations and you will understand the point. Try to submit the form and play with code. Feel free to ask :)

<?php
if(isset($_POST)) {
    print_r($_POST);
}
?>

<form name="main_form" action="" method="POST">
    <h2>
        First Question
    </h2>
    <input type="radio" name="one[]" value="answer1">
    <input type="radio" name="one[]" value="answer2">
    <input type="radio" name="one[]" value="answer3">
    <input type="radio" name="one[]" value="answer4">
    <h2>
        Second question
    </h2>
    <input type="radio" name="two[]" value="answer1">
    <input type="radio" name="two[]" value="answer2">
    <input type="radio" name="two[]" value="answer3">
    <input type="radio" name="two[]" value="answer4">
    <br>
    <input type="submit">
</form>

Best regards.