www.webdeveloper.com
Results 1 to 6 of 6

Thread: Select List keep selected value after auto submit

  1. #1
    Join Date
    Oct 2013
    Posts
    8

    Select List keep selected value after auto submit

    Hi,

    I have a select list as seen below and I want to keep the selected value after it automatically selects can anyone help please? I have tried a few things using javascript but nothing seemed to work.

    Code:
    <select name="inteu_06" onchange="this.form.submit()" style="margin-right:0px">
            <option value="~">Price Limit - Any</option>
            <?php
    do {  
    ?>
            <option value="<?php echo $row_Recordset1['inteu_06']?>"><?php echo $row_Recordset1['inteu_06']?></option>
            <?php
    } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
      $rows = mysql_num_rows($Recordset1);
      if($rows > 0) {
          mysql_data_seek($Recordset1, 0);
    	  $row_Recordset1 = mysql_fetch_assoc($Recordset1);
      }
    ?>
          </select>
    Many thanks
    Joe

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    Quote Originally Posted by jmd87 View Post
    ... I want to keep the selected value after it automatically selects ...
    it's not clear what you mean

  3. #3
    Join Date
    Oct 2013
    Posts
    8
    Hi

    thanks for the reply. Sorry I made a mistake there I ment to say:

    I want to keep the selected value after it automatically submits on selection.
    Does that make sense now?

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    sorry i'm not good in the elnglish language that's why i asked ))

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>123</title>
    <style>
    body{padding-top:250px;text-align:center;}
    </style>
    <script>
    function doc(id){return document.getElementById(id);}
    window.onload=function(){
    var needle='sindex=',
    loc=location.search,
    sindex=loc.substring(loc.indexOf(needle),loc.length)||0,
    sel=doc('myform').getElementsByTagName('select')[0],
    si=doc('myform').elements['sindex'];
    
    sel.selectedIndex=(sindex!=0)?sindex.substring(7,sindex.length):sindex;
    sel.onchange=function(){si.value=this.selectedIndex;doc('myform').submit();}
    }
    </script>
    </head>
    <body>
    <form id="myform" name="myform" action="">
    <select name="inteu_06">
    <option value="">select</option>
    <option value="somevalue">1</option>
    <option value="somevalue">2</option>
    <option value="somevalue">3</option>
    <option value="somevalue">4</option>
    <option value="somevalue">5</option>
    </select>
    <!-- place this hidden input as the last element of the form-->
    <input type="hidden" name="sindex" />
    </form>
    </body>
    </html>

  5. #5
    Join Date
    Oct 2013
    Posts
    8
    Hi,

    It was my fault Thanks for your help but that wasn't what i wanted (my mistake again!)

    I have 4 select lists sorted by alphabetical and I have 1 select list that I want sorted by numerical order. At the moment the number select list does this:

    150
    200
    50
    etc
    etc

    I want it to display as:

    50
    150
    200
    etc
    etc

    The select list is populated from a MySQL table.

    Thanks for your help

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    every option may have the value attribute and the innerHTML which is the text we can see. your questions are not specific thats why it's not clear what you want: to sort the options by numeric order according to each option's value or its text? please ask your questions so the others can understand them unambiguously. by the way, if the select list is populated from a MySQL table why not do this sorting at the server side with php and then output the desirable select element in the browser??

    anyway, here is sorting by option's tex example:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>123</title>
    <style>
    body{padding-top:250px;text-align:center;}
    </style>
    <script>
    function doc(id){return document.getElementById(id);}
    function doCompare(a,b){return a[0]-b[0];}
    function sortNumeric(id){
    var normal_order=[],elem=doc(id),opts=elem.options;
    for(var i=0;i<opts.length;i++){
    normal_order.push([opts[i].text,opts[i].value]);
    }
    elem.options.length=0;
    new_order=normal_order.sort(doCompare);
    for(var k=0;k<new_order.length;k++){
    elem.options[k]=new Option(new_order[k][0],new_order[k][1]);
    }
    }
    /* if you want to sort it automatically replace the below lines 
    with window.onload=function(){sortNumeric('inteu_06');} */
    window.onload=function(){
    doc('go').onclick=function(){sortNumeric('inteu_06');}
    }
    </script>
    </head>
    <body>
    <form id="myform" name="myform" action="">
    <select id="inteu_06" name="inteu_06">
    <option value="abc">100</option>
    <option value="def">25</option>
    <option value="ghi">3</option>
    <option value="jkl">455</option>
    <option value="mno">5000</option>
    </select>
    <br /><br />
    <input type="button" id="go" value="Sort numeric" />
    </form>
    </body>
    </html>
    Last edited by Padonak; 10-31-2013 at 04:12 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles