Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    52
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Change [Select 2] options when [Select 1] option is changed

    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)
    Need domain? Buy Cheap Domain at Rapid Host :)

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by topdown.me View Post
    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.

    Edit:
    Quote Originally Posted by topdown.me View Post
    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.
    Last edited by tangoforce; 11-10-2012 at 09:39 PM.
    I can't really think of anything to write here now...

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    52
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    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.

    Edit:


    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...
    Need domain? Buy Cheap Domain at Rapid Host :)

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    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.
    I can't really think of anything to write here now...

  • #5
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by tangoforce View Post
    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.

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    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?
    I can't really think of anything to write here now...

  • #7
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by tangoforce View Post
    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

    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 Code:
    <?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.
    Last edited by minder; 11-11-2012 at 01:13 AM.

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by minder View Post
    And you seem to be forgetting that some do understand the limitations

    PHP Code:
    <?php
    if(isset($_POST['btnSubmit'])){
    Are you serious? - See the links in my signature as to why this technique is not recommended (due to the technical limitations of IEs bug).

    Quote Originally Posted by minder View Post
    PHP Code:
        unset($_POST['btnSubmit']); 
    What on earth is that for?

    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 can't really think of anything to write here now...

  • #9
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by tangoforce View Post
    Are you serious? -
    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.



    Quote Originally Posted by tangoforce View Post
    What on earth is that for?
    Force of habit because in some cases it is necessary.



    Quote Originally Posted by tangoforce View Post
    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.
    Last edited by minder; 11-11-2012 at 01:31 AM.

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by minder View Post
    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 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
    I can't really think of anything to write here now...

  • #11
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by tangoforce View Post
    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

  • #12
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    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.
    I can't really think of anything to write here now...

  • #13
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by tangoforce View Post
    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.

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by minder View Post
    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.
    I can't really think of anything to write here now...

  • #15
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    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

    Anyway, the op has x-posted inanother thread.

    http://www.codingforums.com/showthre...85#post1290785


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •