I am very new to php. Can someone please help?

I would like to send an entire record from a form to another program (selection.php) using a multidimesional array(??). However, I don't know how to declare, populate or access data from the multidimensional array.

What I would like to see if I select chips uncheck, popcorn - check:

Chips

Popcorn N

Thanks in advance for you help,
lopeza

form.php
<form action="selection.php" method="POST">
<table>
<tr>
<td>
<select name="snacks[]">
<option value="-1">---Select a snack---</option>
<option value="chips">Chips</option>
<option value="popcorn">Popcorn</option>
<option value="pretzels">Pretzels</option>
</select>
</td>
<td align="center"><INPUT TYPE="checkbox" NAME="snacks[][]" VALUE="N" UNCHECKED ></td>
</tr>

<tr>
<td>
<select name="snacks[]">
<option value="-1">---Select a snack---</option>
<option value="chips">Chips</option>
<option value="popcorn">Popcorn</option>
<option value="pretzels">Pretzels</option>
</select>
</td>
<td align="center"><INPUT TYPE="checkbox" NAME="snacks[][]" VALUE="N" UNCHECKED ></td>
</tr>

<tr>
<td ALIGN=center><INPUT TYPE="submit" NAME="p_action" VALUE="Query"></TD>
</tr>
</table>
</form>

selection.php
<?php

$selected = $_POST['snacks'];

foreach( $selected as $snack=>$checked )
{
echo ' <li><b>'.$snack."</b>.<br />\n";
echo " <em>Checked</em>: </li>\n";
echo " <ul>\n";
asort( $checked ); // sorts the list of songs
foreach( $checked as $check )
{
echo ' <li>'.$check.".</li>\n";
}
echo " </ul>\n";
}
?>

    11 days later

    For what you're doing you don't need to use an array you can just use three different form variables.

    The best way to do it if you want people to be able to select multiple snacks is to use a multi-select. This will allow people to click on as many items as they want and they will be put into an array in this manner:

    snacks[0] = value;
    snacks[1] = value;

    where value correlates to <option value=value>.

    To declare a multi select use <select name=... multiple> where the "..." are the other parts of a select declaration.

    For "unchecked" you could use, as the name of the form variable, something like snacks[unchecked].

    Some other things of note:

    $selected = $_POST['snacks'];

    foreach( $selected as $snack=>$checked )

    can just be:

    foreach( $_POST['snacks'] as $snack=>$checked ).

    Also instead of echo, use print, its much nicer. Though that's just a personal preference.

      Write a Reply...