Hello fellow programmers. I'm in a need of community assistance :)

I have a php form with three dropdowns that has jquery function that loads .txt files and builds two dynamic dropdowns realted to first choice.

First user choses option (hardcoded in script) - normal dropdown. Then the script takes control of other choices.

Script is here. Details of my question are below

<script type="text/javascript">
$(function() {

$("#text-one").change(function() {

$.get("/textdata2/podgrupa/" + $(this).val() + ".txt", function(data) {
  $('#text-two').html('<select onChange="load_subcategory(this.value)" name="podkategorija">'+data+'</select>');
    if($("#text-two select").val()!='base'){
        $.get("/textdata2/podkategorija/" + $("#text-two select").val() + ".txt", function(data) {
          $('#text-three').html('<select name="podgrupa">'+data+'</select>');
        }).fail(function() { 
            alert("Missing file: /textdata2/podkategorija/" + $("#text-two select").val() + ".txt");
            $("#text-three").html('<select name="podgrupa"><option>Odaberite rubriku</option></select>') ;
        });
    }else
        $("#text-three").html('<select name="podgrupa"><option>Odaberite rubriku</option></select>');

});


});
});

function load_subcategory(val){
    $.get("/textdata2/podkategorija/" + val + ".txt", function(data) {
      $('#text-three').html('<select name="podgrupa">'+data+'</select>');
    }).fail(function() { 
            alert("Missing file: /textdata2/podkategorija/" + val + ".txt");
            $("#text-three").html('<select name="podgrupa"><option>Odaberite rubriku</option></select>') 
        });

}
</script>

example of one txt file from podgrupa folder

<option value="360">Iznajmljivanje</option>
<option value="361">Unajmljivanje</option>
<option value="456">Cimeri</option>

example of one txt file from podkategorija folder (that is related by value to choice in dropdown that looks into podgrupa folder)

<option value="120">regija 051</option>                    
<option value="121">regija 052</option>                    
<option value="122">ostalo</option> 

When form posts data that user had choose, it writes values from tags. With these values jquery script knows which choice is connected to other.

For example: If user had chose: Rooms (from first hardcoded dropdown - code is 155 in option value), then script automaticaly loads second dropdown with choices from txt file (podgrupa), then user choses from those choices (for example Cimeri - 456) in second dropdown (podgrupa), and script loads third dropdown value (podkategorija) related to choice from second. User then choses value from that 3rd dropdown (for example regija 052 - 121)

After form submission, these values would be written by php: 155, 456, 121.

However, I need it to write different values for second(456) and third(121) choice. - For second it needs to write value "3" and for third value "2" if user has chosen those choices from example. As you can see those different values are the same as position of choice in txt file.

Can something like that be achived with ? Maybe with additional ID? Or maybe these values (456, 121) sholud be the ID, and value sholud be order in txt file (1,2,3...)

Hope you understand what I want to achieve :)

Can I maybe add ID to option (in all files, manually) and use that to replace option value with option ID before writing it file or sending to email?

If I understand correctly, you are populating a select based on the value chosen from a previous select. When all 3 options are chosen you want the value of the first select and the index (rather than value) of the options chosen for the remaining selects to be submitted.

You could use a hidden form field <input name="idlist" type="hidden" value=""> which will be updated by JavaScript (jQuery) as each option is chosen. e.g. <input name="idlist" type="hidden" value="155,3,2>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.