Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Displaying radio button values from session variables

    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:

    PHP Code:
    <?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!

  • #2
    New Coder
    Join Date
    Oct 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do your own homework.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Meh, I couldn't care less if its your homework or not, I'd expect the same help on mine :P
    The problem is that you are not stating what the values of your populated fields are in your html code. Unfortunatly, its messy with embeded HTML, but here's how its done:
    Code:
    <input type="radio" name="q6" value="1" <?php echo ($_SESSION['q6'] == 1) ? 'checked="checked"' : '';?> />
    Simple as that. Problem is, you need to do this with ALL of your input fields (100+ questions). A good solution would be to look into using eval to allow you parsable php within your string, or use an output method from php for your html. I'd recommend the eval, for several reasons.
    Next, your $_SESSION settings. First, don't do all that work, do a check for submission using an or:
    PHP Code:
    <?php
    session_start
    ();
    if (isset(
    $_POST['Submit']) || 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']; 
        
        
    $location = isset($_POST['Submit']) ? 'page3.php' 'page1.php';
        
    header("Location: " $location);  
    }
    Note a couple of things.
    1. Location header *should* be an absolute URI not a relative. This is because HTTP/1.1 requires an absolute URI.
    2. Second, you don't have a way to pass your session in with this method. If the user is not using cookies, they're session will be abandoned on each page load. Fix this by appending 'SID' to the end of the location header location as well as any possible links. Another method is to retrieve it from a post value passed through your form itself.
    3. Third, not a php thing, but you are half way to an xHTML compliant web page. You should consider dropping your use of the <table> tag and opting for a positional control of your div values as entry fields. HINT: Float all of your values left within a div to make it appear like a table. Requires only one width stated (relative or absolute).
    4. And finally, don't ever trust user input - ever. Regardless of this being nothing more than an input for radio buttons, you should check that the datatype matches your expected data type. You can help yourself on this if you convert all of your radio boxes into arrays: q[1], q[2], q[3] for example. This will allow you to seek each value on $_POST['q'] for validation.

    Hope this helps you a bit
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New Coder
    Join Date
    Jan 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Shaffer,

    If I could find the information, I wouldn't be posting in this forum and be asking for help, doing nothing while my deadline draws closer!

    Fou-Lu,

    Thank you for your reply! Have tried it and it works! Cheers!

    Seeing as I put in the value of the radio button in my code, does that mean I can't trust myself too? Think I'll forego the validation part for now, but thanks for the tip! Will keep that in mind for future reference

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts

    As with validation - never trust ANY user input. What I mean, is that just because the html states that the values are checkboxes/radiobuttons, nothing is stopping a user from maliciously sending their own data in place of these fields with an altered form script. A simple check would be sufficient, take the lowest number (1) and the highest number (6 from the looks of it), and simply check prior to the assignment to the session that the question is an integer between 1 and 6 inclusive. Simple check yeah?
    This of course, only really needs to be done if something is manipulating the data after, specifically with a database for instance. This is called an SQL-Injection.
    Note as well, that similar techniques are used for other systems as well, such as the filesystem. These are obviously bad. And the worst part, it could technically all stem off of a simple html script, where the value of radio button was changed to ' or uid like'%admin%'; --.
    Hope that helps
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •