/    Sign up×
Community /Pin to ProfileBookmark

select box options

I have these two dynamic select boxes that exchange options using javascript. They’re in a form which sends the info to a php script. My problem is pretty simple, I just need to be able to get a list of the options that are in the select box. However the only thing I know how to do is get the selected option. Is there a way to accomplish this with php? if not, how could else could I do this? I need to store the values of the options each into seperate rows in a MySQL table.

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Feb 11.2007 — Is one <select> dependent upon the other?
Copy linkTweet thisAlerts:
@michael879authorFeb 11.2007 — how do u mean. I want to be able to tell (in the php script) which select has which options. They both share some pool of options.
Copy linkTweet thisAlerts:
@NightShift58Feb 11.2007 — I mean: Do the options in the second <select> change dynamically depending on which option was chosen in the first <select>?
Copy linkTweet thisAlerts:
@michael879authorFeb 12.2007 — kindof. I already described how it works didnt I? I have two select boxes, and two buttons in between. the <- button moves options from the right box to the left, and the -> button moves options the other direction. The only place that selecting comes into play is determining which option to move.
Copy linkTweet thisAlerts:
@NightShift58Feb 12.2007 — The reason I asked is because it wasn't clear to me based on your original description of the problem. I didn't mean to upset you. Sorry about that.
Copy linkTweet thisAlerts:
@michael879authorFeb 12.2007 — na my mistake, I posted a pretty clear description of it in the javascript section for a different problem. I guess I got the two posts confused. Do you have any advice?
Copy linkTweet thisAlerts:
@michael879authorFeb 12.2007 — ^bump, I still have no good way to solve this.

I did come up with 1 solution which kinda sucks. I could make a javascript function that gets called onsubmit. the function would stick all of the options into a string and then that would be sent to the php script via get. however if I were to do that I dont see how I could send the rest of the form data...
Copy linkTweet thisAlerts:
@michael879authorFeb 13.2007 — ok I think your right in that both posts have the same solution. However, Im not really sure what you did there. I see the line:
<i>
</i> &lt;select name="model" value="&lt;?php echo htmlspecialchars(stripslashes($md)); ?&gt;"&gt;
&lt;option&gt;Please Select A Model&lt;/option&gt;
&lt;/SELECT&gt;

where $md = htmlentities($_POST['model'])

what exactly is stored in $_POST['model']?? For me, all that gets stored in the select post variable is the selected option. however somehow you got all of the options included...
Copy linkTweet thisAlerts:
@michael879authorFeb 13.2007 — ok nevermind, I see what you did. I want a way to get ALL of the option values into my php script. Not just the selected ones. Im not doing this so the user doesnt have to reselect stuff, Im doing it so I can enter the options into my database.
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — Then it's much simpler. Simply select the applicable data from your table and echo each option within a PHP loop.[code=php]<?
$sql = "SELECT * FROM mytable ORDER BY myoptions";
$qry = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());

echo "<select name='optiontest'>";
WHILE ($r = mysql_fetch_array($qry)) :
echo "<option value='" . $r['optionvalue'] . ">" . $r['optionlabel'];
ENDWHILE;
echo "</select>";
?>[/code]
Copy linkTweet thisAlerts:
@michael879authorFeb 14.2007 — lol I wish that were my problem... no I know how to do that. I want to do the exact opposite. I have a dynamic select box where the options are changed depending on user input. I want to be able to find out which options were in the box when the user submitted in the form, and then store those options in a new row.
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — Ok.[code=php]<?php
$option1 = $_POST['select1'];
$option2 = $_POST['select2'];

$sql = "INSERT INTO mytable ";
$sql .= " SET fld_option1 = '$option1', ";
$sql .= " fld_option2 = '$option2' ";

mysql_query(etc...)
?>[/code]
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — I didn't really think the problem could be that simple... So I re-read the whole thread. I would do it this way:[code=php]<?php
$arrOPTIONS = array();
$arrOPTIONS[] = "option1";
$arrOPTIONS[] = "option2";
$arrOPTIONS[] = "option3";
$arrOPTIONS[] = "option4";
...
...
...
echo "<FORM name='myform' etc... >";
FOREACH ($arrOPTIONS as $key => $val) :
echo "<input type='hidden' name='optlist[$key]' value='$val'>";
ENDFOREACH;

echo "<select name='myoptions'>";
FOREACH ($arrOPTIONS as $key => $val) :
echo "<option value='$val'>$val</option>";
ENDFOREACH;
?>[/code]
By storing the individual options under a single hidden variable name with [], these values become available to you in the next script as an array under $_POST. In the case of the example posted, it would be called $_POST['optlist'] which evaluates to an array, through which you can loop.

An alternative would be to pass serialized values as GET or POST.

Of course, you can go with a session variable but POST is easier.
Copy linkTweet thisAlerts:
@michael879authorFeb 14.2007 — I didn't really think the problem could be that simple...[/QUOTE]
lol you finally get it :-p

ok I think thatll work. Thanks a lot. just lemme get this straight tho, your saying I should make a hidden variable for each option and then whenever I move an option to another box, change its hidden variable?

also, changing the subject a little, is your array syntax correct? does blah[] = "blah" create a new element in the blah array with value "blah"?
Copy linkTweet thisAlerts:
@NightShift58Feb 15.2007 — lol you finally get it :-p

ok I think thatll work. Thanks a lot. just lemme get this straight tho, your saying I should make a hidden variable for each option and then whenever I move an option to another box, change its hidden variable?[/quote]
When you get to the next page, the options are all in an array within the $_POST array and are no longer hidden. You can use them and forget about them.also, changing the subject a little, is your array syntax correct? does blah[] = "blah" create a new element in the blah array with value "blah"?[/quote]Yes, that's exactly what it does. The empty [] basically means "append a new one".
Copy linkTweet thisAlerts:
@michael879authorFeb 15.2007 — ah Ive been using array_push, I didnt even know about that thanks.

so yea that solution will definately work, thanks again for all the help.
Copy linkTweet thisAlerts:
@NightShift58Feb 15.2007 — You're welcome!
×

Success!

Help @michael879 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...