Loading page based on option selected when option changes from drop-down

Discussion in 'JavaScript' started by toughguy, Apr 1, 2007.

  1. #1
    Hi Friends,
    I want to know how to load a page as and when a value in the dropdown is selected based on the recently selected value.

    Suppose: Now say the page loaded it a.html and it has a dropdown when user changes selection in the dropdown to say B, the b.html should be loaded, it changed to C the c.html. Please note i don't want ot put any button or anything , appropriate page is loaded automatically once option is changed.

    Thanks a lot for ur help..

    Regards,
     
    toughguy, Apr 1, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    There is the 'onChange' event that gets fired when a select drop down's selected item is changed.

    The code should look something like this:

    <script type="text/javascript">
    function redirectMe (sel) {
        var url = sel[sel.selectedIndex].value;
        window.location = url;
    }
    </script>
    <select name="myselect" onchange="redirectMe(this);">
        <option value="a.html">A</option>
        <option value="b.html">B</option>
        <option value="c.html">C</option>
    </select>
    Code (markup):
     
    phper, Apr 2, 2007 IP
    toughguy likes this.
  3. toughguy

    toughguy Well-Known Member

    Messages:
    846
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    118
    #3
    Hi Phper,
    Thanks a lot..

    Rep added..

    Regards,
     
    toughguy, Apr 2, 2007 IP
  4. dj_djp

    dj_djp Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I know this thread is old now, but this is really similar to what I am after. My only prob is that i need to change the folder that the html file is in as opposed to the html file itself

    E.g. Instead of

    http://www.website.com/a.html
    http://www.website.com/b.html
    http://www.website.com/c.html


    I need

    http://www.website.com/a/index.html
    http://www.website.com/b/index.html
    http://www.website.com/c/index.html

    Any ideas?
     
    dj_djp, Jul 18, 2007 IP
  5. samusexu

    samusexu Well-Known Member

    Messages:
    138
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Hi, just change the value of the options to your desired URL's like bellow: this would work with your example links.

    
    <script type="text/javascript">
    function redirectMe (sel) {
        var url = sel[sel.selectedIndex].value;
        window.location = url;
    }
    </script>
    <select name="myselect" onchange="redirectMe(this);">
        <option value="http://www.website.com/a/index.html">A</option>
        <option value="http://www.website.com/b/index.html">B</option>
        <option value="http://www.website.com/c/index.html">C</option>
    </select>
    
    Code (markup):
    Hope this helps, cheers :)
     
    samusexu, Jul 19, 2007 IP
  6. software4

    software4 Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I have a similar issue, how do i get this to put the value in the action.

     function redirectMe (sel) {
        var url = sel[sel.selectedIndex].value;
        window.location = url;
    }
    
    
    <form name="myselect" onchange="redirectMe(this);">
                          <p align="center">
                            <input type="hidden" name="join_event_id" id="join_event_id" value="<?php echo $event ?>">
                            <br>
                          </p>
    			      <p align="center"> <span class="text">Choose Candidate: &nbsp;</span><span class="style6">&nbsp;  
    			        <select name="candidate" class="text" id="position">
    			          <?php
    					  $sql =  "SELECT
    						`id`, `name`
    					FROM
    						`bio` 
    					WHERE
    						`event` = '$event'";
    					$result2 = mysql_query($sql);
    					if(mysql_affected_rows() > 0) {
    						while ($row = mysql_fetch_assoc($result2)) {  
    							echo '<option value="' . $row['id'] . '" '; if($id == $row['id']) echo 'selected="selected"'; echo '>' . $row['name'] . '</option>';
    						}
    					}
     
    ?>
      </select>
    			        </span>
    			        &nbsp; 
    			        
    			      </p>
       			          
    		              <div align="center">
      <input type="hidden" name="MM_insert" value="form1">
    		              </div>
                        </form>
    PHP:
     
    software4, Oct 12, 2009 IP