View Full Version : How to display the details in another combo box?
yeen83
10-07-2002, 07:58 AM
i have a combo box for user to type in the Item no and it will display the product name in another textbox.Will it able to do so without submitting the form?
Mhtml
10-07-2002, 09:19 AM
Wow, I suprised myself with this one. I created this off the top of my head and I haven't read any tutorials yet. Just been playing with JS.
Anyway do you mean like this? Or are the values going to be out of a database?
<body onload="DisplayProduct(select.value)">
<script language="JavaScript">
function DisplayProduct(Pno){
var ProdNo = new Array();{
ProdNo[1] = 'Pocket toaster'
ProdNo[2] = 'Mini toaster'
ProdNo[3] = 'Standard toaster'
ProdNo[4] = 'Super toaster'
ProdNo[5] = 'Mega toaster'}
document.all.productname.value = ProdNo[Pno]
}
</script>
<select onChange="DisplayProduct(this.value)" name="select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select><textarea name="productname"></textarea>
You can easily make the asp script create this array for each value in a db if that is what you need.
yeen83
10-09-2002, 08:24 AM
yah something similar to this.But how am i going to do it by retreiving it from db?
glenngv
10-09-2002, 08:29 AM
http://www.activeserverpages.com/learn/dblist.asp
Alekz
10-09-2002, 10:53 AM
Hi,
Dumping DB data as a JavaScript array is only suitable for small tables... If You have 20 000 products for example it would be a nightmare.
The other technique is to contact the server and get the data You need without reloading the page - an RPC call...
This can be done in several ways:
1. Submit a hidden field through a hidden frame or IFRAME - the main page is not reloaded, just the hidden frame.
2. Use XMLHTTP - it is available in IE5.5+ (I could be wrong here), Netscape 6.1+ And Mozilla 1+
3. For IE5 You vould use the #download behavior - it can be atached to almost every HTML element
4. You could use hidden IFRAME as HTTP buffer (with IE4 and Netscape 6.0 that's the only way I know, except proxies)
5. For Netscape 4 - opening a socket connection through java.io.net is allowed
6. Use a proxy object, such as a java applet, flash, shockwave movie or even ActiveX to contact the server without reloading the page. That's the method Microsoft used for data binding in HTML forms.
Alex
yeen83
10-09-2002, 10:54 AM
Sorry to ask how to put database values into array?when i alreadi retreive value from database den add into array.
Alekz
10-09-2002, 11:18 AM
Hi,
As You posted in the ASP forum, I suppose You are using ASP...
Here's a quick sample:
Let's suppose You have a table called Products with 2 fields - ID and Name.
And somewhere in Your code You have defined a ConnStr variable that describes the conection to Your database.
Then:
theSQL = "SELECT * FROM Products"
Set RS = Server.CreateObject ("ADODB.Recordset")
RS.Open theSQL, ConnStr
If Not(RS.BOF And RS.EOF) Then
Response.Write "<script language=""JavaScript"">" & vbCrLf
Response.Write "var myArr = new Array();" & vbCrLf
RS.MoveFirst
While Not(RS.EOF)
Response.Write "myArr[myArr.length] = new Array('" & RS("ID") & "','" & RS("Name") & "');" & vbCrLf
RS.MoveNext
Wend
Response.Write "</script>" & vbCrLf
End If
RS.Close
Set RS = Nothing
The resulting JavaScript code will look like this:
var myArr = new Array();
myArr[myArr.length] = new Array('1','Product 1');
myArr[myArr.length] = new Array('2','Product 2');
...................
So in a for loop for example myArr[i][0] will be the ID of Your product and myArr[i][1] will be the name of the product.
Hope this helps,
Alex
yeen83
10-14-2002, 05:13 AM
Hi,
So if i don't use an array.What should i do then? How you do a RPC call?Do you have an example because i dont't quite understand to do it coding form.Thanks.
Originally posted by Alekz
Hi,
Dumping DB data as a JavaScript array is only suitable for small tables... If You have 20 000 products for example it would be a nightmare.
The other technique is to contact the server and get the data You need without reloading the page - an RPC call...
This can be done in several ways:
1. Submit a hidden field through a hidden frame or IFRAME - the main page is not reloaded, just the hidden frame.
2. Use XMLHTTP - it is available in IE5.5+ (I could be wrong here), Netscape 6.1+ And Mozilla 1+
3. For IE5 You vould use the #download behavior - it can be atached to almost every HTML element
4. You could use hidden IFRAME as HTTP buffer (with IE4 and Netscape 6.0 that's the only way I know, except proxies)
5. For Netscape 4 - opening a socket connection through java.io.net is allowed
6. Use a proxy object, such as a java applet, flash, shockwave movie or even ActiveX to contact the server without reloading the page. That's the method Microsoft used for data binding in HTML forms.
Alex
Mhtml
10-14-2002, 08:17 AM
Why not cheat and do it like this?
<%
sql_select="SELECT * FROM Products Order By ProductId"
sConnString="DRIVER={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & Server.MapPath("YOUR_DBHERE.mdb") & ";"
Set Conn = Server.CreateObject("ADODB.connection")
Set rs = Server.CreateObject("ADODB.Recordset")
Conn.Open sConnString
rs.Open sql_select,Conn
Response.Write("<select name=SELECT_NAME>")
Do while Not rs.eof
Response.Write("<option value="&rs("ProductId")">"&rs("ProductName")&"</option>")
rs.MoveNext
loop
Response.Write("</select>")
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
%>
Alekz
10-14-2002, 08:24 AM
Hi,
If You do not need calls to server to be multithreaded, You can use the simple model - create a hidden frame and reload it...
Here's a sample HTML for the framed page
<html>
<frameset rows="0,*">
<frame name="RPC" src="RPC.asp">
<frame name="main" src="main.asp">
</frameset>
</html>
The frame named "RPC" will be hidden
Now from the main frame You can reload that hidden frame with some parameter in the URL. For exampl, if Your combo is named "Products" then
<select name="Products" size="1" onchange="javascript: parent.frames['RPC'].document.location='RPC.asp?productID=' + document.frm.Products.options[document.frm.Products.selectedIndex].value">
Each time the users selects something in the "Products" combo, the hidden frame will reload the PRC.asp with productID parameter passed in the URL.
In RPC.asp, You'll have to get the parameter and output a JavaScript array again, but only with values corresponding to the requested productID.
Catch the onload event of the RPC.asp and fill the second combo with the values from the array.
For a more flexible RPC (no need of hidden frames, multithread) Look at this thread
http://www.codingforums.com/showthread.php?s=&threadid=7135
There's a sample with some restrictions though... I've solved most of them last days...
In this case You'll have to output data from ASP as a comma delimited string for example, then parse that string in the callback function and fill the second combo with appropriate values...
Hope this helps,
Alex
Darksbane
11-24-2002, 08:03 PM
Each time the users selects something in the "Products" combo, the hidden frame will reload the PRC.asp with productID parameter passed in the URL.
In RPC.asp, You'll have to get the parameter and output a JavaScript array again, but only with values corresponding to the requested productID.
Catch the onload event of the RPC.asp and fill the second combo with the values from the array.
OK I am trying to do this with Iframes and am with you up until the catching of the onload of the frame. How do you pass the info which the frame/iframe gets back to the original page?
Alekz
11-29-2002, 07:56 AM
Hi,
There's two ways - the easy one and the hard one :)
- The easy way can be implemented if You can alter the server side scripts. When outputting the content for the hidden IFRAME just add <body onload="javascript: parent.somefunction()"> - this way IFRAME can call a function You'll define in the main document, thus alerting Your code the data is loaded. You can even write the function that will refill the second combo box inside the IFRAME content document. Keep in mind cross-frame scripting security issues - both the document loaded in main window and in IFRAME have to be loaded from the same domain.
- The hard way is to catch the moment when the IFRAME content is loaded, client side and then use innerHTML to get the data. This is possible for all browsers, except Netscape 4.x (no IFRAMES at all), but the approach is different almost for any browser version. Yes, sometimes even for minor versions...
BTW, RPC model can be implemented for Netscape 4.x too - let me know if this is an issue...
Alex
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.