Home  >  Q&A  >  body text

Select option using PHP loop

I have the code below which works but only after clicking to select the option but when I change the value while using the up and down arrows the code does not work.

I tried modifying the script to change the option "Click" to "Change" but this solution did not work. Can anyone help me?

$select = $db_connect -> query("SELECT * FROM templates");
if($select -> num_rows > 0)
{
    echo '<select id="subject" name="subject" class="form-select">';
    
    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>';
}

P粉788765679P粉788765679209 days ago322

reply all(1)I'll reply

  • P粉105971514

    P粉1059715142024-02-27 10:19:18

    Your problem is in the Javascript code.

    Remove the script from the while loop and place it at the end before the tag.

    reply
    0
  • Cancelreply