www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Get Selected Value in Dropdown

  1. #1
    Join Date
    Apr 2009
    Posts
    73

    resolved [RESOLVED] Get Selected Value in Dropdown

    Hi Experts,

    I have three element on a form, a dropdown box which gets values from a table and a textbox which will show the value based on the selection in dropdown, and a third textbox which is uses less at the moment.

    What I want is that when I select any value from the dropdown box and submits the form the selected value disappears from the dropdown textbox and the first value of dropdown list appears.

    I want that the dowpdown shold display the selected value upon submission of form.

    Hope you unerstand my problem and will help me.

    Regards,

    Below is my code.

    PHP Code:
    <?php
    //Naveed
    //2011
    $con mysql_connect("localhost","root","");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }
      
    mysql_select_db("ansid",$con);
    ?>
    <form action="<?PHP $_SERVER['PHP_SELF'?>" method="GET" name="frmAdm">
    <table>
    <tr>
        <td>AdmNo</td>
        <td><select size="1" name="AdmNo" onchange="document.forms['frmAdm'].submit();">
        <option>Admission No</option>
          <?php
        $result 
    mysql_query("select AdmNo from bio");
        while(
    $row mysql_fetch_array($result))
      {
    ?>
    <option><?php  echo $row['AdmNo']; ?></option>
    <?php
    }
        
    ?>
    </select>
        <?php echo $_GET['AdmNo'];
        
    $adm $_GET['AdmNo']; ?>     
    </td>
    </tr>
    <tr>

          <td>RollNo</td>
        <td>
        
    <?php
        $result 
    mysql_query("select RollNo from bio where AdmNo='$adm'");
        while(
    $row mysql_fetch_array($result))
      {
    echo 
    "<input type='text' name='RollNo' value=".$row['RollNo'].">"; }
    ?>  </td>
    </tr>
    <tr>
        <td>Class</td>
        <td><select name="Class">
          <option>Class</option>
          <option>KG</option>
          <option>Prep</option>
          <option>1st</option>
          <option>2nd</option>
          <option>3rd</option>
          <option>4th</option>
          <option>5th</option>
          <option>6th</option>
          <option>7th</option>
          <option>8th</option>
          <option>9th</option>
          <option>10th</option>
        </select></td>
    </tr>
    <tr>
    <td></td><td><input type="submit" /></td></tr>
    </table>
    </form>
    <?php

    mysql_close
    ($con);
    ?>

  2. #2
    Join Date
    Oct 2011
    Location
    Vero Beach, Florida
    Posts
    70
    What I want is that when I select any value from the dropdown box and submits the form the selected value disappears from the dropdown textbox and the first value of dropdown list appears.

    I want that the dowpdown shold display the selected value upon submission of form.
    Well I don't understand. First you said you want the selected value to disappear, then you said you want it to display.

    You can use PHP to rewrite the list as you want, after the form is submitted.
    space
    Orchid Technical Services
    Website Design/Development and Technical Support

  3. #3
    Join Date
    Apr 2009
    Posts
    73
    Dear I want that When I choose a value from the options dropdown and the form is submitted, then the drop down does not show the value selected or chosen but the default value. e.g

    the list contains A,B,C and I chose B, and the form is submitted, the dropdown should show the value B but it shows me A instead of B.

    Hope u understand this time.

    I am trying this code:

    PHP Code:
    <?php
        
    echo '
        <script type="text/javascript">
    i = document.getElementById["iOpt"]; 
    i.options[0].text = ' 
    $_GET["opt"].'
    </script>
        '
    ;
    ?>

    <form name="optf" action="<?PHP $_SERVER['PHP_SELF'?>" method="GET">
    <select name="opt" id="iOpt" onchange="submit();">
    <option value="A" >A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    </select>
    </form>
    Last edited by nyt1972; 11-14-2011 at 12:39 AM.

  4. #4
    Join Date
    Mar 2009
    Location
    Yorkshire
    Posts
    266
    Am i correct in saying that what you want to do is to show the user what value they selected in the drop down box after they submit the form?

    To do this in html you would do something like

    HTML Code:
    <select name="test">
         <option value="a">Value A</option>
         <option value="b" selected>Value B</option>
         <option value="c">Value C</option>
    </select>
    This would select value B as the default when that page is loaded. Do do this based on form data you could simply write a few if statements which puts the word 'selected' in the right option tag depending on the data.
    Ryan Lund

    Development Manager
    http://www.ifindpoker.com

  5. #5
    Join Date
    Oct 2011
    Location
    Vero Beach, Florida
    Posts
    70
    Ryan is correct. Instead of using the javascript, just add to your PHP some code to rewrite the options list with the "selected" attribute added to the option that was selected from $_GET("opt").

    Don't know if you realize this: onchange="submit();" will not submit your form - you need an form <input> button with type=submit.
    space
    Orchid Technical Services
    Website Design/Development and Technical Support

  6. #6
    Join Date
    Apr 2009
    Posts
    73
    I found this code and it worked. Thanks a lot for your replies and time you gave.

    Regards,

    PHP Code:
    <?php $options = array( 1=>'General Question''Company Information''Customer Issue''Supplier Issue''Supplier Issue''Request For Quote''Other' ); $topic $_REQUEST['topic']; // the topic name would now be $options[$topic]  // other PHP etc... ?>  <select name="topic" style="margin-bottom:3px;">      <?php foreach ( $options as $i=>$opt ) : ?>         <option value="<?php echo $i?><?php echo $i == $topic 'selected' ''?>><?php echo $opt ?></option>     <?php endforeach; ?> </select>

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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