SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Form options

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Location
    Gothenburg, Sweden
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form options

    Hey all,

    Does anyone know how to get the value from a form option (dropdown list) and have it post the value without having a submit button?

    So that when I choose a value in the option list the page is automatically reloaded with that value?

    Cheers!

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Not really sure if that's what you mean, but:
    HTML Code:
    <form name="f1">
      <select onchange="document.f1.submit()">
        <option value="1">1</option>
        <option value="1">1</option>
      </select>
    </form>
    Saul

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2006
    Location
    Gothenburg, Sweden
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a way to do it without javascript?

    I can't get it to work the way I want it to work though.

    I have this code:
    Code:
    <form name="categories" id="select-category" method="get" action="index.php">
       <div>
    	<label for="category-list">Categories:</label>
    	<select id="category-list" onchange="document.categories.submit()">
    		<option name="category" value="all">All</option>
    		<option name="category" value="1">Sports</option>
    		<option name="category" value="2">Technology</option>
    		<option name="category" value="3">Science</option>
    		<option name="category" value="4">Entertainment</option>
    		<option name="category" value="5">Gaming</option>
    		<option name="category" value="6">World & Business</option>
    	</select>
       </div>
    </form>
    And If I click on Sports the url will point to index.php?=1 ... not index.php?category=1

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add name="category" to your <select> tag.
    Birnam wood is come to Dunsinane

  6. #6
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And remove the name attribute from the <option>s.

    I suggest you add a submit button as well. You could put it in a <noscript> element if you didn't want to show it when JavaScript is available.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  7. #7
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz View Post
    I suggest you add a submit button as well. You could put it in a <noscript> element if you didn't want to show it when JavaScript is available.
    Actually, it's better to include it in the markup (no NOSCRIPT) and remove the button with JavaScript if that's the case. This takes care of certain special cases where the browser supports JavaScript but a company firewall strips JS code. (In this case the NOSCRIPT element will not be applied.)
    Birnam wood is come to Dunsinane


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
  •