Jump to content


Photo

Very Basic Question - Collecting Values From Form

forms

  • Please log in to reply
26 replies to this topic

#1 joshspaulding

joshspaulding

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIndianapolis Area
  • Age:32

Posted 08 December 2012 - 10:50 AM

Hey everyone, this is my first post after my introduction. Loving all of the info I've already found here!

As I explained in my introduction, I'm new to learning PHP, but I do know html fairly well (although far from an expert) and I understand PHP implementation (what it's for, what it can do, when it should be used etc.)

So I have a very basic question for you all. I'm putting together a test site just to learn basic PHP hands-on. The site is amiarepublicanordemocrat dot com. I have the form setup in index.php and I have results.php created. But I can't figure out how to echo what the user's answers were, while associating an extra snippet of text next to each answer.

There are only two options per question and all I want results.php to display is the user's answer to each question.

Here is the form from index.php

<form name="question1" action="results.php" method="post">


<h4>1. Are you pro-life (against abortion) or pro-choice (for abortion)?</h4>
<input type="radio" name="q1" value="q1a1"/> : I am pro-life<br />
<input type="radio" name="q1" value="q1a2"/> : I am pro-choice


<h4><strong>2.</strong> Do you believe that gun rights are necessary to preserve safety, freedom and to protect us from tyranny, or do you believe unarmed citizens are safer citizens?</h4>
<input type="radio" name="q2" value="q2a1"/> : I support gun rights<br />
<input type="radio" name="q2" value="q2a2"/> : I believe citizens should be unarmed


<h4><strong>3.</strong> Do you believe in free markets (limited regulation on business) or do you believe the government should regulate business' to any extent necessary?</h4>
<input type="radio" name="q3" value="q3a1"/> : I support free markets<br />
<input type="radio" name="q3" value="q3a2"/> : I support regulating to any extent necessary<br />
<br />
<input type="submit" value="Tell me if I'm a Republican or Democrat!" />
</form>

And here is the PHP from results.php

<?php
$qone = $_POST['q1a1'];
$qone = $_POST['q1a2'];
$qtwo = $_POST['q2a1'];
$qtwo = $_POST['q2a2'];
$qthree = $_POST['q3a1'];
$qthree = $_POST['q3a2'];


if ($qone == "q1a1"){
echo "Your answer to question 1 is: I am pro-life. This is a republican/conservative view.<br />";


}elseif ($qone == "q1a2"){
echo "Your answer to question 1 is: I am pro-choice. This is a democrat/liberal view.<br />";


}elseif($qtwo == "q2a1"){
echo "Your answer to question 2 is: I support gun rights. This is a republican/conservative view.<br />";


}elseif ($qtwo == "q2a2"){
echo "Your answer to question 2 is: I believe citizens should be unarmed. This is a democrat/liberal view.<br />";


}elseif ($qthree == "q3a1"){
echo "Your answer to question 3 is: I support free markets. This is a republican/conservative view.<br />";


}elseif ($qthree == "q3a2"){
echo "Your answer to question 3 is: I support regulating to any extent necessary. This is a democrat/liberal view.<br />";
}
else {
echo "Well crap. If you're seeing this then I screwed up the code and it's not finding the answer variables. I'm thinking I need to learn more about arrays... need to figure out how to make results.php understand the difference between the two answers for each question. I think this is done by setting values for each answer in index.php then using arrays to 'convert' them over so they can be translated into the results.php page..?..";
}
?>

Am I WAY off or is just a simple little thing that I'm doing wrong? Thanks!!

Edited by joshspaulding, 08 December 2012 - 10:55 AM.


#2 joshspaulding

joshspaulding

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIndianapolis Area
  • Age:32

Posted 08 December 2012 - 11:08 AM

P.S. Not trying to to start any form of political discussion :) The site is only for testing purposes and even if I do promote it in the future it's strictly bi-partisan!

#3 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 08 December 2012 - 11:54 AM

Now would be the time to learn data driven designs, where you have a data structure (array, database table, ...) that defines what your code does for sets of related data. Then you have simple code that you don't need to change when the amount, content, order, .. of the data changes. The defining data structure is then used for producing the form and processing/validating the result from that form. Doing this also eliminates repeated code and data - DRY (Don't Repeat Yourself) and actually results in the simplest code.

Sample code showing a data driven design -

<?php

// $data[x][y] = x is the question #, y are the entries. 0 is the question/legend, 1,2,... are the choices
$data[1][0] = 'Are you  pro-life (against abortion) or pro-choice (for abortion)?';
$data[1][1] = array('legend'=>'I am pro-life','type'=>1);
$data[1][2] = array('legend'=>'I am pro-choice','type'=>2);
$data[2][0] = 'Do you believe that gun rights are necessary to preserve safety, freedom and to protect us from tyranny, or do you believe unarmed citizens are safer citizens?';
$data[2][1] = array('legend'=>'I support gun rights','type'=>1);
$data[2][2] = array('legend'=>'I believe citizens should be unarmed','type'=>2);
$data[3][0] = 'Do you believe in free markets (limited regulation on business) or do you believe the government should regulate business\' to any extent necessary?';
$data[3][1] = array('legend'=>'I support free markets','type'=>1);
$data[3][2] = array('legend'=>'I support regulating to any extent necessary','type'=>2);
?>

<form action="results.php" method="post">
<?php
// produce the questions/choices
foreach($data as $question=>$array){
    echo "<h4>$question. {$array[0]}</h4>\n";
    $num = count($array);
    for($x=1;$x<$num;$x++){
        echo "<input type='radio' name='question[$question]' value='$x'/> : {$array[$x]['legend']}<br />\n";
    }
}
?>
<br />
<input type='hidden' name='submit' />
<input type="submit" value="Tell me if I'm a Republican or Democrat!" />
</form>

<?php

// $data[x][y] = x is the question #, y are the entries. 0 is the question/legend, 1,2,... are the choices
$data[1][0] = 'Are you  pro-life (against abortion) or pro-choice (for abortion)?';
$data[1][1] = array('legend'=>'I am pro-life','type'=>1);
$data[1][2] = array('legend'=>'I am pro-choice','type'=>2);
$data[2][0] = 'Do you believe that gun rights are necessary to preserve safety, freedom and to protect us from tyranny, or do you believe unarmed citizens are safer citizens?';
$data[2][1] = array('legend'=>'I support gun rights','type'=>1);
$data[2][2] = array('legend'=>'I believe citizens should be unarmed','type'=>2);
$data[3][0] = 'Do you believe in free markets (limited regulation on business) or do you believe the government should regulate business\' to any extent necessary?';
$data[3][1] = array('legend'=>'I support free markets','type'=>1);
$data[3][2] = array('legend'=>'I support regulating to any extent necessary','type'=>2);

$types[0] = 'This is an undecided view.';
$types[1] = 'This is a republican/conservative view.';
$types[2] = 'This is a democrat/liberal view.';

// form processing code
if(isset($_POST['submit'])){
    // loop though the expected question data and display any results
    foreach($data as $question=>$array){
        echo "Your answer to question {$question} is: ";
        if(!isset($_POST['question'][$question]) || $_POST['question'][$question] < 1){
            // no radio selected
            echo "NONE. {$types[0]}<br />";
        } else {
            $answer = (int)$_POST['question'][$question];
            $type = $data[$question][$answer]['type'];
            echo "{$data[$question][$answer]['legend']}. $types[$type]<br />\n";
        }
    }
}
?>

Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#4 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 08 December 2012 - 11:57 AM

P.S. You should be learning php, developing php code, and debugging php code on a local development system. You will lose a lot of time uploading code just the see the result of each change or to troubleshoot each error.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#5 joshspaulding

joshspaulding

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIndianapolis Area
  • Age:32

Posted 08 December 2012 - 02:27 PM

P.S. You should be learning php, developing php code, and debugging php code on a local development system. You will lose a lot of time uploading code just the see the result of each change or to troubleshoot each error.


ok I'll have to look into that. I've been using dreamweaver for 8 years. Actually, I think I can setup Dreamweaver to do that... just have to research that documentation. Thanks for the suggestion!

Now would be the time to learn data driven designs, where you have a data structure (array, database table, ...) that defines what your code does for sets of related data. Then you have simple code that you don't need to change when the amount, content, order, .. of the data changes. The defining data structure is then used for producing the form and processing/validating the result from that form. Doing this also eliminates repeated code and data - DRY (Don't Repeat Yourself) and actually results in the simplest code.


Thank you very much! I'll definitely look into that, but I think for the time being it's too advanced. I'm still learning the very basics... I feel pretty comfortable with the basics that I've learned so far, but I haven't even learned about setting up MySQL databases and calling them etc. so I've got to learn the rest of the basics before I can even start to wrap my head around data driven designs I think.

From an operational standpoint, ignoring the fact that the code could be much more efficient is my code above just totally wrong? Am I way off base or is there just one or two simple things I'm doing that that I could easily fix? I'm just trying to learn from my mistakes. Thanks again!

#6 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 08 December 2012 - 03:57 PM

The form field name='...' attributes are the php $_POST['...'] variable names, so only $_POST['q1'], $_POST['q2'], and $_POST['q3'] can exist and only if one the radio buttons in a field with that name is selected.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#7 joshspaulding

joshspaulding

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIndianapolis Area
  • Age:32

Posted 12 December 2012 - 12:05 PM

The form field name='...' attributes are the php $_POST['...'] variable names, so only $_POST['q1'], $_POST['q2'], and $_POST['q3'] can exist and only if one the radio buttons in a field with that name is selected.


ahh ok. I inserted values in the html form, thinking that it would pull them. It's still not working for me though. Is my logic completely off?

#8 joshspaulding

joshspaulding

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIndianapolis Area
  • Age:32

Posted 12 December 2012 - 12:10 PM

Instead of using if and elseifs maybe I should use a switch statement with cases for each answer? Or is that not the problem at all?

#9 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 12 December 2012 - 12:18 PM

I inserted values in the html form


The form you posted already had values. It would probably help if you posted your current code.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#10 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 12 December 2012 - 12:19 PM

ahh ok. I inserted values in the html form, thinking that it would pull them. It's still not working for me though. Is my logic completely off?


It will retrieve the values (answers), when used correctly.

As @PFMaBiSmAd stated, you must use the name="" attribute as the $_POST index.

Where you have: <input type="radio" name="q1" value="q1a1"/>

You would then retrieve like so:

$qone = $_POST['q1']; // $qone now equals the value="" of the corresponding form input: q1a1

Which will set $qone to 'q1a1'... and so on.

To add, $_POST is used when your form method is set to post: <form method="post">

If you're form method is set to GET (<form method="get">), to retrieve the form data you must then use $_GET:

$qone = $_GET['q1'];


#11 joshspaulding

joshspaulding

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIndianapolis Area
  • Age:32

Posted 12 December 2012 - 12:22 PM

I know this is extremely basic stuff, so i appreciate you taking the time to help! I didn't paste the code again because I've changed it about 10 times and keep trying new things... unsuccessfully :) Obviously. Here is the latest.

<form name="questions" action="results.php" method="post">


<h4>1. Are you  pro-life (against abortion) or pro-choice (for abortion)?</h4>
<input type="radio" name="q1a" /> : I am pro-life<br />
<input type="radio" name="q1b" /> : I am pro-choice


<h4>2. Do you believe that gun rights are necessary to preserve safety, freedom and to protect us from tyranny, or do you believe unarmed citizens are safer citizens?</h4>
<input type="radio" name="q2a" /> : I support gun rights<br />
<input type="radio" name="q2b" /> : I believe citizens should be unarmed


<h4>3. Do you believe in free markets (limited regulation on business) or do you believe the government should regulate business' to any extent necessary?</h4>
  <input type="radio" name="q3a" /> : I support free markets<br />
<input type="radio" name="q3b" /> : I support regulating to any extent necessary<br />
  <br />
<input type="submit" value="Tell me if I'm a Republican or Democrat!" />
</form> 

<?php
$qone = $_POST['q1a'];
$qtwo = $_POST['q2a'];
$qthree = $_POST['q3a'];



if ($qone == "q1a1"){
echo "Your answer to question 1 is: I am pro-life. This is a republican/conservative view.<br />";



}elseif ($qone == "q1a2"){
echo "Your answer to question 1 is: I am pro-choice. This is a democrat/liberal view.<br />";



}elseif($qtwo == "q2a1"){
echo "Your answer to question 2 is: I support gun rights. This is a republican/conservative view.<br />";



}elseif ($qtwo == "q2a2"){
echo "Your answer to question 2 is: I believe citizens should be unarmed. This is a democrat/liberal view.<br />";



}elseif ($qthree == "q3a1"){
echo "Your answer to question 3 is: I support free markets. This is a republican/conservative view.<br />";



}elseif ($qthree == "q3a2"){
echo "Your answer to question 3 is: I support regulating to any extent necessary. This is a democrat/liberal view.<br />";
}
else {
echo "Well crap. If you're seeing this then I screwed up the code and it's not finding the answer variables. I'm thinking I need to learn more about arrays... need to figure out how to make results.php understand the difference between the two answers for each question. I think this is done by setting values for each answer in index.php then using arrays to 'convert' them over so they can be translated into the results.php page..?..";
}
?>


#12 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 12 December 2012 - 12:36 PM

That code will not work as you've now removed the values from the radio buttons.

Add the values back to the radio buttons and run again.

<input type="radio" name="q1a" value="q1a1"/> : I am pro-life<br />

and so on...

#13 joshspaulding

joshspaulding

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIndianapolis Area
  • Age:32

Posted 12 December 2012 - 12:38 PM

Breakthrough!! lol I got the answer to question one! But it's not showing the answer to the last two questions.

NEWEST CODE

<form name="questions" action="results.php" method="post">


<h4>1. Are you  pro-life (against abortion) or pro-choice (for abortion)?</h4>
<input type="radio" name="q1a" value="q1a"/> : I am pro-life<br />
<input type="radio" name="q1b" value="q1b"/> : I am pro-choice


<h4>2. Do you believe that gun rights are necessary to preserve safety, freedom and to protect us from tyranny, or do you believe unarmed citizens are safer citizens?</h4>
<input type="radio" name="q2a" value="q2a"/> : I support gun rights<br />
<input type="radio" name="q2b" value="q2b"/> : I believe citizens should be unarmed


<h4>3. Do you believe in free markets (limited regulation on business) or do you believe the government should regulate business' to any extent necessary?</h4>
  <input type="radio" name="q3a" value="q3a"/> : I support free markets<br />
<input type="radio" name="q3b" value="q3b"/> : I support regulating to any extent necessary<br />
  <br />
<input type="submit" value="Tell me if I'm a Republican or Democrat!" />
</form> 


<?php
$qone = $_POST['q1a'];
$qtwo = $_POST['q2a'];
$qthree = $_POST['q3a'];



if ($qone == "q1a"){
echo "Your answer to question 1 is: I am pro-life. This is a republican/conservative view.<br />";



}elseif ($qone == "q1b"){
echo "Your answer to question 1 is: I am pro-choice. This is a democrat/liberal view.<br />";



}elseif($qtwo == "q2a"){
echo "Your answer to question 2 is: I support gun rights. This is a republican/conservative view.<br />";



}elseif ($qtwo == "q2b"){
echo "Your answer to question 2 is: I believe citizens should be unarmed. This is a democrat/liberal view.<br />";



}elseif ($qthree == "q3a"){
echo "Your answer to question 3 is: I support free markets. This is a republican/conservative view.<br />";



}elseif ($qthree == "q3b"){
echo "Your answer to question 3 is: I support regulating to any extent necessary. This is a democrat/liberal view.<br />";
}
else {
echo "Well crap. If you're seeing this then I screwed up the code and it's not finding the answer variables. I'm thinking I need to learn more about arrays... need to figure out how to make results.php understand the difference between the two answers for each question. I think this is done by setting values for each answer in index.php then using arrays to 'convert' them over so they can be translated into the results.php page..?..";
}
?>

Edited by joshspaulding, 12 December 2012 - 12:44 PM.


#14 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 12 December 2012 - 12:42 PM

And, the name=',,,' attribute for each radio button in a set must be the same name. You can currently click on both radio buttons in a set and both will remain set (no pun intended there.)
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#15 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 12 December 2012 - 12:44 PM

ahh ok. See, now that confuses me again :) Sorry. Because I thought it was the "name="xxx" that is sent to the php file? Adding now though.


Within a form you have various inputs, textareas, etc.  Each are given a name (that are meant to send a value upon form submission).  The name is a representation of that input, just like giving a name to a person.  Each input also has a value (depending on the input type, some are addressed as value="some_value_here" where a textarea's value is like so: <textarea>value goes here</textarea>, and a select box takes its value from the child <option value="value goes here">something</option>.

It's the value that you're passing, and you're using the name as a way to retrieve it.  That way, your processing script can differentiate between x number of form inputs.  Without a name (aside from an ID which is not used in this particular case), your script don't know which input is which.

#16 joshspaulding

joshspaulding

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIndianapolis Area
  • Age:32

Posted 12 December 2012 - 12:50 PM

And, the name=',,,' attribute for each radio button in a set must be the same name. You can currently click on both radio buttons in a set and both will remain set (no pun intended there.)


That was my next question. Ok, I have that fixed, but now I keep getting the else statement again. I'm reading and looking into mrMarcus's last reply now.

Edited by joshspaulding, 12 December 2012 - 12:51 PM.


#17 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 12 December 2012 - 12:54 PM

In your form processing code, add the following line to display the actual data being submitted -

echo '<pre>',print_r($_POST, true),'</pre>';

Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

#18 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 12 December 2012 - 01:01 PM

Breakthrough!! lol I got the answer to question one! But it's not showing the answer to the last two questions.

NEWEST CODE

<form name="questions" action="results.php" method="post">


<h4>1. Are you  pro-life (against abortion) or pro-choice (for abortion)?</h4>
<input type="radio" name="q1a" value="q1a"/> : I am pro-life<br />
<input type="radio" name="q1b" value="q1b"/> : I am pro-choice


<h4>2. Do you believe that gun rights are necessary to preserve safety, freedom and to protect us from tyranny, or do you believe unarmed citizens are safer citizens?</h4>
<input type="radio" name="q2a" value="q2a"/> : I support gun rights<br />
<input type="radio" name="q2b" value="q2b"/> : I believe citizens should be unarmed


<h4>3. Do you believe in free markets (limited regulation on business) or do you believe the government should regulate business' to any extent necessary?</h4>
  <input type="radio" name="q3a" value="q3a"/> : I support free markets<br />
<input type="radio" name="q3b" value="q3b"/> : I support regulating to any extent necessary<br />
  <br />
<input type="submit" value="Tell me if I'm a Republican or Democrat!" />
</form> 


<?php
$qone = $_POST['q1a'];
$qtwo = $_POST['q2a'];
$qthree = $_POST['q3a'];



if ($qone == "q1a"){
echo "Your answer to question 1 is: I am pro-life. This is a republican/conservative view.<br />";



}elseif ($qone == "q1b"){
echo "Your answer to question 1 is: I am pro-choice. This is a democrat/liberal view.<br />";



}elseif($qtwo == "q2a"){
echo "Your answer to question 2 is: I support gun rights. This is a republican/conservative view.<br />";



}elseif ($qtwo == "q2b"){
echo "Your answer to question 2 is: I believe citizens should be unarmed. This is a democrat/liberal view.<br />";



}elseif ($qthree == "q3a"){
echo "Your answer to question 3 is: I support free markets. This is a republican/conservative view.<br />";



}elseif ($qthree == "q3b"){
echo "Your answer to question 3 is: I support regulating to any extent necessary. This is a democrat/liberal view.<br />";
}
else {
echo "Well crap. If you're seeing this then I screwed up the code and it's not finding the answer variables. I'm thinking I need to learn more about arrays... need to figure out how to make results.php understand the difference between the two answers for each question. I think this is done by setting values for each answer in index.php then using arrays to 'convert' them over so they can be translated into the results.php page..?..";
}
?>


As was stated, radio buttons in a set need to have the same name:

<form name="questions" action="results.php" method="post">








<h4>1. Are you  pro-life (against abortion) or pro-choice (for abortion)?</h4>
<input type="radio" name="q1" value="q1a"/> : I am pro-life<br />
<input type="radio" name="q1" value="q1b"/> : I am pro-choice








<h4>2. Do you believe that gun rights are necessary to preserve safety, freedom and to protect us from tyranny, or do you believe unarmed citizens are safer citizens?</h4>
<input type="radio" name="q2" value="q2a"/> : I support gun rights<br />
<input type="radio" name="q2" value="q2b"/> : I believe citizens should be unarmed








<h4>3. Do you believe in free markets (limited regulation on business) or do you believe the government should regulate business' to any extent necessary?</h4>
  <input type="radio" name="q3" value="q3a"/> : I support free markets<br />
<input type="radio" name="q3" value="q3b"/> : I support regulating to any extent necessary<br />
  <br />
<input type="submit" value="Tell me if I'm a Republican or Democrat!" />
</form>

In doing so, the radio button acts as a "choose one or the other".  With different names, they all become selectable.

#19 mrMarcus

mrMarcus

    I'm a beleafer.

  • Members
  • PipPipPip
  • 1,903 posts
  • LocationCanada

Posted 12 December 2012 - 01:08 PM

This works:

if (isset($_POST['submit'])) {
    $qone = $_POST['q1'];
    $qtwo = $_POST['q2'];
    $qthree = $_POST['q3'];

    if ($qone == "q1a"){
        echo "Your answer to question 1 is: I am pro-life. This is a republican/conservative view.<br />";
    }
    elseif ($qone == "q1b"){
        echo "Your answer to question 1 is: I am pro-choice. This is a democrat/liberal view.<br />";
    }
    if($qtwo == "q2a"){
        echo "Your answer to question 2 is: I support gun rights. This is a republican/conservative view.<br />";
    }
    elseif ($qtwo == "q2b"){
        echo "Your answer to question 2 is: I believe citizens should be unarmed. This is a democrat/liberal view.<br />";
    }
    if ($qthree == "q3a"){
        echo "Your answer to question 3 is: I support free markets. This is a republican/conservative view.<br />";
    }
    elseif ($qthree == "q3b"){
        echo "Your answer to question 3 is: I support regulating to any extent necessary. This is a democrat/liberal view.<br />";
    }
}
?>
<form name="questions" action="" method="post">

<h4>1. Are you  pro-life (against abortion) or pro-choice (for abortion)?</h4>
<input type="radio" name="q1" value="q1a"/> : I am pro-life<br />
<input type="radio" name="q1" value="q1b"/> : I am pro-choice

<h4>2. Do you believe that gun rights are necessary to preserve safety, freedom and to protect us from tyranny, or do you believe unarmed citizens are safer citizens?</h4>
<input type="radio" name="q2" value="q2a"/> : I support gun rights<br />
<input type="radio" name="q2" value="q2b"/> : I believe citizens should be unarmed

<h4>3. Do you believe in free markets (limited regulation on business) or do you believe the government should regulate business' to any extent necessary?</h4>
  <input type="radio" name="q3" value="q3a"/> : I support free markets<br />
<input type="radio" name="q3" value="q3b"/> : I support regulating to any extent necessary<br />
  <br />
<input type="submit" value="Tell me if I'm a Republican or Democrat!" name="submit" />
</form>

Next what you can work on is ensuring each radio box has been selected (if that's a requirement), by checking whether each set contains a value using empty()

if (empty($_POST['q1'])) {
        echo 'Please enter a response to question 1.';
    }

Edited by mrMarcus, 12 December 2012 - 01:09 PM.


#20 joshspaulding

joshspaulding

    Member

  • Members
  • PipPip
  • 26 posts
  • LocationIndianapolis Area
  • Age:32

Posted 12 December 2012 - 01:36 PM

AHHH I had the array keys wrong! It does work, thanks alot!! You too PFMaBiSmAd!

I see you added the following and i see it works with the submit input name. But it also works if I leave that line out and leave the input name out. What's the purpose of that code and the submit input name?

if(isset($_POST['submit'])){


Alrighty, I'll start looking into that now! Thanks again!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com