-1

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>
  • 1
    Does this answer your question? [How to use onClick() or onSelect() on option tag in a JSP page?](https://stackoverflow.com/questions/3487263/how-to-use-onclick-or-onselect-on-option-tag-in-a-jsp-page) – CBroe Jun 30 '20 at 09:39
  • _“I tried several methods like onchange function in javascript but it gets applied to the whole select menu”_ - so what? Then you _check_ which option was selected, before you do anything else. – CBroe Jun 30 '20 at 09:40
  • @CBroe the link you gave didn't help me I just want to apply the href link on a single option tag which is add new category. – Raju Prasad Jun 30 '20 at 09:46
  • It shows how to use a change event handler on the select element, and how to get _which_ option was selected. That _is_ what you need here. (Because, as the answer also explains, `onclick` will not work properly on option elements.) Please stop saying “didn’t help” reflexively, just because there is no answer you can copy&paste directly, without having to do the tiniest bit yourself. – CBroe Jun 30 '20 at 09:50
  • You should be able to take this, and write your own function, that only redirects elsewhere using `location.href = …`, when the specific option you want to trigger this, was selected. Make an _attempt_ at least, and show us that, instead of going “didn’t work” or “doesn”t help”. – CBroe Jun 30 '20 at 09:52
  • I tried these above-mentioned methods but I didn't get the desired output that's why I asked question I am still a student and learning new things that's why I don't know much about it, thank you for your valuable time. – Raju Prasad Jun 30 '20 at 10:16
  • _“I tried these above-mentioned methods but I didn't get the desired output”_ - then you need to _show us_ what you have tried, so that we can figure out where you went wrong. (For the future; this particular problem here should be solved, once you fix the small mistake kannan made in their answer.) – CBroe Jun 30 '20 at 10:18
  • sorry, I am new to this forum and I don't know much what to show I tried from next time I will take care of these to not happen again. – Raju Prasad Jun 30 '20 at 10:26

1 Answers1

0

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>
kannan
  • 691
  • 5
  • 17
  • I tried this method but didn't get the desired output it's still redirecting the whole element in select tag instead of redirecting only the last value i.e categories – Raju Prasad Jun 30 '20 at 10:11
  • could please share that Where you want to redirect – kannan Jun 30 '20 at 10:13
  • Are you looking for the functionality to add a new category into the existing category drop down list itself – kannan Jun 30 '20 at 10:15
  • 1
    `onChange="if(this.value=='categories'); window.location='addcategories.php'"` – that semi-colon after the `if(…)` is the mistake here, that needs to be removed. This makes the if block “empty”, and anything else that comes after it, will execute in any case. – CBroe Jun 30 '20 at 10:16
  • @kannan exactly right I am looking for a functionality to add a new category in existing categories. – Raju Prasad Jun 30 '20 at 10:20
  • thankyou soo much it's working now thank you all for your help. – Raju Prasad Jun 30 '20 at 10:30