Gossamer Forum
Home : Products : DBMan SQL : Discussion :

Re: [TIF] adding records - two select fields, one updates the other?

Quote Reply
Re: [TIF] adding records - two select fields, one updates the other? In reply to
This can be done with javascript. Follow the steps below to work it out:

1. You need two globals for loading data from Category and SubCategory table.
sub {
return { cats => $DB->table('Category')->select()->fetchall_hashref };
}
sub {
return {subcats => $DB->table('SubCategory')->select()->fetchall_hashref() };
}

2. Here is the add form

<html>
<head>
<%global_cat%>
<%global_subcat%>
<script type="text/javascript">
var subcat = new Object();
<%loop subcats%>
subcat[<%SubCatID%>] = { name: '<%escape_js SubCatName%>', cat: <%CatID%> };
<%endloop%>

function loadSub(cat) {
var subObj = document.getElementById('subcat');
while (subObj.options.length > 0) { subObj.remove(0); }
for (var k in subcat) {
if (subcat[k].cat != cat) continue;
var o = document.createElement('OPTION');
subObj.options.add(o);
o.innerHTML = subcat[k].name;
o.value = k;
}
}
window.onload = function() {
var catObj = document.getElementById('cat');
if (cat.selectedIndex >= 0) {
loadSub(cat.options[cat.selectedIndex].value);
}
}
</script>
</head>
<body>
<select id="cat" name="CatID" onchange="loadSub(this.options[this.selectedIndex].value)">
<%loop cats%><option value="<%CatID%>"><%CategoryName%></option><%endloop%>
</select>
<select id="subcat" name="SubCatID">
</select>
.........................
</body>
</html>


Hope that helps.

TheStone.

B.
Subject Author Views Date
Thread adding records - two select fields, one updates the other? timbo 9218 Nov 18, 2003, 6:41 AM
Thread Re: [timbo] adding records - two select fields, one updates the other?
assombracao 9078 Nov 19, 2003, 3:02 AM
Thread Re: [assombracao] adding records - two select fields, one updates the other?
timbo 9099 Nov 27, 2003, 12:07 PM
Thread Re: [timbo] adding records - two select fields, one updates the other?
604 9029 Nov 27, 2003, 1:30 PM
Thread Re: [TheStone] adding records - two select fields, one updates the other?
timbo 9035 Nov 27, 2003, 10:45 PM
Thread Re: [timbo] adding records - two select fields, one updates the other?
604 9085 Nov 28, 2003, 10:43 AM
Thread Re: [TheStone] adding records - two select fields, one updates the other?
timbo 9057 Nov 29, 2003, 12:47 AM
Thread Re: [timbo] adding records - two select fields, one updates the other?
604 9021 Dec 1, 2003, 10:14 AM
Thread Re: [TheStone] adding records - two select fields, one updates the other?
TIF 8463 Nov 9, 2005, 9:02 PM
Thread Re: [TIF] adding records - two select fields, one updates the other?
604 8470 Nov 10, 2005, 9:45 AM
Thread Re: [TheStone] adding records - two select fields, one updates the other?
TIF 8452 Nov 10, 2005, 9:01 PM
Post Re: [TIF] adding records - two select fields, one updates the other?
TIF 8353 Nov 15, 2005, 5:52 AM
Thread Re: [TIF] adding records - two select fields, one updates the other?
604 8314 Nov 17, 2005, 4:54 PM
Thread Re: [TheStone] adding records - two select fields, one updates the other?
TIF 8275 Nov 21, 2005, 8:49 PM
Thread Re: [TIF] adding records - two select fields, one updates the other?
604 8306 Nov 22, 2005, 12:43 PM
Post Re: [TheStone] adding records - two select fields, one updates the other?
TIF 8369 Nov 25, 2005, 8:58 AM