Link to home
Start Free TrialLog in
Avatar of baxtalo
baxtalo

asked on

Updating multiple records with ASP

I'm trying to update all returned records at the same time, where the checkbox is checked.
Something is missing from my code; would you please let me know how I could update the AutoStatus field of each selected record?
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/DB/Autos.mdb"))

set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT * FROM Auto_Table", conn
%>

<html>
<head>
<title>All Requests</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
}
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
}
}
// End -->
</script>

</head>
<body>

<form name="editor" method="post" action="Approval_send.asp">

<table border="1">
<tr>
<th>Employee Name</th>
<th>Emp.#</th>
<th>Auto Status</th>
<th><input type="checkbox" onClick="this.value=check(this.form.ID)"></th>
</tr>
<%
if not (rs.EOF or rs.BOF) then rs.movefirst
do until rs.EOF
%>
<tr>
<td><%=rs("EmpName")%></td>
<td><%=rs("Emp_Id")%></td>
<td><%=rs("AutoStatus")%></td>
<td><input type="checkbox" name="ID" value="<%=rs("ID") %>"></td>
</tr>
<%
rs.MoveNext
loop
rs.Close
conn.Close
%>
</table>


<br /><br />
<select name="AutoStatus">
<option value="Pending">Select Status</option>
<option value="Disapproved">Disapproved</option>
<option value="Approved">Approved</option>
</select>

<input type="hidden" name="Pipa" value="&#252">
<input name="ApprovedBy" type="hidden" id="ApprovedBy" value="<%=Request.ServerVariables("LOGON_USER")%>" />
<input type="Submit" name="Submit" value="Update" />
</center>
</form>

</body>
</html>

Open in new window

Avatar of rhawk
rhawk

Hi baxtalo,
Not sure on this, just looking quickly over your code but it looks like you are trying to update the values in the Javascrip client-side? You cnanot do that.

I would make this change...
do until rs.EOF
%>
<tr>
<td><%=rs("EmpName")%></td>
<td><%=rs("Emp_Id")%></td>
<td><%=rs("AutoStatus")%></td>
<td><input type="checkbox" name="ID_<%=rs("ID").Value%>" value="1"></td>
</tr>
<%
rs.MoveNext


Then in the receiving page set all checkflags to false. Then run through each request field name received and it begins with "ID_" then you get the ID number from the name by using RIGHT('<fieldname>',LEN(<fieldname>)-3) and set the value to true (I am assuming the database has the field as a true/false value.

Do you follow that? If not let me know and I can toss together a fast version of the 2 pages to show you.

David
DOH! I see what you are doing. You want to change them IN the HTML based on the top one.
Make this function in the Javascript:
function checkLikeMe($) {
    var x = document.getElementsByName('ID');
    for (var y = 0; y < x.length; y++)
        x[y].checked = $;
}

Now change the top input checkbox to:
<input type="checkbox" onClick="checkLikeMe(this.checked)">

That ought to do it for you.
ASKER CERTIFIED SOLUTION
Avatar of rhawk
rhawk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of baxtalo

ASKER

Thank you very much, it's going to work now. Have a great day!
One question... How are you going to know what record to update on the receiving end? I think you may not have that one yet. Do you need more code to do that as well?
Avatar of baxtalo

ASKER

Thank you so much, everything is working now. I really appreciate your help.