Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ajax dynamic combo

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
hi - have managed to get the following code
to work with my data
it consists of a make combo and a model combo
when a user selects a make the respective models are displayed.
this is different to what ive done before because it uses ajax to do the lookup.

i need to add more columns to the model combo.
at the moment it has
Model
Price

i need it to display
Model
Price
Colour
Regyear

Make combo
Code:
<%
lst_x_makeid = "<select id='x_makeid' name='x_makeid' onChange='EW_ajaxupdatecombo(this.form.x_modelid, this.options[this.selectedIndex].value);'>"
lst_x_makeid = lst_x_makeid & "<option value=''>Please Select</option>"
sSqlWrk = "SELECT `Make` FROM `stock`"
If x_makeid = "" Or IsNull(x_makeid) Then
	sSqlWrk = sSqlWrk & " WHERE 0=1"
Else
	sSqlWrk = sSqlWrk & " WHERE `Make` = '" & AdjustSql(x_makeid) & "'"
End If
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sSqlWrk, conn, 1, 2
If Not rswrk.Eof Then
	datawrk = rswrk.GetRows
	rowswrk = UBound(datawrk, 2)
	For rowcntwrk = 0 To rowswrk
		lst_x_makeid = lst_x_makeid & "<option value='" & datawrk(0, rowcntwrk) & "'"
		If CStr(datawrk(0, rowcntwrk)&"") = CStr(x_makeid&"") Then
			lst_x_makeid = lst_x_makeid & " selected"
		End If
		lst_x_makeid = lst_x_makeid & ">" & datawrk(0, rowcntwrk) & "</option>"
	Next
End If
rswrk.Close
Set rswrk = Nothing
lst_x_makeid = lst_x_makeid & "</select>"
Response.Write lst_x_makeid
sSqlWrk = "SELECT `Make` FROM `stock`"
sSqlWrk = sSqlWrk & " GROUP BY make ORDER BY `Make` Asc"
sSqlWrk = EW_Encode(TEAencrypt(sSqlWrk, EW_RANDOM_KEY))
%>
<input type="hidden" name="s_x_makeid" value="<%= sSqlWrk %>">
<input type="hidden" name="lc_x_makeid" value="1">
<input type="hidden" name="ld1_x_makeid" value="0">
<input type="hidden" name="ld2_x_makeid" value="-1">

Model combo
Code:
<%
lst_x_modelid = "<select id='x_modelid' name='x_modelid'>"
lst_x_modelid = lst_x_modelid & "<option value=''>Please Select</option>"

lst_x_modelid = lst_x_modelid & "</select>"
Response.Write lst_x_modelid
sSqlWrk = "SELECT `VehicleID`, model, `Price` FROM `stock`"
'left(Products.ProductName+SPACE(40),40)+Suppliers.CompanyName as Name, 
sSqlWrk = sSqlWrk & " WHERE (`Make`='@FILTER_VALUE')"
sSqlWrk = sSqlWrk & " ORDER BY `Model` Asc"
sSqlWrk = EW_Encode(TEAencrypt(sSqlWrk, EW_RANDOM_KEY))
%>
<input type="hidden" name="s_x_modelid" value="<%= sSqlWrk %>">
<input type="hidden" name="lc_x_modelid" value="3">
<input type="hidden" name="ld1_x_modelid" value="1">
<input type="hidden" name="ld2_x_modelid" value="2">

javascript function EW_ajaxupdatecombo
Code:
// Update a combobox with filter value by AJAX
// object_value_array format
// object_value_array[n] = option value
// object_value_array[n+1] = option text 1
// object_value_array[n+2] = option text 2
function EW_ajaxupdatecombo(obj, filter_value) {
	if (!(document.getElementsByTagName || document.all))
		return;
	try {
		var value = (obj.selectedIndex > -1) ? obj.options[obj.selectedIndex].value : null;
		for (var i = obj.length-1; i > 0; i--) {
			obj.options[i] = null;
		}
		var s = eval('obj.form.s_' + obj.name + '.value');
		//var s = eval('s_' + obj.name);
		//if (!s || s == '' || filter_value == '') return;
		if (!s || s == '') return;
		var lc = eval('obj.form.lc_' + obj.name + '.value');
		if (!lc || lc == '') return;
		var ld1 = eval('obj.form.ld1_' + obj.name + '.value');
		if (!ld1 || ld1 == '') return;
		var ld2 = eval('obj.form.ld2_' + obj.name + '.value');
		if (!ld2 || ld2 == '') return;
		var xmlHttp = EW_createXMLHttp();
		if (!xmlHttp) return;		
		xmlHttp.open('get', EW_LookupFn + '?s=' + s + '&q=' + encodeURIComponent(filter_value) +
			'&lc=' + encodeURIComponent(lc) +
			'&ld1=' + encodeURIComponent(ld1) +
			'&ld2=' + encodeURIComponent(ld2));
		xmlHttp.onreadystatechange = function() {
			//alert(xmlHttp.responseText);					
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200 &&
				xmlHttp.responseText) {
				//alert(xmlHttp.responseText);
				var object_value_array = xmlHttp.responseText.split('\r');
				for (var j=0; j<object_value_array.length-2; j=j+3) {
					EW_newopt(obj, object_value_array[j], object_value_array[j+1],
						object_value_array[j+2]);
				}
				EW_selectopt(obj, value);
			}
		}		
		xmlHttp.send(null);
	}	catch (e) {}
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top