PDA

View Full Version : PHP: gather value data from radio buttons?


batty13
February 18th, 2006, 05:36 AM
Hi,

Im hoping someone can help me understand how to finish this quiz form that I have created! The basics of the form using php worked perfect, name, message, and email were all gathered and sent via emails one to visitor one to me.
This next part has me a bit stumped, tried to figure it out and I know that Im close, just wanted to get some input here before I spin my wheels all night doing nothing. THe entire form has a bunch of true False questions, with three radio buttons. First is true, second is undecided third is false.
This is the html inside the form:

<!---------------------------------------------------------------------
<p>
2. I know how to gather value fields from buttons
</p>
<input type="radio" name="q1" value="true"><strong> T</strong><br />
<input type="radio" name="q1" value="undecided"> ? <br />
<input type="radio" name="q1" value="false"><strong> F</strong>
<div id="title"></div><br>
------>


For the php part I found a tutorial that suggested i do it something like
this

<!----------------------------------------------------------------------

//we set the switch on the type radio which we create in the html file
switch($_POST["radiobutton"])
{
case "true":
$val="true"; // if we pick 'T' then the value will be get this parameter
break;

case "undecided":
$val="undecided"; // if we pick the '?' then the value will be get this parameter
break;

case "false":
$val="false"; // if we pick 'F' then the value will be get this parameter

default:
$val="value";
}

echo "the option u have pick is ".$val;
--------->

I have two quesitons about this.

First: If i have several true and false questions, do i need to write that code block out for each of the questions?

Second: Do i have to use the default:$val="value"; statement??

Thanks for the help, sorry if its a big one. I really dont know php much at all. I just have some expereince in javascript.

Lokin

batty13
February 18th, 2006, 07:03 AM
Alright, so i ended up getting it to work with one minor flaw!!!

I have it setup with the multiple questions like this.

XHTML
radio set one is name="question1"
radio set two is name="question2"

PHP
switch($_POST["question1"]) +extended rules

switch($_POST["question2"]) +extended rules

then i have the mail results to me part, but the issues I am having is that It is sending the $val for answer one ? How do i get it to pull the data from
question2?

if(mail($your_email, "Email From $site_name", "
From: $name
Email: $email

Question1: $val

Question2: $val

Thanks in advance!!

degsy
February 18th, 2006, 12:46 PM
please post your full code

batty13
February 18th, 2006, 07:34 PM
certainly.

<!--Begin Contact Form inside of content-->
<!-- PHP mail funtion (place this script in your HTML page where you want the thank you response to appear) -->
<?php
// Note ///////////////////////////////////////////////////
// Fill in the sitename variable (name of your web site)
// and the your_email variable with your email address
//////////////////////////////////////////////////////////
$site_name="lokin crook";
$sitename = "test";
$your_email = "info@lokincrook.net";
$email = $_POST["email"];
$name = $_POST["name"];

//Question 1
switch($_POST["question1"])
{
case "true":
$val="true"; // if we pick 'T' then the value will be get this parameter
break;

case "undecided":
$val="?"; // if we pick the '?' then the value will be get this parameter
break;

case "false":
$val="false"; // if we pick 'F' then the value will be get this parameter
break;
}

//Question 2
switch($_POST["question2"])
{
case "true":
$val="true"; // if we pick 'T' then the value will be get this parameter
break;

case "undecided":
$val="undecided"; // if we pick the '?' then the value will be get this parameter
break;

case "false":
$val="false"; // if we pick 'F' then the value will be get this parameter
break;
}

//Question 3
switch($_POST["question3"])
{
case "true":
$val="let it go"; // if we pick 'T' then the value will be get this parameter
break;

case "undecided":
$val="undecided"; // if we pick the '?' then the value will be get this parameter
break;

case "false":
$val="mention it to them"; // if we pick 'F' then the value will be get this parameter
break;
}



// Auto-Resopnd to person sending mail indicating success
$autorespond = mail("$email", "$site_name ,Pre-Employment Questionnaire", "Thank you for sending your email to $site_name. We will get back to you as soon as we can.","From:$your_email\r\nReply-to:$your_email");

// Send the email to you
if(mail($your_email, "Email From $site_name", "
From: $name
Email: $email

1: $val

2: $val

3: $val

","From:$email\r\nReply-to:$email")){
echo "<br /><br /><strong> $name</strong>,<br /><br />You have successfully completed the Pre-Employment Questionnaire, <br />and your test results has been successfully submitted.";
}else{
echo "The message could not be sent. Please try again or contact <a href=\"mailto:$your_email\" title=\"\">$your_email</a>.";
}
?>
<!-- End PHP mail function -->

batty13
February 18th, 2006, 07:37 PM
The from questions are identical except the name part.

<input type="radio" name="question1" value="true"><strong> T</strong><br />
<input type="radio" name="question1" value="undecided"> ?<br />
<input type="radio" name="question1" value="false"><strong> F</strong><br /><br>

Thanks !!!!

oracle128
February 18th, 2006, 07:47 PM
The problem is, each switch statement you use (one for each question) is reusing the same variable, $val. You need each one to use a different variable (eg. $val1, $val2, $val3). You'll also need to change the email message, so it sends the results of $val1, $val2 and $val3, instead sending the same $val three times. Or, if you're a little more adenturous, use a for loop to analyse each question, and store the user's answer in an array.

batty13
February 18th, 2006, 07:52 PM
A for loop would be awesome. Except I dont know Php really and Im just winging this due to necessity. I do have experience with javascript and know how to write a For loop that way. Is is similiar in ease?

Thanks for your help. I knew it was something like that, I did try renaming the $vals but not in both places! Thanks!

oracle128
February 19th, 2006, 08:28 AM
For loops in any language are pretty much the same structure, and are themselves pretty easy. The only difficulty is in getting the questions and your code setup so it can easily utilize a loop - a good loop should treat every iteration as the same, and have as little 'specific code' as possible (that is, code that treats a certain iteration, or in your case, a quiz question, differently from the other iterations of the loop). That shouldn't be too hard for your script, since each quiz question has the same set of possible answers ("True", "False", or "Undecided").

Also, while loops are generally good practice, they're generally only useful for iterations of about 5 or more, largely depending on the code (I'm guessing you only have three questions there as an example/testing?). Though of course if you want to utilize one just for the sake of learning (or you do have more quiz questions than what you've got so far), we can assist you. You'll need to learn about two things - first, for loops. As I said, if you've done them in other languages, you'll have no problem here. The second concept you'll need to know is arrays.

For for loops, you can reference the PHP documentation here (http://www.php.net/manual/en/control-structures.for.php).

Arrays (http://au.php.net/types.array) are also covered in the PHP docs.

You can also search for tutorials online, simply google for "php" and the function/feature you want help with.
http://www.phpfreaks.com/
http://www.pixel2life.com/tutorials/PHP_Coding/All/

Now, for your code, you will not want use a for loop just once, but several times. Here's where:
-Displaying the questions
-Analysing the result
-Emailing the results

Displaying the questions
This will also need an array which contains the quiz questions. This PHP code will need to go inside the web page which contains your quiz form.
$questions = array("What is the answer to question 1?","What is the answer to question 2?","What is the answer to question 3?");For convenience, you'll want a variable that defines how many questions you have, to make things simple:$size = count($questions);That simply sets the $size variable to the number of questions on the $questions array. Next, onto actually displaying the questions:
for ($i=0;$i<$size;$i++)
{
echo $questions[$i]; //print question number $i
echo "<input type=\"radio\" name=\"question".$i+1."\" value=\"true\"><strong> T</strong><br />"; //need to use escape character to echo double quotes
echo "<input type=\"radio\" name=\"question".$i+1."\" value=\"undecided\"> ?<br />";
echo "<input type=\"radio\" name=\"question".$i+1."\" value=\"false\"><strong> F</strong><br /><br>";
}As you can see, the above loop while perform one iteration for every question in the $questions array. When outputting quotation marks ("), you need to use the escape character (in PHP and many languages, a backslash) so the PHP interpreter doesn't confuse an outputted quote mark with one that finished the output string. By using name=\"question".$i+1."\", the interpreter will output the line name="question1" on the first iteration (when $i is 0), name="question2", and so on.

Analysing the results
For analysing the form answers (and replace the PHP code you gave), try this:$val = array();
for ($i=0;$i<$size;$i++)
{
switch($_POST["question".$i+1])
{
case "true":
$val[]="true"; // if we pick 'T' then the value will be get this parameter
break;

case "undecided":
$val[]="?"; // if we pick the '?' then the value will be get this parameter
break;

case "false":
$val[]="false"; // if we pick 'F' then the value will be get this parameter
break;
}
}You might think that setting $val with each iteration will, like before, replace the value, rather than having a seperate variable for each answer. However, notice the double square brackets; the way arrays in PHP work, setting an array variable like that will add to the array rather than replace the existing value. Thus, the $val array will have a new array item containing the answer for each question.

If you POST the results of the quiz form to the same PHP page the quiz is on, you shouldn't have a problem with using the $size variable. However, if the results are analysed on a seperate PHP page, you will need a way to re-obtain the number of questions. This can simply be done by including a hidden field in the HTML of your quiz form:echo "<input type=\"hidden\" name=\"size\" value=$size>"This will send the field "size" to the PHP result analyser, so you can set the right size.

Emailing the results
Lastly, you'll use the contents of the $val array to send all the answers in the email. Given the above code, you should be able to figure that out. Note that you can get an element from an array with $val[$i]

batty13
February 19th, 2006, 08:57 AM
Hot damn!

You guys simply rock...