...

View Full Version : Change [Select 2] options when [Select 1] option is changed



topdown.me
11-10-2012, 06:54 PM
Hello.
I have 2 selects in a form:
Select 1
-food
-drinks
Select 2
-hamburger
-cheetos
-orange juice
-cola

I want to do that when I choose "FOOD" in select 1, I get the options: Hamburger and cheetos, and when I pick "DRINKS" in select 1,
select 2 will change to orngae juice and cola.

How I can do that? (need to work in all browsers)

tangoforce
11-10-2012, 08:32 PM
Select 1
-food
-drinks
Select 2
-hamburger
-cheetos
-orange juice
-cola


So if a user selects food in select one, it takes hamburger and cheetos FROM select 2? What happens if the user changes their mind and wants to select drinks instead? - they won't be able to because you've just preset it with food items.

As per your original question, it's really a client side issue (javascript). You could do it using ajax (javascript using http to a php script) but thats overkill for this situation.




I want to do that when I choose "FOOD" in select 1, I get the options: Hamburger and cheetos, and when I pick "DRINKS" in select 1,
select 2 will change to orngae juice and cola.


Now I'm slightly confused here. The way you've written it says this to me:
When I select food in select 1, select 1 changes to contain further choices.
When I select drinks in select 1 select 2 changes to contain further choices.

Now I'm having doubts and thinking you also meant select 2 changes for food choices and perhaps you didn't word it very well. Either way I'm confused.

topdown.me
11-10-2012, 08:55 PM
So if a user selects food in select one, it takes hamburger and cheetos FROM select 2? What happens if the user changes their mind and wants to select drinks instead? - they won't be able to because you've just preset it with food items.

As per your original question, it's really a client side issue (javascript). You could do it using ajax (javascript using http to a php script) but thats overkill for this situation.




Now I'm slightly confused here. The way you've written it says this to me:
When I select food in select 1, select 1 changes to contain further choices.
When I select drinks in select 1 select 2 changes to contain further choices.

Now I'm having doubts and thinking you also meant select 2 changes for food choices and perhaps you didn't word it very well. Either way I'm confused.


I'll say it simple:

When I pick FOOD option, I get list of foods I can choose!
When I pick DRINK option, I get list of drinks I can choose!

thats it...

tangoforce
11-10-2012, 10:42 PM
Ok thats a bit better. Your previous description over complicated and confused the issue.

You'll want to use javascript then for that, put your food and drink items into javascript arrays and when selecting from select one, load the appropriate array items into the second select.

As for code, this is a php forum. You need to head off to the javascript forum for help.

minder
11-10-2012, 11:15 PM
You'll want to use javascript then for that, .....

That's not correct because the op said


(need to work in all browsers)

If you use javascript, your solution won't work in browsers that do not support javascript or have it disabled for some reason.

The only way I can think of to make it work in all browsers is to do it server side and so the op is on the right track posting in the PHP forum.

You can use javascript if you must not have a page refresh after a food type is selected in the 1st select list.

tangoforce
11-10-2012, 11:47 PM
Well in that case the op might as well have a page 1 => page 2 type setup then. Any form that submits, changes, submits again is time consuming and frankly an annoyance to many people. For that reason it's better to have a wizard style instead.

Also what you're forgetting is that some people want it to work 'in all browsers' but don't always understand the limitations they are insisting on. You have no idea if the op is aware that javascript does / does not work on all browsers and likewise the op has not specified if they are happy to submit the page for every change.

Finally, if the user selects an option from select 1 and the second select needs to then change (as mentioned by the op) then this would rely on javascript to trigger an event anyway because the op has not mentioned clicking an 'update' button in their logic. By that, you could assume that the op is reliant on javascript being enabled as they mentioned no form submissions or buttons to update the users choice.

I'm wrong?

minder
11-10-2012, 11:59 PM
Also what you're forgetting is that some people want it to work 'in all browsers' but don't always understand the limitations they are insisting on.

And you seem to be forgetting that some do understand the limitations :thumbsup:

If you want it to work in all browsers you could start with something like this example. Otherwise, if you do it with javascript you have to accept that it will not work in all browsers. If it not working in all browsers is not an issue then by all means use javascript.

All the data entry and processing is done within 1 php driven page.


<?php
$foodOpts = array('hamburger', 'cheetos');
$drinkOpts = array('coke', 'orange juice');
if(isset($_POST['btnSubmit'])){
unset($_POST['btnSubmit']);
// do your validation to match food type selection with selected food and data processing here
echo 'form has been submitted';
die();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
</head>
<body>
<form action="#" method="post">
<select name="selFoodType">
<option value="food" <?php if(isset($_POST['selFoodType']) && $_POST['selFoodType'] == 'food'){echo 'selected="selected"';} ?> >Food</option>
<option value="drink" <?php if(isset($_POST['selFoodType']) && $_POST['selFoodType'] == 'drink'){echo 'selected="selected"';} ?>>Drink</option>
</select>
<input type="submit" name="btnGetOptions" value="Get food type options" />
<?php
if (isset($_POST['btnGetOptions'])) {
unset($_POST['btnGetOptions']);
$foodType = $_POST['selFoodType'];
switch ($foodType) {
case 'food':
$opts = $foodOpts;
break;
case 'drink':
$opts = $drinkOpts;
break;
}
echo '<select name="selFoodOpt">';
foreach ($opts as $val) {
echo '<option value="' . $val . '">' . $val . '</option>';
}
echo '</select>';
}
?>
<input type="submit" name="btnSubmit" value="Submit" />
</form>
</body>
</html>

A better way would be to have a 2D array linking the food types and food options and create the 2 select lists dynamically in php. But without seeing any code from the op I'm not going jump too far ahead atm.

tangoforce
11-11-2012, 12:14 AM
And you seem to be forgetting that some do understand the limitations :thumbsup:


<?php
if(isset($_POST['btnSubmit'])){



Are you serious? :eek: - See the links in my signature as to why this technique is not recommended (due to the technical limitations of IEs bug).





unset($_POST['btnSubmit']);



What on earth is that for? :confused:

Also you're showing the op that they should be mixing html and php. We always discourage that around here and normally only recommend keeping php and html seperate.

minder
11-11-2012, 12:27 AM
Are you serious? :eek: -

I don't have any problems with it. It works fine in the latest IE and the versions I need to support. If it doesn't work in a version you need to support then do something else.




What on earth is that for? :confused:


Force of habit because in some cases it is necessary.





Also you're showing the op that they should be mixing html and php. We always discourage that around here and normally only recommend keeping php and html seperate.

I have never had any problems mixing php code within html and it's my and a lot of other peoples' way of doing things. There is no rule that says it must not be done. If you have a preference to not mix the 2 then that's just your and other peoples choice.

Why don't you post a php only solution to what the op wants separating the php from the html as you prefer to do?

The op can now post their own code if they need more specific help.

tangoforce
11-11-2012, 12:47 AM
I don't have any problems with it. It works fine in the latest IE and the versions I need to support. If it doesn't work in a version you need to support then do something else.

Last I heard the bug was still alive and kicking in IE9 and it definitely is in IE8. If you have no problems with it then its either because you don't understand the bug or don't care about it. If you only support the latest browser then you'll have a future problem because you're clearly not catering for or even thinking about user difficulties :rolleyes: You need to understand and cater for these things.

As for posting code, no need. The op hasn't been back to say if they want the javascript or php route. Besides, your code is apparently bug free using the submit button so I'm sure they'll be just fine with your code :thumbsup:

minder
11-11-2012, 12:56 AM
If you have no problems with it then its either because you don't understand the bug or don't care about it.

I actually prefer users to have to click a submit button because they then can't use the excuse "I accidentally pressed Enter and didn't realise the form data would be submitted blah blah blah....".

I'm retired now and so am not beholden to other peoples' whims and preferences and baloney in the way they do things or think the way things should be done. I can do whatever I like in the way I think is best :)

tangoforce
11-11-2012, 01:11 AM
Thats fair enough but you shouldn't go around advising others to be checking for the submit button if you know the risks but choose to put up with it. Others don't know that risk and consequence and won't know how to deal with it (hence my sig).

I've heard of people loosing orders over this bug.. you wouldn't believe how many times it continues to cause problems for IE users. It certainly isn't baloney either.

minder
11-11-2012, 01:24 AM
I've heard of people loosing orders over this bug..

That may be true, but it's also true that people have accidentally placed orders because they "accidentally" pressed the Enter key without realising their order would be submitted and then have to go through the hassle to try to cancel it. So I suspect those people would see the Enter key submitting the form as being the bug and not the other way around.

tangoforce
11-11-2012, 02:17 AM
That may be true, but it's also true that people have accidentally placed orders because they "accidentally" pressed the Enter key without realising their order would be submitted and then have to go through the hassle to try to cancel it. So I suspect those people would see the Enter key submitting the form as being the bug and not the other way around.

Nearly every order page I've ever known of has a confirmation page. If you're saying you've known of order entry pages that actually place orders with no confirmation page before actually placing the order then they are simply badly designed sites.

As for the enter key working in a form, thats common knowledge so not exactly a bug. The bug is that IE doesn't actually use it while every other browser does.

minder
11-11-2012, 02:24 AM
Even the confirmation page can be "accidentally" submitted by users by pressing the Enter key without realising they will be submittimg the order when they could be thinking that the Enter key could be the same as clicking teh Cancel button.

You'll be amazed at what computer illiterate people will assume when clicking things on pages :eek:

Anyway, the op has x-posted inanother thread.

http://www.codingforums.com/showthread.php?p=1290785#post1290785

topdown.me
11-11-2012, 08:02 AM
Even the confirmation page can be "accidentally" submitted by users by pressing the Enter key without realising they will be submittimg the order when they could be thinking that the Enter key could be the same as clicking teh Cancel button.

You'll be amazed at what computer illiterate people will assume when clicking things on pages :eek:

Anyway, the op has x-posted inanother thread.

http://www.codingforums.com/showthread.php?p=1290785#post1290785

I had no idea where to post this, because I has to be combined. I need to loop all the "foods" and "drinks" from the database using PHP.

Anyway, someone gave ma a link that I think might be good - http://www.javascriptkit.com/script/script2/triplecombo.shtml

Edit: no, its not good.. I cant loop my options.. \:
Read more at http://www.codingforums.com/showthread.php?p=1290895#eoOMTxOwmzYmoUkx.99

btw, I have no problem using Javascript. It fits all browsers. if the user dont allow js, its his problem :D

Philip M
11-11-2012, 09:29 AM
I'm retired now and so am not beholden to other peoples' whims and preferences and baloney in the way they do things or think the way things should be done. I can do whatever I like in the way I think is best :)

I have the horrible feeling that we are back with bullant and his many doppelgangers. :mad:

minder
11-11-2012, 12:08 PM
btw, I have no problem using Javascript. It fits all browsers. if the user dont allow js, its his problem :D

In that case you may as well use javascript.

Like I said earlier I would use a 2D array to link the food types to the food options to create the the selects dynamically. You can then also loop through the 2D array to do whatever you need to do.

You can use mySQL to get the data out of the database and then loop through the result set using php to write the code to create your js 2D array. Then use plain ol' js to do the rest.

tangoforce
11-11-2012, 01:02 PM
I have the horrible feeling that we are back with bullant and his many doppelgangers. :mad:

Nothing would surprise me. Most programmers like to actively limit the bugs in their code but this guy seems to be absolutely careless about the most simple of bugs while making up rather feeble excuses for it such as users hitting enter on a confirmation page :rolleyes:

minder
11-12-2012, 08:49 AM
Most programmers like to actively limit the bugs in their code but this guy seems to be absolutely careless about the most simple of bugs

One man's "bug" could be another man's "feature". Does MS actually acknowledge it as a know "bug", since ithe functionality has been around for so long in IE?

Maybe MS see it as a feature as well :) for the reasons I posted before.

Does it appear in the W3C specs anywhere. Oh but wait, the specs are really nothing more than a non-binding recommendation. So who actually has the authority to enforce that a feature must be seen as a bug by everyone? Answer - NO-ONE !!!

So if you want to call it a bug then that is meerly just your and maybe other peoples' opinion and nothing nore than that. I will continue to see it as a feature as I described earlier :) until someone with actual authority says I cannot ;)

tangoforce
11-12-2012, 09:46 AM
You call one browser being different to the rest a 'feature' and refuse to handle the way all the other browsers deal with it.

I don't know if you're just stubborn or careless or both but most coders will write code that handles all situations. You seem to want to handle only the very basics. In my view that makes you an amateur especially when you start defending a well known bug and calling it a feature and claiming that you know of plenty of cases where users have hit enter on a confirmation page.

I bet you couldn't actually point me to one article online where a webmaster has had that problem.

minder
11-12-2012, 10:00 AM
I couldn'r care less what you think. Instead of wasting other peoples' time trying to get me to change the way I do things why not go tell MS about how you think they should do things and see what their reply is and whether they will take you seriously or not.

If MS can do what they like, then so can I :)

tangoforce
11-12-2012, 10:05 AM
Instead of wasting other peoples' time

You're the one who dragged this thread back up after a few days not me :thumbsup:

minder
11-12-2012, 10:17 AM
You're the one who dragged this thread back up after a few days

that's correct - with a php only solution since op said it need to work in all browsers. Your the one whinging about the code in the solution. I couldn't care less how you code but like I said, I am retired now and can do things as I see best since I don't have to put up with how other people think I should do things anymore :). If MS can do what they like, then so can I.

So why not go whinge to MS and see if they won't laugh at your whinging as well.

Best of luck :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum