www.webdeveloper.com
Results 1 to 6 of 6

Thread: Array vars displayed based on select option

  1. #1
    Join Date
    Mar 2005
    Posts
    3

    Array vars displayed based on select option

    Hello,

    I have a select list form field on my page, and I would like it to control what is displayed in a text box next to it. In a simple form, it would be something like a list of users, and then upon selection of a user in the dropdown, their email would display in the text box (or div , etc) next to it.

    The select list is being generated from a DB table already, and each option has a user_id as the value (ex. 1, 2, 3,e etc..). I also build an array of user data (in PHP) which includes the user_id and user_email that I need to somehow pass on to JS.

    I am assuming I need to build some sort of JS array code out of my array data in order for my select list to be able to display a certain array value? What would that look like?

    If I had a select list created, and a JS array, how do I link the two together to alter the value of the email textbox upon onchange?

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Just drop it straight into the html:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>email selection</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <form action="#" name="form1">
    <div>
    <select name="select1" onchange="document.forms['form1'].input1.value=this[this.selectedIndex].value;">
    <option value="">select a person</option>
    <option value="tom@home.com">Tom</option>
    <option value="dick@home.com">Dick</option>
    <option value="harry@home.com">Harry</option>
    </select>
    <input type="text" name="input1">
    </div>
    </form>
    </body>
    </html>
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Mar 2005
    Posts
    3
    Well, the example I described is just simplified. Really, there would be email, password, fullname, etc. in text boxes for each entry in the select list that would all change when the select was changed.

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Like this?
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>email selection</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    <!--
    function Info(idx) {
    var f=document.forms['form1'];
    var person=f.select1[idx].value;
    var info=[
    ['no person selected','',''],
    ['Tom Jones','tom@home.com','TomPassword'],
    ['Dick','dick@home.com','DickPassword'],
    ['Harry','harry@home.com','HarryPassword']
    ];
    f.textarea1.value=info[person][0]+'\n'+info[person][1]+'\n'+info[person][2];
    }
    //-->
    </script>
    </head>
    <body>
    <form action="#" name="form1">
    <div>
    <select name="select1" onchange="Info(this.selectedIndex);">
    <option value="0">select a person</option>
    <option value="1">Tom</option>
    <option value="2">Dick</option>
    <option value="3">Harry</option>
    </select>
    <textarea rows="3" cols="20" name="textarea1">
    </textarea>
    </div>
    </form>
    </body>
    </html>
    Last edited by Fang; 03-08-2005 at 01:20 PM.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    Mar 2005
    Posts
    3
    Ah, that is nearly it! Except I will be placing each array element into a seperate input text box instead of one textarea. Nice. I will give this a rip. Thanks!

    Is there a common term for this type of JS code that I should have done a search for? I was looking for "onchange array select" on google, etc.

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    A search with "onchange selectedIndex" would find it, but if you had known that you could have scripted it yourself.
    At least 98% of internet users' DNA is identical to that of chimpanzees

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