Page 1 of 1

Sending Session Variable Values to Radio Buttons

Posted: Thu Jan 04, 2007 2:57 am
by Sherrie
Hi all!

I'm pretty new to PHP, and I have been tasked by my boss to develop an online employee opinion survey.

There are about 100 questions which are split into 10 pages (10 questions per page). On each form there is a Next Page button and a Previous Page button which inputs the values of the current form into the session variable array.

However, when my Previous Page button is clicked and the user I get the message that the header has already been sent. If I just redirected to the previous page, the previous page is blank without remembering the user's input although I have tested my session variables and they work.

How do I get the values out of the session variables to use them in a radio button form?

here's my code so far:

Code: Select all

<?php 
session_start(); 
if (isset($_POST['Submit'])) { 
$_SESSION['answer'][5] = $_POST['q6']; 
$_SESSION['answer'][6] = $_POST['q7']; 
$_SESSION['answer'][7] = $_POST['q8']; 
$_SESSION['answer'][8] = $_POST['q9']; 
$_SESSION['answer'][9] = $_POST['q10'];
header("Location:page3.php");
}
elseif (isset($_POST['Submit2'])) {
$_SESSION['answer'][5] = $_POST['q6']; 
$_SESSION['answer'][6] = $_POST['q7']; 
$_SESSION['answer'][7] = $_POST['q8']; 
$_SESSION['answer'][8] = $_POST['q9']; 
$_SESSION['answer'][9] = $_POST['q10'];
var_dump($_POST);
header("Location:page1.php");
}
?>

<html>
<head>
<title>Survey Page 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="page2.php">
  <table width="85%" border="0" align="center" cellpadding="0" cellspacing="2">
    <tr align="center" bgcolor="lightgrey"> 
      <td width="5%"><b><font size="2" face="Arial, Helvetica, sans-serif">&nbsp;</font></b></td>
      <td><b><font size="2" face="Arial, Helvetica, sans-serif">&nbsp;</font></b></td>
      <td width="5%"><b>
        <div align="center"><font size="2" face="Arial, Helvetica, sans-serif">1</font></div>
        </b></td>
      <td width="5%"><b>
        <div align="center"><font size="2" face="Arial, Helvetica, sans-serif">2</font></div>
        </b></td>
      <td width="5%"><b>
        <div align="center"><font size="2" face="Arial, Helvetica, sans-serif">3</font></div>
        </b></td>
      <td width="5%"><b>
        <div align="center"><font size="2" face="Arial, Helvetica, sans-serif">4</font></div>
        </b></td>
      <td width="5%"><b>
        <div align="center"><font size="2" face="Arial, Helvetica, sans-serif">5</font></div>
        </b></td>
      <td width="5%"><b>
        <div align="center"><font size="2" face="Arial, Helvetica, sans-serif">6</font></div>
        </b></td>
    </tr>
    <tr bgcolor="white"> 
      <td><font size="2" face="Arial, Helvetica, sans-serif">6</font></td>
      <td height="35"><font size="2" face="Arial, Helvetica, sans-serif">I feel 
        good about my job.</font></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q6" value="1">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q6" value="2">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q6" value="3">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q6" value="4">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q6" value="5">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q6" value="6">
          </font></font></div></td>
    </tr>
    <tr bgcolor="lightgrey"> 
      <td><font size="2" face="Arial, Helvetica, sans-serif">7</font></td>
      <td height="35"><font size="2" face="Arial, Helvetica, sans-serif">My work 
        group uses teamwork to get things done.</font></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q7" value="1">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q7" value="2">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q7" value="3">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q7" value="4">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q7" value="5">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q7" value="6">
          </font></font></div></td>
    </tr>
    <tr bgcolor="white"> 
      <td><font size="2" face="Arial, Helvetica, sans-serif">8</font></td>
      <td height="35"><font size="2" face="Arial, Helvetica, sans-serif">An important 
        part of my job is to improve the way things are done.</font></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q8" value="1">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q8" value="2">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q8" value="3">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q8" value="4">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q8" value="5">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q8" value="6">
          </font></font></div></td>
    </tr>
    <tr bgcolor="lightgrey"> 
      <td><font size="2" face="Arial, Helvetica, sans-serif">9</font></td>
      <td height="35"><font size="2" face="Arial, Helvetica, sans-serif">The people 
        in my workgroup are trying to improve the quality of our products/services.</font></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q9" value="1">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q9" value="2">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q9" value="3">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q9" value="4">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2"> 
          </font><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q9" value="5">
          </font></font><font size="2"> </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q9" value="6">
          </font></font></div></td>
    </tr>
    <tr bgcolor="white"> 
      <td height="31"><font size="2" face="Arial, Helvetica, sans-serif">10</font></td>
      <td height="35"><font size="2" face="Arial, Helvetica, sans-serif">The people 
        in my workgroup share ideas and information to improve the quality of 
        our products/services. </font></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q10" value="1">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q10" value="2">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q10" value="3">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q10" value="4">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q10" value="5">
          </font></font></div></td>
      <td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="q10" value="6">
          </font></font></div></td>
    </tr>
  </table>
  <p align="center">
    <input type="submit" name="Submit2" value="Previous Page">
    <input type="submit" name="Submit" value="Next Page">
  </p>
  <p>&nbsp;</p>
</form>
</body>
</html>
Any help will be greatly appreciated!

Thanks in advance!

Posted: Thu Jan 04, 2007 3:51 am
by hrubos
I think that :

Code: Select all

<td><div align="center"><font face="Arial, Helvetica, sans-serif"><font size="2">
          <input type="radio" name="<?php $_SESSION['answer'][5] ?>" value="6"> 

.....................
Have success!!![/quote]

Posted: Thu Jan 04, 2007 4:42 am
by Sherrie
Thank you for your reply!

How would I assign the session variables then if the radio buttons have no name?

The reason that I'm asking is because at the end of the survey, the participants have the option of reviewing their response before submitting the survey. So if they want to change their answers, what happens then?

Thanks again!

Posted: Thu Jan 04, 2007 4:51 am
by Sherrie
I've also tried amending the code to what you suggested, and the values are still not displayed.

Would appreciate any help!

Posted: Thu Jan 04, 2007 10:36 am
by Kieran Huggins
Your issue is actually related to how radio buttons work as part of an HTML form. Read this:

http://www.w3schools.com/html/html_forms.asp

Posted: Thu Jan 04, 2007 2:48 pm
by Sherrie
Kieran,

Thank you for your response.

I am aware of how the radio buttons work, what I don't understand is how to program PHP to return the checked value of the radio button, thus retaining the response on the form.

Look forward to any help!

Posted: Thu Jan 04, 2007 3:00 pm
by Kieran Huggins
Radio buttons exist in groups - they're grouped by having the same name. Each one has (theoretically) a different value. Only one value is returned for the group, thus "name=value".

If you want a radio button to be checked by default, you use the "checked" attribute:

Code: Select all

<input type="radio" name="bob" value="1" />
<input type="radio" name="bob" value="2" checked="checked" />
<input type="radio" name="bob" value="3" />
<input type="radio" name="bob" value="4" />

Posted: Thu Jan 04, 2007 3:13 pm
by Sherrie
Thanks again for your prompt response, Kieran,

However, I'm afraid that doesn't answer my question. I have no problem assigning the values of the radio button to the session variable, the problem that I'm having is retrieving the values from the session variables.

For example, the answer to question 1 is stored in $_SESSION['answers'][0], the answer to question 2 is stored in $_SESSION['answers'][1], etc. I have tested my code, and this part works perfectly.

How would I need to code it so that when the user clicks to review the form, the radio button group named "q1" will get its value from $_SESSION['answers'][0] and the radio button group names "q2" will get its value from $_SESSION['answers'][1]?

Posted: Thu Jan 04, 2007 3:21 pm
by Kieran Huggins
If you want a radio button to be checked by default, you use the "checked" attribute:

Code: Select all

<input type="radio" name="bob" value="1" />
<input type="radio" name="bob" value="2" checked="checked" />
<input type="radio" name="bob" value="3" />
<input type="radio" name="bob" value="4" />
So when you're echoing out each radio button, check the value and add the "checked" if it's the same.

Posted: Thu Jan 04, 2007 3:21 pm
by feyd
When writing out the radio button compare the stored session data against the current radio button's value. When they match, output the "checked" attribute.

Posted: Thu Jan 04, 2007 3:31 pm
by Sherrie
feyd,

Thank you for your reply.

Could you please show me a few lines of code on how to do this so i can understand it better? I can understand the theory of it but I'm not sure where to start.

I would greatly appreciate it!

Posted: Thu Jan 04, 2007 3:34 pm
by Kieran Huggins
all yours feyd 8)

Posted: Thu Jan 04, 2007 3:37 pm
by feyd

Code: Select all

echo '<input type="radio" name="' . $question . '" value="' . $answer . '" ' . ($answer == $foo[$question] ? 'checked="checked" ' : '') . '/>';

Posted: Thu Jan 04, 2007 3:41 pm
by Sherrie
Kieran & feyd,

Thank you for your responses!

Where would I need to set the variables $question and $answer?

And I don't understand what $foo does. I'm sorry if I'm being tedious!

Posted: Fri Jan 05, 2007 2:09 am
by Sherrie
I have found the solution to the problem, and so I thought I'd post it in here just in case anyone else encounters the same problem that I did.

Here's the code that I used:

Code: Select all

<input type="radio" name="q1" value="1" <?php if($_SESSION['answer'][0] == '1') print 'checked="checked" '; ?>/> 
<input type="radio" name="q1" value="2" <?php if($_SESSION['answer'][0] == '2') print 'checked="checked" '; ?>/> 
<input type="radio" name="q1" value="3" <?php if($_SESSION['answer'][0] == '3') print 'checked="checked" '; ?>/> 
<input type="radio" name="q1" value="4" <?php if($_SESSION['answer'][0] == '4') print 'checked="checked" '; ?>/> 
<input type="radio" name="q1" value="5" <?php if($_SESSION['answer'][0] == '5') print 'checked="checked" '; ?>/> 
<input type="radio" name="q1" value="6" <?php if($_SESSION['answer'][0] == '6') print 'checked="checked" '; ?>/>
Thanks for all your help!