PDA

Click to See Complete Forum and Search --> : onchange() event


lenin
Jul 6th, 2000, 03:06 AM
Hi,
I posted a question regarding onchange() event recently (rhanks to Mark Sreeves ). Basically I wanted to return the value that was clicked on in a list box, and use this as a paramter for the populating another list box.

However I think in order to manipulate the list choice involves writing a javascript function ( which I am not keen on ). I want the value selected assigned to a variable, which can then be used as a parameter for returning records.

Can this be done?


All the sites I have looked at required writing a javascript function. Is this necessary?

Thanks in advance.

Lenin

Mark Sreeves
Jul 6th, 2000, 07:13 AM
I think you'll have to write some javascript.

So is this what you want to do?

include the selected item in an SQL select
and then populate a second list from the record set.

How much data is required for the second list?



[Edited by Mark Sreeves on 07-06-2000 at 08:15 AM]

lenin
Jul 6th, 2000, 07:23 AM
Thanks ( again ) for the reply Mark.

Yes, JScript will be fine.

The datasets are very small, between 10 - 30 records. I was going to use SQL to return all records ( via a join ) to the client and populate the second combo as required.

Can you give me some pointers on the JScript, I have never used it.

Thanks.

Lenin.

dvst8
Jul 6th, 2000, 07:57 AM
i am doing something similar....

i'm assuming you must have a form on your page...?
if so, then what I would suggest is that when onchange event fires, simulate a click of the submit button, which will submit the page to itself. then you can refresh your SQL statement based on their selection and populate another form item with the results.

i have the simulating submit click working in IE, but not in Netscape. see my thread on this topic for the simple javascript (and fix it if you can!)

dvst8

lenin
Jul 6th, 2000, 07:59 AM
dv8,
what was the thread subject?

dvst8
Jul 6th, 2000, 08:01 AM
its 3-4 threads down... the only one started by me :)

doesnt work in Netscape

/d8

Mark Sreeves
Jul 6th, 2000, 09:21 AM
The way I see it, you've got 2 options

1) either load all the data into a [client-side]javascript array and then populate the list from that.

or

2) post the selected item back to the server and re write the page.

option 1 would be the fastest
option 2 would be more 'secure' as only required data is written back to the client and more complex sql can be run.


If you can give me some indication the data for list one and list 2 I'll knock-up some code for you

lenin
Jul 6th, 2000, 01:06 PM
Mark,
option 2 is probably closer to my requirements. It seems from an initial look, JScript has a few advantages og VBScript ( paramterised functions etc. ).

Thanks for the continued help.

Lenin

lenin
Jul 6th, 2000, 02:09 PM
Hi again MArk: Think I have a version that works now.

Tried this:


<SCRIPT Language="JavaScript">
<!--
function BuildList(num)
{
thehref = num.options[num.selectedIndex].value;
alert(thehref)
}
//-->/

from the onchange event and it returns the the value clicked on. however I want to pass this value ( variable ) to another call to the database. Is this possible from within the Jscript function? Can I persist the value in a session variable?

Mark Sreeves
Jul 7th, 2000, 03:14 AM
I'll answer your last Question seperately

database has two tables

table1 and table2



table1:
id list1
1 boys names
2 girls names

table2:
id list2
1 Bert
1 fred
1 Joe
1 Mark
1 Tim
2 Betty
2 Dot
2 Mary
2 Sally



and the script:

<%@ Language=VBScript %>
<BODY BACKGROUND="rfbg.gif" BGCOLOR="#ffffff">
<HEAD>
<!--
Written by Mark Sreeves
-->
<%
'now load all the data for list 2 from table2 into the client side javascript array
dim conn
dim rst

Set Conn = Server.CreateObject("ADODB.Connection")

strSQL = "SELECT * FROM table2;"

Conn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\lenin.mdb")

Set rst = Conn.Execute(strSQL)

Response.Write("<script Language='JavaScript'>" & vbcrlf)
Response.Write("var DetailArray = new Array();")
i = 0
do while not rst.EOF
%>
DetailArray[<%=i%>]= new Array("<%=rst("id")%>","<%=rst("List2")%>");

<%
rst.MoveNext
i = i + 1
loop
rst.Close
Response.Write("</script>")
%>
<script Language="JavaScript">
var i;
function fillList2(id){
var j;
j = 1;
for(var i=0; i < DetailArray.length; i++)
{
if (DetailArray[i][0] == id)
{
document.frm1.select2.options[j].value =j;
document.frm1.select2.options[j].text = DetailArray[i][1];
j++;
};
}
}

</script>


<form name="frm1">
<SELECT name=select1 onchange="fillList2(select1.options[select1.selectedIndex].value)">

<%
strSQL = "SELECT * FROM table1;"
Set rst = Conn.Execute(strSQL)
do while not rst.EOF
'fill the combo box
%>
<OPTION value="<%=rst("id")%>"><%=rst("list1")%>
<%rst.MoveNext

loop

rst.close

conn.Close
set conn = nothing
set rst = nothing

%>


</SELECT>
<BR>
<SELECT name=select2>

<% 'load dummy options

for i = 0 to 10
Response.Write "<OPTION value='xx'>"
next

%>
</SELECT>
</form>
</BODY>
</HTML>

lenin
Jul 7th, 2000, 03:48 AM
Many thnaks Mark, I'll try it tonight.

Lenin

Mark Sreeves
Jul 8th, 2000, 12:32 PM
lenin, the code I posted was 2 scripts cobbled together.

I was rather busy on Friday so I thought I would post it to keep you going anyway!

I'll clean it up, comment it properly and repost it.

Also the line that says

if (DetailArray[0] == id)

should be

if (DetailArray[ i ][0] == id)

but without spaces. In the original posting it was interpreted as the italic tag.

I'll have to stop using i as a loop counter! :)

lenin
Jul 13th, 2000, 04:28 AM
Mark,
got this working ( eventually ). Many thanks for all the input.

Lenin.