A
Andy Kowaluk
I have an intranet site that connects to an access database using ASP
(Javascript).
Currently, when an user attempts to enter a duplicate value into the
database it is rejected and the user is redirected to a custom
message generated by the ODBC driver (I think). How can I change this
so that the user is redirected to a message that I customise myself?
I am new to coding in Javascript. The page was created in Dreamweaver. Here
is the code. If you can help I would be grateful.
Andy
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/connBooking.asp" -->
<%
// *** Edit Operations: declare variables
// set the form action variable
var MM_editAction = Request.ServerVariables("SCRIPT_NAME");
if (Request.QueryString) {
MM_editAction += "?" + Request.QueryString;
}
// boolean to abort record edit
var MM_abortEdit = false;
// query string to execute
var MM_editQuery = "";
%>
<%
// *** Insert Record: set variables
if (String(Request("MM_insert")) == "frbook") {
var MM_editConnection = MM_connBooking_STRING;
var MM_editTable = "Bookings";
var MM_editRedirectUrl = "confirmation.htm";
var MM_fieldsStr =
"mnWeekBook|value|mnDayBook|value|mnPeriodBook|value|mnResourceBook|value|mn
UserBook|value";
var MM_columnsStr =
"WeekendingId|',none,''|Day|',none,''|Period|',none,''|Resource|',none,''|Us
ername|',none,''";
// create the MM_fields and MM_columns arrays
var MM_fields = MM_fieldsStr.split("|");
var MM_columns = MM_columnsStr.split("|");
// set the form values
for (var i=0; i+1 < MM_fields.length; i+=2) {
MM_fields[i+1] = String(Request.Form(MM_fields));
}
// append the query string to the redirect URL
if (MM_editRedirectUrl && Request.QueryString && Request.QueryString.Count
+ Request.QueryString;
}
}
%>
<%
// *** Insert Record: construct a sql insert statement and execute it
if (String(Request("MM_insert")) != "undefined") {
// create the sql insert statement
var MM_tableValues = "", MM_dbValues = "";
for (var i=0; i+1 < MM_fields.length; i+=2) {
var formVal = MM_fields[i+1];
var MM_typesArray = MM_columns[i+1].split(",");
var delim = (MM_typesArray[0] != "none") ? MM_typesArray[0] : "";
var altVal = (MM_typesArray[1] != "none") ? MM_typesArray[1] : "";
var emptyVal = (MM_typesArray[2] != "none") ? MM_typesArray[2] : "";
if (formVal == "" || formVal == "undefined") {
formVal = emptyVal;
} else {
if (altVal != "") {
formVal = altVal;
} else if (delim == "'") { // escape quotes
formVal = "'" + formVal.replace(/'/g,"''") + "'";
} else {
formVal = delim + formVal + delim;
}
}
MM_tableValues += ((i != 0) ? "," : "") + MM_columns;
MM_dbValues += ((i != 0) ? "," : "") + formVal;
}
MM_editQuery = "insert into " + MM_editTable + " (" + MM_tableValues + ")
values (" + MM_dbValues + ")";
if (!MM_abortEdit) {
// execute the insert
var MM_editCmd = Server.CreateObject('ADODB.Command');
MM_editCmd.ActiveConnection = MM_editConnection;
MM_editCmd.CommandText = MM_editQuery;
MM_editCmd.Execute();
MM_editCmd.ActiveConnection.Close();
if (MM_editRedirectUrl) {
Response.Redirect(MM_editRedirectUrl);
}
}
}
%>
<%
var reWeekBook__MMColParam = "False";
if (String(Request("MM_EmptyValue")) != "undefined" &&
String(Request("MM_EmptyValue")) != "") {
reWeekBook__MMColParam = String(Request("MM_EmptyValue"));
}
%>
<%
var reWeekBook = Server.CreateObject("ADODB.Recordset");
reWeekBook.ActiveConnection = MM_connBooking_STRING;
reWeekBook.Source = "SELECT WeekendingID FROM Weeks WHERE Completed = "+
reWeekBook__MMColParam.replace(/'/g, "''") + " ORDER BY WeekNo ASC";
reWeekBook.CursorType = 0;
reWeekBook.CursorLocation = 2;
reWeekBook.LockType = 1;
reWeekBook.Open();
var reWeekBook_numRows = 0;
%>
<%
var reResourcesBook = Server.CreateObject("ADODB.Recordset");
reResourcesBook.ActiveConnection = MM_connBooking_STRING;
reResourcesBook.Source = "SELECT Resource FROM Resources ORDER BY Sort ASC";
reResourcesBook.CursorType = 0;
reResourcesBook.CursorLocation = 2;
reResourcesBook.LockType = 1;
reResourcesBook.Open();
var reResourcesBook_numRows = 0;
%>
<%
var reUsersBook = Server.CreateObject("ADODB.Recordset");
reUsersBook.ActiveConnection = MM_connBooking_STRING;
reUsersBook.Source = "SELECT * FROM Users ORDER BY Sort ASC";
reUsersBook.CursorType = 0;
reUsersBook.CursorLocation = 2;
reUsersBook.LockType = 1;
reUsersBook.Open();
var reUsersBook_numRows = 0;
%>
<html>
<head>
<title>Resource Bookings</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
</style>
<link href="Styles/Style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="Header"> Resource Booking Form</div>
<div id="Content">
<div id="TopMenu">|<a href="http://intranet"> Intranet Home</a> |<a
href="default.asp">
Search Form</a> |<a href="bookings.asp"> Booking Form</a> |</div>
<form action="<%=MM_editAction%>" method="POST" name="frbook" id="frbook">
<table width="90%" border="0">
<tr>
<td width="25%"><strong>Week Ending</strong></td>
<td width="34%"><select name="mnWeekBook" id="mnWeekBook">
<%
while (!reWeekBook.EOF) {
%>
<option
value="<%=(reWeekBook.Fields.Item("WeekendingID").Value)%>"><%=(reWeekBook.F
ields.Item("WeekendingID").Value)%></option>
<%
reWeekBook.MoveNext();
}
if (reWeekBook.CursorType > 0) {
if (!reWeekBook.BOF) reWeekBook.MoveFirst();
} else {
reWeekBook.Requery();
}
%>
</select> </td>
<td width="45%" rowspan="7"><iframe width=174 height=189
name="gToday:normal:agenda.js" id="gToday:normal:agenda.js"
src="iflateng.htm"
scrolling="no" frameborder="0">
<ilayer>
<layer name="gToday:normal:agenda.js" src="nflateng.htm"> </layer>
</ilayer>
</iframe> </td>
</tr>
<tr>
<td><strong>Day</strong></td>
<td><select name="mnDayBook" size="1" id="mnDayBook">
<option value="Monday" selected>Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
</select> </td>
</tr>
<tr>
<td><strong>Period</strong></td>
<td><select name="mnPeriodBook" size="1" id="mnPeriodBook">
<option value="Period 1" selected>Period 1</option>
<option value="Period 2">Period 2</option>
<option value="Period 3">Period 3</option>
<option value="Period 4">Period 4</option>
<option value="Period 5">Period 5</option>
<option value="Period 6">Period 6</option>
</select></td>
</tr>
<tr>
<td><strong>Resources</strong></td>
<td><select name="mnResourceBook" id="mnResourceBook">
<%
while (!reResourcesBook.EOF) {
%>
<option
value="<%=(reResourcesBook.Fields.Item("Resource").Value)%>"><%=(reResources
Book.Fields.Item("Resource").Value)%></option>
<%
reResourcesBook.MoveNext();
}
if (reResourcesBook.CursorType > 0) {
if (!reResourcesBook.BOF) reResourcesBook.MoveFirst();
} else {
reResourcesBook.Requery();
}
%>
</select> </td>
</tr>
<tr>
<td><strong>User</strong></td>
<td> <select name="mnUserBook" id="mnUserBook">
<%
while (!reUsersBook.EOF) {
%>
<option
value="<%=(reUsersBook.Fields.Item("Username").Value)%>"><%=(reUsersBook.Fie
lds.Item("Username").Value)%></option>
<%
reUsersBook.MoveNext();
}
if (reUsersBook.CursorType > 0) {
if (!reUsersBook.BOF) reUsersBook.MoveFirst();
} else {
reUsersBook.Requery();
}
%>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Make Booking"></td>
<td><input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="frbook">
</form>
</div>
<div id="Footer">
<div align="justify">
<strong>Instructions</strong> <br>
To make a booking complete each of the fields and press the MAKE
BOOKING
button. Be sure to modify every field.
</div>
</div>
</body>
</html>
<%
reWeekBook.Close();
%>
<%
reResourcesBook.Close();
%>
<%
reUsersBook.Close();
%>
(Javascript).
Currently, when an user attempts to enter a duplicate value into the
database it is rejected and the user is redirected to a custom
message generated by the ODBC driver (I think). How can I change this
so that the user is redirected to a message that I customise myself?
I am new to coding in Javascript. The page was created in Dreamweaver. Here
is the code. If you can help I would be grateful.
Andy
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/connBooking.asp" -->
<%
// *** Edit Operations: declare variables
// set the form action variable
var MM_editAction = Request.ServerVariables("SCRIPT_NAME");
if (Request.QueryString) {
MM_editAction += "?" + Request.QueryString;
}
// boolean to abort record edit
var MM_abortEdit = false;
// query string to execute
var MM_editQuery = "";
%>
<%
// *** Insert Record: set variables
if (String(Request("MM_insert")) == "frbook") {
var MM_editConnection = MM_connBooking_STRING;
var MM_editTable = "Bookings";
var MM_editRedirectUrl = "confirmation.htm";
var MM_fieldsStr =
"mnWeekBook|value|mnDayBook|value|mnPeriodBook|value|mnResourceBook|value|mn
UserBook|value";
var MM_columnsStr =
"WeekendingId|',none,''|Day|',none,''|Period|',none,''|Resource|',none,''|Us
ername|',none,''";
// create the MM_fields and MM_columns arrays
var MM_fields = MM_fieldsStr.split("|");
var MM_columns = MM_columnsStr.split("|");
// set the form values
for (var i=0; i+1 < MM_fields.length; i+=2) {
MM_fields[i+1] = String(Request.Form(MM_fields));
}
// append the query string to the redirect URL
if (MM_editRedirectUrl && Request.QueryString && Request.QueryString.Count
MM_editRedirectUrl += ((MM_editRedirectUrl.indexOf('?') == -1)?"?":"&")0) {
+ Request.QueryString;
}
}
%>
<%
// *** Insert Record: construct a sql insert statement and execute it
if (String(Request("MM_insert")) != "undefined") {
// create the sql insert statement
var MM_tableValues = "", MM_dbValues = "";
for (var i=0; i+1 < MM_fields.length; i+=2) {
var formVal = MM_fields[i+1];
var MM_typesArray = MM_columns[i+1].split(",");
var delim = (MM_typesArray[0] != "none") ? MM_typesArray[0] : "";
var altVal = (MM_typesArray[1] != "none") ? MM_typesArray[1] : "";
var emptyVal = (MM_typesArray[2] != "none") ? MM_typesArray[2] : "";
if (formVal == "" || formVal == "undefined") {
formVal = emptyVal;
} else {
if (altVal != "") {
formVal = altVal;
} else if (delim == "'") { // escape quotes
formVal = "'" + formVal.replace(/'/g,"''") + "'";
} else {
formVal = delim + formVal + delim;
}
}
MM_tableValues += ((i != 0) ? "," : "") + MM_columns;
MM_dbValues += ((i != 0) ? "," : "") + formVal;
}
MM_editQuery = "insert into " + MM_editTable + " (" + MM_tableValues + ")
values (" + MM_dbValues + ")";
if (!MM_abortEdit) {
// execute the insert
var MM_editCmd = Server.CreateObject('ADODB.Command');
MM_editCmd.ActiveConnection = MM_editConnection;
MM_editCmd.CommandText = MM_editQuery;
MM_editCmd.Execute();
MM_editCmd.ActiveConnection.Close();
if (MM_editRedirectUrl) {
Response.Redirect(MM_editRedirectUrl);
}
}
}
%>
<%
var reWeekBook__MMColParam = "False";
if (String(Request("MM_EmptyValue")) != "undefined" &&
String(Request("MM_EmptyValue")) != "") {
reWeekBook__MMColParam = String(Request("MM_EmptyValue"));
}
%>
<%
var reWeekBook = Server.CreateObject("ADODB.Recordset");
reWeekBook.ActiveConnection = MM_connBooking_STRING;
reWeekBook.Source = "SELECT WeekendingID FROM Weeks WHERE Completed = "+
reWeekBook__MMColParam.replace(/'/g, "''") + " ORDER BY WeekNo ASC";
reWeekBook.CursorType = 0;
reWeekBook.CursorLocation = 2;
reWeekBook.LockType = 1;
reWeekBook.Open();
var reWeekBook_numRows = 0;
%>
<%
var reResourcesBook = Server.CreateObject("ADODB.Recordset");
reResourcesBook.ActiveConnection = MM_connBooking_STRING;
reResourcesBook.Source = "SELECT Resource FROM Resources ORDER BY Sort ASC";
reResourcesBook.CursorType = 0;
reResourcesBook.CursorLocation = 2;
reResourcesBook.LockType = 1;
reResourcesBook.Open();
var reResourcesBook_numRows = 0;
%>
<%
var reUsersBook = Server.CreateObject("ADODB.Recordset");
reUsersBook.ActiveConnection = MM_connBooking_STRING;
reUsersBook.Source = "SELECT * FROM Users ORDER BY Sort ASC";
reUsersBook.CursorType = 0;
reUsersBook.CursorLocation = 2;
reUsersBook.LockType = 1;
reUsersBook.Open();
var reUsersBook_numRows = 0;
%>
<html>
<head>
<title>Resource Bookings</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
</style>
<link href="Styles/Style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="Header"> Resource Booking Form</div>
<div id="Content">
<div id="TopMenu">|<a href="http://intranet"> Intranet Home</a> |<a
href="default.asp">
Search Form</a> |<a href="bookings.asp"> Booking Form</a> |</div>
<form action="<%=MM_editAction%>" method="POST" name="frbook" id="frbook">
<table width="90%" border="0">
<tr>
<td width="25%"><strong>Week Ending</strong></td>
<td width="34%"><select name="mnWeekBook" id="mnWeekBook">
<%
while (!reWeekBook.EOF) {
%>
<option
value="<%=(reWeekBook.Fields.Item("WeekendingID").Value)%>"><%=(reWeekBook.F
ields.Item("WeekendingID").Value)%></option>
<%
reWeekBook.MoveNext();
}
if (reWeekBook.CursorType > 0) {
if (!reWeekBook.BOF) reWeekBook.MoveFirst();
} else {
reWeekBook.Requery();
}
%>
</select> </td>
<td width="45%" rowspan="7"><iframe width=174 height=189
name="gToday:normal:agenda.js" id="gToday:normal:agenda.js"
src="iflateng.htm"
scrolling="no" frameborder="0">
<ilayer>
<layer name="gToday:normal:agenda.js" src="nflateng.htm"> </layer>
</ilayer>
</iframe> </td>
</tr>
<tr>
<td><strong>Day</strong></td>
<td><select name="mnDayBook" size="1" id="mnDayBook">
<option value="Monday" selected>Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
</select> </td>
</tr>
<tr>
<td><strong>Period</strong></td>
<td><select name="mnPeriodBook" size="1" id="mnPeriodBook">
<option value="Period 1" selected>Period 1</option>
<option value="Period 2">Period 2</option>
<option value="Period 3">Period 3</option>
<option value="Period 4">Period 4</option>
<option value="Period 5">Period 5</option>
<option value="Period 6">Period 6</option>
</select></td>
</tr>
<tr>
<td><strong>Resources</strong></td>
<td><select name="mnResourceBook" id="mnResourceBook">
<%
while (!reResourcesBook.EOF) {
%>
<option
value="<%=(reResourcesBook.Fields.Item("Resource").Value)%>"><%=(reResources
Book.Fields.Item("Resource").Value)%></option>
<%
reResourcesBook.MoveNext();
}
if (reResourcesBook.CursorType > 0) {
if (!reResourcesBook.BOF) reResourcesBook.MoveFirst();
} else {
reResourcesBook.Requery();
}
%>
</select> </td>
</tr>
<tr>
<td><strong>User</strong></td>
<td> <select name="mnUserBook" id="mnUserBook">
<%
while (!reUsersBook.EOF) {
%>
<option
value="<%=(reUsersBook.Fields.Item("Username").Value)%>"><%=(reUsersBook.Fie
lds.Item("Username").Value)%></option>
<%
reUsersBook.MoveNext();
}
if (reUsersBook.CursorType > 0) {
if (!reUsersBook.BOF) reUsersBook.MoveFirst();
} else {
reUsersBook.Requery();
}
%>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Make Booking"></td>
<td><input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="frbook">
</form>
</div>
<div id="Footer">
<div align="justify">
<strong>Instructions</strong> <br>
To make a booking complete each of the fields and press the MAKE
BOOKING
button. Be sure to modify every field.
</div>
</div>
</body>
</html>
<%
reWeekBook.Close();
%>
<%
reResourcesBook.Close();
%>
<%
reUsersBook.Close();
%>