简体   繁体   中英

how to add a link on single option in select tag

hello I am trying to create a category select dropdown which consists of some data coming from database I have created a page named addcategory.php and I am trying to add a in the select tag something like "add new category" I tried several methods like onchange function in javascript but it gets applied to the whole select menu I only want to add a link to the last option of the select tag below I am attaching the code which I am using.

<select class="w-100" id="categoryselect" name="selectcat" required>
  <option value="" disabled selected>Select Category</option>
    <?php 
        $sql="SELECT * FROM categories";
        $result=mysqli_query($conn, $sql);
        while($row=mysqli_fetch_array($result))
        {
            echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
        } 
    ?>
  <option value="categories" onClick="window.location = 'addcategory.php'">Add New Category</option>
</select>

Bind the event handler to select tag instead of bind the event into options tag.

The onchange event occurs when the value of an element has been changed.

<select class="w-100" id="categoryselect" name="selectcat" onChange="if(this.value=='categories') window.location='addcategories.php'">
      <option value="" disabled selected>Select Category</option>
        <?php 
            $sql="SELECT * FROM categories";
            $result=mysqli_query($conn, $sql);
            while($row=mysqli_fetch_array($result))
            {
                echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
            } 
        ?>
      <option value="categories">Add New Category</option>
    </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM