Dropdown question (populating a text field)

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Dropdown question (populating a text field)

    Hello everybody,

    I have a problem and would appreciate any help on offer.

    I have a table with two fields in it. On my form there are the following
    objects:
    Dropdown menu (populated with the first field in the table)
    Textfield ( - problem - )

    What I'm trying to do is populate the Textfield with the second field
    corresponding to the chosen record in the Dropdown menu.
    I need to pass on both variables to the next .asp page (which writes the
    data into another table) as both need to be written into the database (I'm
    using the POST method).
    This also needs to be done without reloading the page if at all possible.

    e.g.
    field1 field2
    tom 001
    dick 002
    harry 003

    If "tom" is selected in the Dropdown menu, then i would like to see "001" in
    the Textfield etc.

    I've found some good code where I found out how I could populate the
    Textfield with the Dropdown item itself (using "OnChange" on the Dropdown
    menu - callig a small JS function) but this populates the Textfield with the
    Dropdown item, and not the corresponding field which I need.

    I hope that I've described the problem well enough! Thank you all for
    reading.

    ~p




    packet loss Guest

  2. Similar Questions and Discussions

    1. populating dropdown using tables from two databases
      I tried it with populating the values from the first qury to an array and using that array inside NOT IN clause of second query. But its giving...
    2. Slow populating dropdown?
      I am, for the first time, using Dreamweaver to create recordsets and populate a few dropdowns. Normally, I'd do this by hand, but thought I'd give...
    3. Populating a Text Field
      like i said before the table xtra is perfect for this job, that's what i used previously and it's so much clearer than trying to populate a text...
    4. dropdown and dynamic text field
      hi i am using the following code to try and get a dropdown (sizeList) to fill a dynamic text field with differing values. sizeList has setPrice...
    5. Populating DropDown in webform
      #1) put the data in table AA and put a flag like "New Detail" yes,no and requery you database and if user has selected "New Detail" then add ther...
  3. #2

    Default Re: Dropdown question (populating a text field)

    The quick and dirty way is to give the values in your dropdown both values
    from the db, i.e.

    <option value="tom,001">Tom</option>
    <option value="dick,002">Dick</option>

    And then use client side code to split the values, the get the one after the
    comma and use the same type of code that you have seen before to set the
    value of the textbox.

    Ray at work

    "packet loss" <packetloss@gmx.de> wrote in message
    news:boef6i$1e1jh0$1@ID-132410.news.uni-berlin.de...
    > Hello everybody,
    >
    > I have a problem and would appreciate any help on offer.
    >
    > I have a table with two fields in it. On my form there are the following
    > objects:
    > Dropdown menu (populated with the first field in the table)
    > Textfield ( - problem - )
    >
    > What I'm trying to do is populate the Textfield with the second field
    > corresponding to the chosen record in the Dropdown menu.
    > I need to pass on both variables to the next .asp page (which writes the
    > data into another table) as both need to be written into the database (I'm
    > using the POST method).
    > This also needs to be done without reloading the page if at all possible.
    >
    > e.g.
    > field1 field2
    > tom 001
    > dick 002
    > harry 003
    >
    > If "tom" is selected in the Dropdown menu, then i would like to see "001"
    in
    > the Textfield etc.
    >
    > I've found some good code where I found out how I could populate the
    > Textfield with the Dropdown item itself (using "OnChange" on the Dropdown
    > menu - callig a small JS function) but this populates the Textfield with
    the
    > Dropdown item, and not the corresponding field which I need.
    >
    > I hope that I've described the problem well enough! Thank you all for
    > reading.
    >
    > ~p
    >
    >
    >
    >

    Ray at Guest

  4. #3

    Default Re: Dropdown question (populating a text field)

    "packet loss" <packetloss@gmx.de> wrote in message
    news:boef6i$1e1jh0$1@ID-132410.news.uni-berlin.de...
    > Hello everybody,
    >
    > I have a problem and would appreciate any help on offer.
    >
    > I have a table with two fields in it. On my form there are the following
    > objects:
    > Dropdown menu (populated with the first field in the table)
    > Textfield ( - problem - )
    >
    > What I'm trying to do is populate the Textfield with the second field
    > corresponding to the chosen record in the Dropdown menu.
    > I need to pass on both variables to the next .asp page (which writes the
    > data into another table) as both need to be written into the database (I'm
    > using the POST method).
    > This also needs to be done without reloading the page if at all possible.
    >
    > e.g.
    > field1 field2
    > tom 001
    > dick 002
    > harry 003
    >
    > If "tom" is selected in the Dropdown menu, then i would like to see "001"
    in
    > the Textfield etc.
    >
    > I've found some good code where I found out how I could populate the
    > Textfield with the Dropdown item itself (using "OnChange" on the Dropdown
    > menu - callig a small JS function) but this populates the Textfield with
    the
    > Dropdown item, and not the corresponding field which I need.
    >
    > I hope that I've described the problem well enough! Thank you all for
    > reading.
    >
    > ~p
    How about the following except that you would build the drop-down list from
    your database.


    <html>
    <head>
    <title>page1.asp</title>
    <script language="javascript" type="text/javascript">
    <!--
    function DropText() {
    var form = document.forms[0];
    var valu = form.Drop.options[form.Drop.selectedIndex].value;
    if (valu != "") form.Text.value = valu;
    }
    // -->
    </script>
    </head>
    <body>
    <form action="page2.asp" method="post">
    <select name="Drop" onchange="DropText()">
    <option value="">
    <option value="001">tom
    <option value="002">dick
    <option value="003">harry
    </select>
    <textarea name="Text" rows="6" cols="10"></textarea>
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>



    McKirahan Guest

  5. #4

    Default Re: Dropdown question (populating a text field)

    [...]
    >
    > How about the following except that you would build the drop-down list
    from
    > your database.
    >
    >
    > <html>
    > <head>
    > <title>page1.asp</title>
    > <script language="javascript" type="text/javascript">
    > <!--
    > function DropText() {
    > var form = document.forms[0];
    > var valu = form.Drop.options[form.Drop.selectedIndex].value;
    > if (valu != "") form.Text.value = valu;
    > }
    > // -->
    > </script>
    > </head>
    > <body>
    > <form action="page2.asp" method="post">
    > <select name="Drop" onchange="DropText()">
    > <option value="">
    > <option value="001">tom
    > <option value="002">dick
    > <option value="003">harry
    > </select>
    > <textarea name="Text" rows="6" cols="10"></textarea>
    > <input type="submit" value="Submit">
    > </form>
    > </body>
    > </html>
    >
    >
    I know why I don't like JS that much lol. I'll look at it tomorrow at work,
    It's past 23:00 here and I need my beauty sleep :)

    Cheers,

    ~p


    packet loss Guest

  6. #5

    Default Re: Dropdown question (populating a text field)

    [...]
    > How about the following except that you would build the drop-down list
    from
    > your database.
    >
    >
    > <html>
    > <head>
    > <title>page1.asp</title>
    > <script language="javascript" type="text/javascript">
    > <!--
    > function DropText() {
    > var form = document.forms[0];
    > var valu = form.Drop.options[form.Drop.selectedIndex].value;
    > if (valu != "") form.Text.value = valu;
    > }
    > // -->
    > </script>
    > </head>
    > <body>
    > <form action="page2.asp" method="post">
    > <select name="Drop" onchange="DropText()">
    > <option value="">
    > <option value="001">tom
    > <option value="002">dick
    > <option value="003">harry
    > </select>
    > <textarea name="Text" rows="6" cols="10"></textarea>
    > <input type="submit" value="Submit">
    > </form>
    > </body>
    > </html>
    >
    Hello,
    this works a great! Like you said, all I had to do was to populate the
    dropdown box as before, and the other parameter also gets shown (and can be
    therefore passed on).

    Thank you!

    ~p


    ~p Guest

  7. #6

    Default Re: Dropdown question (populating a text field)

    "~p" <packetloss@gmx.de> wrote in message
    news:bofnvc$1buupq$1@ID-132410.news.uni-berlin.de...
    > [...]
    > > How about the following except that you would build the drop-down list
    > from
    > > your database.
    > >
    > >
    > > <html>
    > > <head>
    > > <title>page1.asp</title>
    > > <script language="javascript" type="text/javascript">
    > > <!--
    > > function DropText() {
    > > var form = document.forms[0];
    > > var valu = form.Drop.options[form.Drop.selectedIndex].value;
    > > if (valu != "") form.Text.value = valu;
    > > }
    > > // -->
    > > </script>
    > > </head>
    > > <body>
    > > <form action="page2.asp" method="post">
    > > <select name="Drop" onchange="DropText()">
    > > <option value="">
    > > <option value="001">tom
    > > <option value="002">dick
    > > <option value="003">harry
    > > </select>
    > > <textarea name="Text" rows="6" cols="10"></textarea>
    > > <input type="submit" value="Submit">
    > > </form>
    > > </body>
    > > </html>
    > >
    >
    > Hello,
    > this works a great! Like you said, all I had to do was to populate the
    > dropdown box as before, and the other parameter also gets shown (and can
    be
    > therefore passed on).
    >
    > Thank you!
    >
    > ~p
    Your very welcome. On reflection, I've thought of a couple of things.

    1) There is no need for the "if" test in the function.

    2) You might want to give the form a name and reference it in the
    function. If there is only one form on the page then there's no problem; if
    there's more than one then there could be a problem -- naming it might be
    safer.

    3) You might want to add "onsubmit=" to the form tag to invoke a function
    that ensures that an item has been selected before proceeding to the next
    page.

    <form action="page2.asp" method="post" name="form1" onsubmit="return
    DropTest()">

    function DropTest() {
    var form = document.form1;
    if (form.Text.value != "") return true;
    alert("Selection required!");
    return false;
    }

    function DropText() {
    var form = document.form1;
    var valu = form.Drop.options[form.Drop.selectedIndex].value;
    form.Text.value = valu;
    }


    McKirahan Guest

  8. #7

    Default Re: Dropdown question (populating a text field)


    "McKirahan" <News@McKirahan.com> schrieb im Newsbeitrag
    news:LkPqb.139977$e01.468311@attbi_s02...
    > "~p" <packetloss@gmx.de> wrote in message
    > news:bofnvc$1buupq$1@ID-132410.news.uni-berlin.de...
    > > [...]
    > > > How about the following except that you would build the drop-down list
    > > from
    > > > your database.
    > > >
    > > >
    > > > <html>
    > > > <head>
    > > > <title>page1.asp</title>
    > > > <script language="javascript" type="text/javascript">
    > > > <!--
    > > > function DropText() {
    > > > var form = document.forms[0];
    > > > var valu = form.Drop.options[form.Drop.selectedIndex].value;
    > > > if (valu != "") form.Text.value = valu;
    > > > }
    > > > // -->
    > > > </script>
    > > > </head>
    > > > <body>
    > > > <form action="page2.asp" method="post">
    > > > <select name="Drop" onchange="DropText()">
    > > > <option value="">
    > > > <option value="001">tom
    > > > <option value="002">dick
    > > > <option value="003">harry
    > > > </select>
    > > > <textarea name="Text" rows="6" cols="10"></textarea>
    > > > <input type="submit" value="Submit">
    > > > </form>
    > > > </body>
    > > > </html>
    > > >
    > >
    > > Hello,
    > > this works a great! Like you said, all I had to do was to populate the
    > > dropdown box as before, and the other parameter also gets shown (and can
    > be
    > > therefore passed on).
    > >
    > > Thank you!
    > >
    > > ~p
    >
    > Your very welcome. On reflection, I've thought of a couple of things.
    >
    > 1) There is no need for the "if" test in the function.
    >
    > 2) You might want to give the form a name and reference it in the
    > function. If there is only one form on the page then there's no problem;
    if
    > there's more than one then there could be a problem -- naming it might be
    > safer.
    >
    > 3) You might want to add "onsubmit=" to the form tag to invoke a
    function
    > that ensures that an item has been selected before proceeding to the next
    > page.
    >
    > <form action="page2.asp" method="post" name="form1" onsubmit="return
    > DropTest()">
    >
    > function DropTest() {
    > var form = document.form1;
    > if (form.Text.value != "") return true;
    > alert("Selection required!");
    > return false;
    > }
    >
    > function DropText() {
    > var form = document.form1;
    > var valu = form.Drop.options[form.Drop.selectedIndex].value;
    > form.Text.value = valu;
    > }
    >
    To be honest, I didn't see that there was no need for the "if" clause!
    Regarding the second point, I always make a point of giving a form a name,
    habit I suppose.
    The third point I have handled as well, albeit somewhat different and
    probably more complex!

    A little background (maybe this will help someone else with a similar
    problem) - on the form itself there are 3 objects that require user
    input/selection.

    1. dropdown (article number)
    2. textfield (quantity, default = 1)
    3. dropdown (department)

    Both dropdowns are populated with data from the db, with the first one also
    passing the second corresponding field on as well. These four variables get
    carried over and are written into the db along with the time the form was
    submitted, and the NT username (which gets spitted up so that the domain
    gets removed).

    Both dropdowns already have values inserted due to being populated, my
    problem was that if a user wanted the first value selected, he would simply
    select the quantity a,d the Dept. then submit the form - I got round this by
    making sure the first dropdown has focus as the page loads (body
    OnLoad-focus) then made sure that a value was selected calling the same
    function which you suggested from both an OnBlur and an OnChange in the
    dropdown element - this I repeated for all 3 objects and wrote the small
    function for each one. Another reason behind this is that after the first
    selection, a small text appears like "You wish to enter the following
    info..." and the record is then shown in a textfield below the selection.
    Once the quantity has been entered/chosen, then that too will then be shown
    underneath, ditto the Dept.

    Now I'm sure that a lot of people out there are laughing because there are
    more elegant ways of doing this - but I'm not that fit with JS - and if
    nothing else, it got me writing it a little (even if it was modifying
    working code!) If anyone would like the code to take a look at, or suggest
    improvements then please let me know - my main aim was for it to work as I
    wanted it to and this is now the case - for which I am deeply gratefull!

    ~p


    packet loss Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139