...

View Full Version : help! -- dropdowns and populating text boxes



xo_tiff_xo
12-17-2004, 07:01 AM
Hi everyone! I don't know much about javascript, but I think I need to use it for what I want to do for my project.

I have a form in which I have a drop down box with ID numbers in it. When I select an ID number, I want to be able to populate the form with variables associated with the ID number. For example, when I select ID number 1, I want to write John in textbox1, John's last name, Doe, in textbox2, John Doe's address in textbox3.

Is there a way to split up the option values in order to place them into seperate textboxes? Right now, I have one long string that writes into one textbox.

This is what I have so far:

<%
strSQL = "SELECT * FROM tblStudent"
Set rs = conn.execute(strSQL)

Do While Not rs.EOF
%>

<option value="<%=rs("StudentID")%>&nbsp;<%=rs("FirstName")%>&nbsp;<%=rs("LastName")%>"><%= rs("StudentID")%></option>
<%
rs.MoveNext
Loop
%>


and then I have this so that it updates the textbox when an ID is selected:

<td><input type="text" name="FirstName" size="30" tabindex="2"></td>
<script language="javascript">
function MoveID()
{
var FirstName = document.me.IDNumber.value;
document.me.FirstName.value = FirstName;
}
</script>

Is there a way to take the LastName from the option value and write it to the LastName textbox?

Thanks in advance!

xo_tiff_xo
12-17-2004, 07:06 AM
i think i found the solution to my original question. now i'm just wondering how to loop through the case function now? what is the syntax?

someone please help.

<script language="javascript">
function fillFields(Select)
{
var Form1 = Select.form;
switch (Select.options[Select.selectedIndex].value)
{
case '<%= rs("StudentID")%>':Form1.FirstName.value='<%= rs("FirstName")%>';Form1.LastName.value='<%= rs("LastName")%>';break;
}
}
</script>

<select size="1" name="IDNumber" tabindex="1" onchange="fillFields(this)">
<option value=" ">&nbsp;</option>
<%
Do While Not rs.EOF
%>
<option value="<%= rs("StudentID")%>"><%= rs("StudentID")%></option>
<%
rs.MoveNext
Loop
%>
</select>

glenngv
12-18-2004, 07:15 AM
Try this:


<%
dim strOptions, strID
%>
<script language="javascript">
function fillFields(Select)
{
var Form1 = Select.form;
switch (Select.options[Select.selectedIndex].value)
{
<%
while not rs.EOF and not rs.BOF
strID = rs("StudentID")
strOptions = strOptions & "<option value=""" & strID & """>" & strID & "</option>" & vbCrLf
%>
case "<%= strID%>":Form1.FirstName.value="<%= rs("FirstName")%>";Form1.LastName.value="<%= rs("LastName")%>";break;
<%
rs.Movenext
wend
%>
default: //for the first empty option, reset fields(?)
Form1.FirstName.value="";
Form1.LastName.value="";
break;
}
}
</script>

<select size="1" name="IDNumber" tabindex="1" onchange="fillFields(this)">
<option value="">&nbsp;</option>
<%=strOptions%>
</select>

xo_tiff_xo
12-19-2004, 12:55 AM
Thanks so much, Glenn! The code is exactly what I wanted it to do. You're a genius :thumbsup: !! Take care.

draco1938
12-11-2006, 05:33 PM
I have a very similar problem, if not the same problem, as this however I cannot get the code to work. Here is what I have copy pasted / edited so far....

<?PHP
$username="ommitted";
$password="ommitted";
$database="GasPrices";

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");
$query= "SELECT * FROM gastable ORDER BY Name ASC";
$result=mysql_query($query)or die( mysql_error());
?>
</?PHP



<%
dim strOptions, Name
%>
<HTML>
<HEAD>
<script language="javascript">
function fillFields(Select)
{
var Form1 = Select.form;
switch (Select.options[Select.selectedIndex].value)
{
<%
while not rs.EOF and not rs.BOF
Name = rs("StudentID")
strOptions = strOptions & "<option value=""" & Name & """>" & Name & "</option>" & vbCrLf
%>
case "<%= Name%>":Form1.FirstName.value="<%= rs("FirstName")%>";Form1.LastName.value="<%= rs("LastName")%>";break;
<%
rs.Movenext
wend
%>
default: //for the first empty option, reset fields(?)
Form1.FirstName.value="";
Form1.LastName.value="";
break;
}
}
</script>
</HEAD>
<BODY>
<select size="1" name="stationname" tabindex="1" onchange="fillFields(this)">
<option value="">&nbsp;</option>
<%=strOptions%>
</select>
</BODY>
</HTML>


Basically I am trying to generate a list of the gas stations in my table in the drop down box. The list will be a concatenated listing of station name / address. For example:

name = Chevron
address = 1 Main Street

concatenated version = "Chevron - 1 Main Street"

That is how they will appear in the drop down box. Once the user selects one of those it will split it into the appropriate name / address boxes. For example:

they select: "Chevron - 1 Main Street"
box1 value - "Chevron"
box2 value - "1 Main Street"

Someone please help I am ripping my hair out!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum