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

Variables from radio buttons into MySQL table

Hi fellas,

I`m trying to create page for my philosophical academic research paper, using wordpress (because of reasons), and it requires some PHP knowledge I dont possess..yet.

What I Dream to Achieve

After using mt_rand, user is assigned to one of three groups. Whole code is plainly 3 if statements corresponding to each group. In each group user is asked several questions with radio type input. Answers from these inputs should be attached to variables which then should be send to MySQL table.

ilustration of the page code:

example

The Problems

Is there a treehouse video with explanation of these radio type inputs in php?

...and mainly, is there any way to transform answers from radio type input to variables without resending user to another page, (and maybe there are treehouse lessons coping with that?)

Dont know if question is clear, but thanks in advance!

4 Answers

Andrew McCormick
Andrew McCormick
17,730 Points

basic form:

<FORM name ="form1" method ="post" action ="">
<Input type = 'Radio' Name ='answer' value= 'yes'> Yes
<Input type = 'Radio' Name ='answer' value= 'no'> No
<Input type = "Submit" Name = "Submit1" VALUE = "Select a Radio Button">
</FORM>

PHP at the beginning of the file:

if(isset($_POST['Submit1'])) {
$selected_option = $_POST['answer'];
//some statements to add $selected_option to your DB
}

does that help?

Thanks, Andrew! Tried it, but the problem is that it refreshes the page when the submit1 is clicked, and in that way it ruins the purpose (after refreshing mt_rand assigns the group over again). Perhaps Im looking for something that could hold a value after selection without refreshing/redirecting to another page - that is, until all the radios have been selected.

looks difficult, but it is what I need! Thanks again!