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.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript load option value from TXT file, show that in dropdown and get user choice

    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

    Code:
    <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

    Code:
    <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)

    Code:
    <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

    EDIT: 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?

    Thanks for help in advance

  • #2
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by jeremygb View Post
    Or maybe these values (456, 121) sholud be the ID, and value sholud be order in txt file (1,2,3...)
    This sounds like the correct solution. PHP will see whatever is in the value attribute after form submission.

    You can programmatically correct the incorrect values, but it would make more sense to just fix the HTML markup.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #3
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great. Thanks for the advice. A friend help me with this javascript script, and I'm not so proficient with js.

    How can I switch in script, so that it looks for ID, not value?

    $(this).val() switch to $(this).attr('id') - something like this?


  •  

    Tags for this Thread

    Posting Permissions

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