Hi all. Can you guys post a sample code of a js script access to database for example, a login trying to check a user name and password. And how do that works, if it stores all data in a txt file or what?
@yearbassNov 01.2006 — #there is no way to access database using javascript (in this case is clientside javascript). but you can access XML data using AJAX.
@semi-sentientNov 01.2006 — #Sample code using AJAX and ASP...
JavaScript (AJAX): [code=php]//*****************************************************************************************/ // Populates the "to" and "from" fields once a data source and table name are selected... //*****************************************************************************************/ function getRecordCount() { var tblName = element["table_value"].value; // table/query name var connStr = element["connection_string"].value; // connection string
// Do some validation before proceeding (abort if any are blank)... if (connStr == "" || tblName == "") { return; }
// Instantiate our request object... objReq = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
if (objReq) { objReq.onreadystatechange = function() { if (objReq.readyState == 4) { // 4 = COMPLETE if (objReq.status == 200) { if (objReq.responseText.indexOf("Error:") == -1) { // Update the "to" and "from" field on the form... element["from"].value = "1"; element["to"].value = objReq.responseText; } else { alert(objReq.responseText); } } else { alert("There was a problem retrieving a list of records to process."); } } } objReq.open("GET", "http://servername/sql_query.asp?tblName=" + tblName + "&connStr=" + connStr, true); objReq.send(null); } else { alert("There was a problem retrieving a list of records to process."); } }[/code]
ASP (sql_query.asp): [code=php]<% ' Declare variables to be used. Dim connStr, tblName, objConn, objRS, strSQL, rsArray, rsCount
' Get querystring values for connection string and table name connStr = Request.QueryString("connStr") tblName = Request.QueryString("tblName")
On Error Resume Next
' Establish/open connection to the database. Set objConn = Server.CreateObject("ADODB.Connection") objConn.ConnectionString = connStr objConn.Open
' Retrieve the record count... Set objRS = Server.CreateObject("ADODB.Recordset") objRS.Open tblName, objConn
If Not objRS.EOF Then rsArray = objRS.GetRows() rsCount = UBound(rsArray, 2) + 1 End If
Response.Write(rsCount)
' Clean up objRS.Close Set objRS = Nothing Set objConn = Nothing
If Err <> 0 Then Response.Write("Error: could not retrieve a record count...") End If %>[/code]