[thelist] Php array into session via form checkboxes

Marek Kilimajer kilimajer at webglobe.sk
Fri Oct 17 19:20:23 CDT 2003


Barney wrote:
> Hi all,
> 
> Long term lurker (and fumbling php coder) dropping in for some much needed help. I'm stumped by the following:
> 
> I am trying to build an online questionnaire in php which posts to itself via $php_self

You mean $PHP_SELF. Are you aware that ariables are case sensitive?

> as each question is answered and then proceeds to the next question. The data collected is stored in a session and at the end will be written to MySql. For this particular question the selections made are stored as a serialized array variable in a session and then unserialized on the next page to enable the array elements to be accessed from the session. This is because further questions are then asked about each selection made. The question/layout and lack of error checking etc etc is irrelevant - i'm just trying to get it to work first!
> 
> The big problem I have is that I've got the array data to be accessable after the page is posted BUT instead of the selections made in the checkboxes being passed as variables, all the values are being posted. 

Unchecked checkboxes are not posted.

Try print_r($_POST) in your action script.

 > I simply can't work out why this is the case as I would expect 
unselected checkboxes to pass a blank value to the array. Any help or 
pointers in the right direction would be much appreciated as I'm 
completely stumped and have spent a long time on the web trying to find 
a solution.
> 
> here is the code:
> 
> <?php
> session_start();
> 
> /* Q4 variables */
> $bands=array();
> session_register('serbands');
> session_register('bandsother');
> ?>
> <html>
> <head>
> <title>Online questionnaire</title>
> </head>
> <body>
> <?php
> if(!IsSet($stage))
>  {
>  ?>    
>  <p>Which of the following bands would be in your top three choices for the best band in the world?<br>
>   <form method="post" action="<?php print("$php_self");?>">
>    <input type="checkbox" name="bands" value=<?php $bands[]="The Undertones";?>> The Undertones<br> 
>    <input type="checkbox" name="bands" value="<?php $bands[]="Hendrix";?>"> Hendrix<br>  
>    <input type="checkbox" name="bands" value="<?php $bands[]="Spacemen 3";?>"> Spacemen 3<br>
>    <input type="checkbox" name="bands" value="<?php $bands[]="Mudhoney";?>"> Mudhoney<br>
>    <input type="checkbox" name="bands" value="<?php $bands[]="Dylan";?>"> Dylan<br>
>    <input type="checkbox" name="bands" value="<?php $bands[]="Johnny Cash";?>"> Johnny Cash<br>
>    <input type="checkbox" name="bands" value="<?php $bands[]="Joy Division";?>"> Joy Division<br>
>    <input type="checkbox" name="bands" value="<?php $bands[]="Ween";?>"> Ween<br> 
>    <input type="checkbox" name="bands" value="<?php $bands[]="Janis Joplin";?>"> Janis Joplin<br>  
>    <input type="checkbox" name="bands" value="<?php $bands[]="Elvis";?>"> Elvis<br>
>    <input type="checkbox" name="bands" value="<?php $bands[]="The Pixies";?>"> The Pixies<br>
>    <input type="checkbox" name="bands" value="<?php $bands[]="The Orb";?>"> The Orb<br>
>    <input type="checkbox" name="bands" value="<?php $bands[]="The White Stripes";?>"> The White Stripes<br>
>    <input type="checkbox" name="bands" value="<?php $bands[]="other";?>"> Other 
>    <input type="text" name="bandsother" size="20"><br>
>    <?php $serbands = array (serialize($bands));?>
>    <input type="hidden" name="stage" value="5">
>    <input type="submit" value="next">
>   </form>
>  </p>
> <?php
>  }
> elseif($stage==5)
>  { 
>    $session_data = unserialize ($serbands[0]);
>    if (!is_array ($session_data)) {
>     // something went wrong, initialize to empty array
>     $session_data = array();
>    }
>   $howmany = count($session_data);
>   print ("The array contains $howmany elements.<br>\n");
> 
> 
>   for ($n=0; $n<count($session_data); $n++){
>    $line=each($session_data);
>    print ("$line[key]'s value is $line[value].<p>\n");
>  }
> }
> ;?>
> </body>
> </html>
> 
> thanks,
> 
> Griff



More information about the thelist mailing list