Keep $_GET value of a drop down menu after submitting the form

user3450364

EDITED:

Demo: http://jsfiddle.net/53GKh/6/

I have 2 Drop down menu's in HTML:

First one is to get car brand:(is working fine)

<form action="car-list.php" method="get">
<select name="search" id="main_list" >
<option value="BMW"<?php if ($_GET['search']== 'BMW') {echo "selected='selected'"; }   ?>>Bmw</option>
</select>
</form>

After the user choose a Brand the second drop down menu will be 'activated' to choose the model of the car:

<select  name="model"  id="brand" class="secondary">
<option disabled selected> Kies </option>
</select>

To 'activate' the second drop down menu im using the following JS code:

$(function() {
var sel, i,
    list = ['aixman', 'alfaromeo', 'bmw'],
    aixman = ['Aixman'],
    alfaromeo = ['33', '75'],
    bmw= ['1-serie','3-serie','4-serie','5-serie','6-serie'],
    astonmartin = ['Cygnet', 'DB7'],

    dev_default = '<option value="default" selected>Kies</option>',



sel_brand = $('#brand');
sel_version = $('#version');

$('select').change(function() {
    switch (this.id) {
        case 'main_list':
            sel_brand.find('option').remove();
            sel_brand.append(dev_default);
            sel_brand.show();
            if (this.value == 'BMW') {
                for (i = 0; i < bmw.length; i++) {
                    $("#brand").append(
                        '<option value="' + bmw[i] + '">' + bmw[i] + '</option>'
                    );        
                }

            }
            break;

    }
});

}); //END document.ready()

I have just updated my JS code: How can I echo the selected value of my second dropbox menu after submitting/refreshing the page?

user216084

I suggest you change your server-side PHP code to accommodate this functionality.

Steps:

  1. Whenever a form is submitted, using the menu 1 selection, get all the values for menu 2 and set in a variable.
  2. In the HTML of second menu, you check that if the variable in step 1 isset(), and if yes, then create HTML for the options of menu 2 in a loop. Also, while doing so, using the selected value from your form submission, pre-select the option. Else if empty(), then do nothing.

When the user will be making a selection in menu 1, JavaScript will kick in and do its task. And whenever form is submitted, this will pre-create the menu 2.

Hope this gets you going in the right direction. Kindly ask if you face any difficulty in implementing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related