Home > Mobile >  Selection option in php loop
Selection option in php loop

Time:07-14

I have the code below and this code works, but only after clicking on selection option but code doesn't work when i change value when use arrow up and down.

I tried modification script, change option "click" to "change" but this solution doesn't work. Someone can help me ?

$select = $db_connect -> query("SELECT * FROM templates");      
if($select -> num_rows > 0)
{
    echo '<select id="subject" name="subject" >';
    
    while($row = $select -> fetch_assoc())
    {
        echo '<option id="'.$row["id"].'" value="'.$row['template_subject'].'">'.$row['template_subject'].'</option>';
        ?>
        <script>
        $("#subject #<?php echo $row['id']; ?>").on('click', function(){
            
            if((this.id = "<?php echo $row['id'];?>") && (this.id != 1))
            {
                $("textarea").html("<?php echo $row['template_text']; ?>");
                $("input[name='new_subject']").hide();
            }
            else
            {
                $("textarea").html("");
                $("input[name='new_subject']").show();
            }

        }); 
        </script>
        <?php
    }
    echo '</select>';
}

CodePudding user response:

Your problem is in the Javascript code.

        <script>
        $("#subject").change( function(){
             var text = $( "#subject option:selected").val();
             var id = $(this).children(":selected").attr("id");

            if(id != 1)
            {
                $("textarea").html(text);
                $("input[name='new_subject']").hide();
            }
            else
            {
                $("textarea").html("");
                $("input[name='new_subject']").show();
            }

        }); 
        </script>

remove the script from the while loop , put it at the end before </body> tag.

  • Related