Welcome visitor
* Login
* Register
It is currently March 19th, 2019, 7:52 am
All times are UTC [ DST ]
* Search
View unanswered posts - View active topics

FORUMS » Client Side » HTML



Post new topic Reply to topic  [ 2 posts ] 
Author Message
  Autofill a form with a select drop down menu
PostPosted: November 28th, 2009, 12:32 am 

Posts: 16
Hello, I hope you can help me. I have a dropdown select menu at the start of my form and I want to fill some of the next fields according to the selection made on this menu. How can I do it?


Top
 Profile  
 
  Re: Autofill a form with a select drop down menu
PostPosted: November 28th, 2009, 5:15 am 
User avatar

Posts: 112
This is a simple example. Hope it helps. Just let me say that it would be easier with server side language.
<script type="text/javascript">

//this array works as a database
//the index on this array
//should match the select option value
//i.e. dbarray[1] data should be about person with ID "a001"
dbarray=new Array();
dbarray[0]='~~';
dbarray[1]='john~dotdot@whosethere.net~';
dbarray[2]='jane~nomail~';
dbarray[3]='jean~email@mail.com~'
//'~' is used to delimiter each data-field
//you should one '~' per line for each field to fill


//this array is for the fields to fill
//its values correspond to the id of the fields to fill
nfields=new Array();
nfields[1]='name';
nfields[2]='mail';
function populate(x)
{
  st=dbarray[x];
  for(i=1;i<=2;i++) //number "2" is for the 2 empty fields
  {
    prtst=st.substr(0,st.search('~'));
    document.getElementById(nfields[i]).value=prtst;
    st=st.substr(st.search('~')+1);
  } 
}
</script>

<form name="form" action="" method="get">
ID <select name="id" onchange="populate(this.value);">
<option value="0"></option>
<option value="1">a001</option>
<option value="2">d210</option>
<option value="3">7e3s</option>
</select>
<br />
Name <input name="name" id="name" />
<br />
eMail <input name="mail" id="mail" />
</form>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

FORUMS » Client Side » HTML


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron