Home Using AJAX XMLHttpRequest to send Form input values to a server, execute an SQL query using said values and return the results
Reply: 0

Using AJAX XMLHttpRequest to send Form input values to a server, execute an SQL query using said values and return the results

user2825
1#
user2825 Published in April 22, 2018, 10:04 pm

I am new to using AJAX and Classic ASP and would like some help/advice.

I have 2 input boxes, one for a start date and one for an end date, and a button on my web page. When the user enters 2 dates and clicks the button I want a function to run in javascript that uses AJAX XMLHttpRequest to take the values entered running an ASP Classic script, connecting to my SQL database. There, the values entered in the input boxes needs to be used in my SQL query to determine the period of time from which I want to obtain results, passing the results back to my javascript.

I cannot get the program to run correctly, and I am unsure if the problem is my javascript code or my ASP code..or both.

The variables return blank.

Please see below:

HTML:

<div id="form_container">

<input type="text" id="sDate" name="sDate" />
<input type="text" id="eDate" name="eDate" />
<br>
<button type="button" id="btnDates" onclick="sendData()">Update</button>

<div id="results">Results here...</div>
</div>

Javascript:

var xmlhttp;
function sendData(sDate, eDate) {   
xmlhttp = GetXmlHttpObject();
if (xmlhttp == null) {
alert ("Your browser does not support AJAX!");
return;
}

var url = "getDates.asp?sDate="+sDate+"&eDate="+eDate;
xmlhttp.open("GET",url,true);
 xmlhttp.send(null);

xmlhttp.onreadystatechange=stateChanged 
function stateChanged() { 
if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete")
{ 
document.getElementById("results").innerHTML=xmlhttp.responseText 
}}

}

getDates.asp:

<%
    Dim strPwd, strUsr
    Dim strDomain, strQuery
    Dim objConn, objComm, objRs
    Dim start_Date, end_Date

    strDomain = "..."
    strUsr = "..."
    strPwd = "..."

    start_Date = Request.QueryString("sDate")
    end_Date = Request.QueryString("eDate")

    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.open "..."
    Set objComm = CreateObject("ADODB.Command")
    Set objComm.ActiveConnection = objConn

strQuery = "SELECT [TICKET_ID] FROM [TICKET] WHERE 
LAST_UPDATED between '"&sDate&"' and '"&eDate&"'
ORDER BY [TICKET_ID]DESC"

objComm.CommandText = strQuery
Set objRs = objComm.Execute

    Do While Not objRs.EOF
            For i = 0 To objRs.Fields.Count - 1 Step 1
                results = objRs.Fields(i)
            Next

            objRs.MoveNext
        Loop

    Response.Write("Start date: "&start_Date)
    Response.Write("End date: "&end_Date)
Response.Write("Results: "&results)

    objRs.Close
    Set objRs = nothing
    Set objConn = nothing   
    Set objComm = nothing           
%>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.251655 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO