Updating database records in classic ASP using javascript
Hello,
How do I update database record (MS Access) in classic ASP when javascript onclick event is triggered on the image button?
Here's the detailed description of what I'm trying to achieve.
I have created a recordset "rsMembers" in my (classic) ASP page for displaying a record stored in the MS Access database table and bound the table field data on the ASP page for the initial display of the personal details record data to the user when the page is loaded on the browser. The ASP page looks like the image shown below.
What I want to achieve is when the user clicks the blue circular button on the page, I want the javascript onclick event script to trigger/execute vbscript ASP update codes shown below.
<% Set conn = Server.CreateObject("ADODB.Connection") conn.Open Session("DSN=MembersSampleDSN") Set rsMembers = Server.CreateObject("ADODB.Recordset") rsMembers.Open "MembersTable", conn, 1, 3, 2 ' Update the value of 'DataPrinted' field for the currently displayed record rsMembers ("DataPrinted") = "Yes" rsMembers .Update rsMembers .Close%>
I know how to update MS Access database records using ASP when the ASP page is loaded or when the submit button inside a form is pressed but I don't know how to achieve this using javascript -- onclick event of the image button. That is, I want to know the way of changing the database record without submitting/opening the ASP page.
Is this possible? Please show me the correct javascript codes for executing particular block of vb codes shown above if possible.
<img id="BlueButton" onclick="DataPrinted" /> function DataPrinted() { ? }
Will it redirect me to the 'somePage.asp' or will it just run the vb codes stored in the 'somePage.asp' quietly in the background and update the "DataPrinted" field of the currently displayed record?
If window.location = 'somePage.asp'; has to send/redirect the user to the somePage.asp then I think I have to use the ajax method. I'll try to learn how to use jquery and ajax to accomplish this.
And for the UpdateDataPrintStatus.asp which actually updates the "DataPrinted" field value:
<% Set conn = Server.CreateObject("ADODB.Connection") conn.Open Session("DSN=MembersSampleDSN") Set rsMembers = Server.CreateObject("ADODB.Recordset") Dim rsMembersSQL, IDNumber IDNumber = Request.Form("ID") rsMembersSQL = "MembersTable WHERE IDNumber = " & IDNumber rsMembers.Open rsMembersSQL, conn, 1, 3, 2 ' Update the value of 'DataPrinted' field for the currently displayed record rsMembers("DataPrinted") = "Yes" rsMembers.Update rsMembers.Close%>
In fact, I have encountered an error when I have omitted "xmlhttp= new XMLHttpRequest();" in the DataPrinted(IDNumber) javascript function. I have simply put this code on the top of the javascript function but, is this the correct place to put this code? Did I do everything correctly?
Big Monty, if I use the code shown below,
Open in new window
Will it redirect me to the 'somePage.asp' or will it just run the vb codes stored in the 'somePage.asp' quietly in the background and update the "DataPrinted" field of the currently displayed record?If window.location = 'somePage.asp'; has to send/redirect the user to the somePage.asp then I think I have to use the ajax method. I'll try to learn how to use jquery and ajax to accomplish this.