Alright, lets help ourselves out a little bit.
In both forms, do a var_dump($_POST) to see what PHP is seeing.
Storing HTML Multiple Select values
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am
Re: Storing HTML Multiple Select values
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.
Here's what it did on both form pages:
array(2) { [0]=> string(1) "1" [1]=> string(1) "2" }
:-/ I don't get it.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Storing HTML Multiple Select values
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.
-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am
Re: Storing HTML Multiple Select values
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....
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....
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Storing HTML Multiple Select values
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.
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.
-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am
Re: Storing HTML Multiple Select values
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?
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?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Storing HTML Multiple Select values
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.
Does that help a bit?
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>';
?>-
the9ulaire
- Forum Commoner
- Posts: 74
- Joined: Mon Jun 11, 2007 11:31 am
Re: Storing HTML Multiple Select values
Yes, yes! That makes sense! I can't believe I didn't realize that before...I've used that technique before.
Thanks!
Thanks!