SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to make selected option unselectable

    I`m trying to create a form with options. What i want to do is make options unselectable for the next user when a user selects it. Is there a simple way i can achieve this?

    PHP Code:
    <select
      <
    option value ="volvo">Volvo</option
      <
    option value ="saab">Saab</option
      <
    option value ="opel">Opel</option
      <
    option value ="audi">Audi</option
    </
    select

  2. #2
    Mouse catcher silver trophy Stevie D's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    5,888
    Mentioned
    122 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by hasseholland View Post
    I`m trying to create a form with options. What i want to do is make options unselectable for the next user when a user selects it. Is there a simple way i can achieve this?

    PHP Code:
    <select
      <
    option value ="volvo">Volvo</option
      <
    option value ="saab">Saab</option
      <
    option value ="opel">Opel</option
      <
    option value ="audi">Audi</option
    </
    select
    Assuming you got that example from www.w3schools.com/tags/tag_option.asp, you just need to read a bit further down the page ... use the 'disabled' attribute to disable an option, ie prevent it from being selected.

    Edit:

    Moved from PHP to XHTML because the question as you've asked it isn't anything to do with PHP.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,809
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You can't make options unselectable using just HTML. You need a server side language such as PHP to do that (the OP posted in the correct forum to start with).

    What you would need to do is to capture the selection made by the first visitor and use that to determine which option to make unselectable for subsequent visitors. For example if the $sel variable contains the value of the option you want disabled you would use:

    Code:
    <select> 
      <option value ="volvo"<?php if ($sel == 'volvo') echo ' disabled'; ?>>Volvo</option> 
      <option value ="saab"<?php if ($sel == 'saab') echo ' disabled'; ?>>Saab</option> 
      <option value ="opel"<?php if ($sel == 'opel') echo ' disabled'; ?>>Opel</option> 
      <option value ="audi"<?php if ($sel == 'audi') echo ' disabled'; ?>>Audi</option> 
    </select>
    Note that this would only apply to the next person to open the form after the first person submitted it - it would not apply to the people who open the form while the first person is still filling it out.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •