0

I have a dropdown list of users from my database, then at the last row I want to make a static option to create a new user and redirect the user to another page,

Tried to post the link in value, or enter in <a> tags or add an href, but with no success.

    <label for="cars">Assign to:</label>
        <select id="cars" name="assignTo" required>
            <option value=""></option>
                      <option value="Jey">Jey</option>
                    <option value="Mike">Mike</option>
                      <option value="Moses">Moses</option>
                    <option value="Joe">Joe</option>
  <option value="" href="/add-user.php">Add new user</option>
        </select>
Peneh
  • 195
  • 1
  • 10
  • 1
    Not sure what you're trying to achieve, but option doesn't have an href attribute. - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option – rovr138 Jul 16 '21 at 16:19
  • You can put link inside value of the option then in the select you can use javascript to do redirect, you just need to check if value contains the link https://stackoverflow.com/a/7562129/10634638 – estinamir Jul 16 '21 at 16:22
  • I am trying to have some regular options and one to a link, if I use JS all will be directed and the file does not exist as this is the name of users. – Peneh Jul 16 '21 at 16:31

1 Answers1

2

You could make an onchange event check the value when it is changed, and use the open function to redirect:

var cars = document.getElementById("cars")

cars.addEventListener("change", function() {
  if (cars.value == "addUser") {
    open("/add-user.php", "_self")
  }
})
<label for="cars">Assign to:</label>
<select id="cars" name="assignTo" required>
  <option value=""></option>
  <option value="Jey">Jey</option>
  <option value="Mike">Mike</option>
  <option value="Moses">Moses</option>
  <option value="Joe">Joe</option>
  <option value="addUser">Add new user</option>
</select>

When it is changed, it checks the value for addUser, and if it is, it will redirect the user to the page.

Brendan R.
  • 113
  • 1
  • 6