Avatar of ZKM128
ZKM128
 asked on

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.

The ASP page on the browser
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
%>

Open in new window


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() { 
        ?
    }

Open in new window

JavaScriptASPVB Script

Avatar of undefined
Last Comment
ZKM128

8/22/2022 - Mon
SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Big Monty

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ZKM128

ASKER
Thank you experts,
Big Monty, if I use the code shown below,
window.location = 'somePage.asp';

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.
ZKM128

ASKER
I followed the instruction given on: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
and It worked :-)

For the image button:
<img src="Button.jpg" onclick="DataPrinted(<%=(rsMembers.Fields.Item("IDNumber").Value)%>)"/>

Open in new window


"DataPrinted" javascript function:
<script type="text/javascript">
	
	function DataPrinted(IDNumber) {
	
		xmlhttp= new XMLHttpRequest();
		xmlhttp.open("POST","UpdateDataPrintStatus.asp",true);
 		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 		xmlhttp.send("ID="+IDNumber);
	
        }
	
</script>

Open in new window


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
%>

Open in new window


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?
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ZKM128

ASKER
Thank you
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck