Page 1 of 1
session variables for check boxes/multiple select box
Posted: Wed Jun 22, 2005 3:54 pm
by alliou
Hi,
I'm new to PHP, and I've run into a snag. I'm trying to display a list of foreign languages available to users in either a list of check boxes or by use of a multiple select box (haven't figured out how to get either one to work.) I'm getting this list from a database. The value of the form object will be the language id. What is displayed to the user will be the actual language. For instance, if I were using checkboxes, here's what it would look like on registration.php:
Code: Select all
<?php
while ($row = $result->fetchRow (DB_FETCHMODE_ASSOC))
{
?>
<input type=checkbox name="Language[]" value=<?php echo $row['LanguageID'];?>><?php echo $row['ForeignLanguage'];
}
Now, this page is submitting to a page called doinsert.php. On doinsert.php, I am setting session variables and checking for errors. Here's what I can't figure out: how do I capture each item the user has checked and store each individual answer in its own session variable to be used later on? Does this make sense? Please let me know if you need more information, or if I'm not being clear. Thanks!
Posted: Wed Jun 22, 2005 4:55 pm
by shiznatix
Code: Select all
foreach ($_POST['Language'] as $key => $val)
{
$_SESSION['lang'][] = $val;
}
then on the menu thing if you want those to be selected you can do
Code: Select all
<?
while ($row = $result->fetchRow (DB_FETCHMODE_ASSOC))
{
?>
<input type=checkbox name="Language[]" value=<?php echo $row['LanguageID']; echo (isset($_SESSION['lang']['LanguageID']) ? 'CHECKED' : ''); ?>><?php echo $row['ForeignLanguage'];
}
Posted: Wed Jun 22, 2005 5:55 pm
by alliou
I put the second piece of code in my registration.php (where the form is) and the first piece of code in doinsert.php (where my processing occurs.) When I check some boxes and hit submit, it comes back with nothing checked, and when I print out what the session variable is, it says "The value of $_SESSION['lang'] is 'Array' ".
Is there something I'm doing wrong?
Posted: Wed Jun 22, 2005 6:14 pm
by shiznatix
sorry i goofed up in the code a little. make the one part this
Code: Select all
<?
while ($row = $result->fetchRow (DB_FETCHMODE_ASSOC))
{
?>
<input type=checkbox name="Language[]" value=<?php echo $row['LanguageID']; echo (isset($_SESSION['lang'][$row['LanguageID']]) ? 'CHECKED' : ''); ?>><?php echo $row['ForeignLanguage'];
}
all work and no sleep makes shiznatix make mistakes
Posted: Thu Jun 23, 2005 10:57 am
by alliou
ok, so I'm getting closer

I think I know what's going on, I just don't have a grasp on arrays enough to troubleshoot it. When I just echo
Code: Select all
foreach ($_POST['Language'] as $key => $val){
$_SESSION['lang'][$row['LanguageID']] = $val;
echo $_SESSION['lang'][$row['LanguageID']]."<br>";
}
to the screen on doinsert.php, it grabs everything and displays it perfectly. For instance, if I pick the checkboxes w/ values of 2 and 4, it will display 2<br><br>4 on the screen.
It's when I come back to registration.php (the page I have to display the checks on) that it loses something. The session variables are right, it just somehow isn't putting it all together to make the box checked. It's like I'm missing something in this code:
Code: Select all
<input type=checkbox name="Language[]" value="<?php echo $row['LanguageID'];?>"<?php echo (isset($_SESSION['lang'][]) ? 'CHECKED' : ''); ?>><?php echo $row['ForeignLanguage'];
NOTE: I've also tried this:
Code: Select all
<input type=checkbox name="Language[]" value="<?php echo $row['LanguageID'];?>"<?php echo (isset($_SESSION['lang'][$row['LanguageID']]) ? 'CHECKED' : ''); ?>><?php echo $row['ForeignLanguage'];
which yields the same result...no boxes checked.