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
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts

    dynamic select_options trouble

    Hey All,
    Trying to dynamicaly generate options for a predefined select named MSA.
    The following works... sort of -- insofar as I manage to generate at least one option,
    but if there are more matches of ($state_abrev and $parr[3]) I still get only one option -- (that from the first match).
    Please, what am I doing wrong here?

    PHP Code:
    foreach($msa_stats as $parr){
        if(
    preg_match("/$state_abrev/i"$parr[3])) {
        echo 
    "<script>
            var opts=document.forms['get_MSA'].MSA.options;
            for (i=0;i<opts.length;i++){
            opts[i] = new Option('$parr[2]','$parr[2]');
            }        
            </script>"
    ;
          }


  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You probably want to revise the way you build the Javascript to only echo one beginning script tag and assign the variable opts only once. Then inside your foreach() you should echo the Javascript code that adds a new option, and make sure the iterator variable you use (the variable "i") doesn't get set back to zero each time a match is found. Instead you should add 1 to i each time a match is found. I think...

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Thanks Fumigator,

    Took me a bit, but I got it!

    PHP Code:
    $get_msa "<script>var opts=document.forms['get_MSA'].MSA.options;for (i=0;i<opts.length;i++){ ";
        foreach(
    $msa_stats as $parr){
            if (
    preg_match("/$state_abrev/i"$parr[3])) {
            
    $get_msa "$get_msa"."opts[i++] = new Option('$parr[2]','$parr[2]'); ";
        )
    $get_msa.= '}</script>';
    echo 
    $get_msa
    Surprised to discover that concatenation ($get_msa.=)would not work within the confines of the foreach, so I worked around it.


  •  

    Posting Permissions

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