Page 2 of 2
Re: Storing HTML Multiple Select values
Posted: Fri Feb 15, 2008 3:22 pm
by RobertGonzalez
Alright, lets help ourselves out a little bit.
In both forms, do a var_dump($_POST) to see what PHP is seeing.
Re: Storing HTML Multiple Select values
Posted: Fri Feb 15, 2008 9:19 pm
by the9ulaire
That's what's so weird to me: it gives me the same as the other form. But when I post the information to another page it changes...
Here's what it did on both form pages:
array(2) { [0]=> string(1) "1" [1]=> string(1) "2" }
:-/ I don't get it.
Re: Storing HTML Multiple Select values
Posted: Sat Feb 16, 2008 12:55 am
by RobertGonzalez
All I can suggest is to check the markup to make sure that the form is treating the multiple as array. PHP does not choose what is and is not an array, the form does. I'd start there.
Re: Storing HTML Multiple Select values
Posted: Mon Feb 18, 2008 11:03 am
by the9ulaire
So I simply just chose another route to do this. It's not near as efficient. :-/
I did see elsewhere online that it was said that you can't $_POST arrays.... Not sure if it's true or not but I know my code was perfect and wouldn't post an array....
Re: Storing HTML Multiple Select values
Posted: Mon Feb 18, 2008 11:08 am
by RobertGonzalez
Don't give up yet. What you want to do can be easily accomplished.
Start with the basics... your markup. Post the form and check the $_POST array on the resulting page using PHP. If things are going wobbly make a simple form and repeat this process of checking while adding a single field on each test.
This is development bro. It sometimes goes this way. Keep your head and keep plugging along. It gets easier, but you have to work through the stumbling blocks that come at you early in your development.

Re: Storing HTML Multiple Select values
Posted: Fri Feb 22, 2008 2:08 pm
by the9ulaire
I figured out how to store the values! Suddenly it just came to me how to make it work. I realize now I was making a very, very basic mistake.
Now, I'm just wondering how to use those values to check checkboxes.
Here's my situation:
I use my form with four checkboxes to store data into a table with two fields (activity_id, season). The activity_id corresponds with the id in another table. Now when I want to edit the seasons that are available, I want the checkboxes to be checked in the form if they're in the database.
How do I go about making the checkboxes be checked in the form if they appear in the season column?
Does that even make any sense?
Re: Storing HTML Multiple Select values
Posted: Fri Feb 22, 2008 2:19 pm
by RobertGonzalez
Generally speaking I store any text that can appear on screen in my database. That means that I would have all of the list options in a table somewhere and would build the list from that table data. That means that you have an array (generally speaking) of ID's and Text Strings for the drop down.
As you loop that dataset you can easily check to see if the ID at that loop point is a member of an array of, say, user selected IDs from that same table.
Code: Select all
<?php
// I will leave you getting the users selections from the table and
// I will assume they are in a one-dimensional array called $user_selects
$sql = "SELECT `option_id`, `option_value` FROM `options_table` ORDER BY `option_value` ASC;";
if (! $result = mysql_query($sql)) {
die('Could not execute the query (' . $sql . ') as entered because: ' . mysql_error()); // This should not be used in production
}
// If it is a straight select ditch the multiple bit
echo '<select name="myselect" id="myselect" class="select-multi" multiple="multiple">';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<option value="' . $row['option_id'] . '"';
if (in_array($row['option_id'], $user_selects)) {
echo ' selected="selected"';
}
echo '>' . $row['option_value'] . '</option>';
}
echo '</select>';
?>
Does that help a bit?
Re: Storing HTML Multiple Select values
Posted: Sun Feb 24, 2008 10:17 am
by the9ulaire
Yes, yes! That makes sense! I can't believe I didn't realize that before...I've used that technique before.
Thanks!