Jump to content

Recommended Posts

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

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";
       }
   }
}
?>

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.

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!

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.

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?

 

 

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'];

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..?..";
}
?>

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...

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

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.

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

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.

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

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!!

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?

 

 

 

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

 

That is to keep your processing code from executing even when the form hasn't been submitted (on page load).

 

I'm assuming you don't have any error reporting in place as you should/would have gotten 'undefined index' warnings.

 

It's best to keep my addition in place as you only want the form processing code to be run when the form has been submitted.

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.';
}

 

This displays the echo on the results page. Do you know how I could make a "required" pop up window display on submit if one of the questions isn't answered?

Ahhhaaaa. Basic html :)

 

required="required"

 

The required attribute is not respected by all browsers, ie. IE <=9, for example. I believe it falls within HTML5 capable browsers, so don't rely on it.

Edited by mrMarcus
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.