Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnChange event without submitting form.

Status
Not open for further replies.

ewylam

Programmer
Jan 25, 2005
16
US
Ok, I need some help here. I have a form with a couple of dropdown menus that are populated dynamically. The uses needs to select an option from the first menu before the second is populated with options. I have successfully complete this with an "OnChange" event and everything works fine. The problem comes when I added the Submit button and the code to actually insert all the data into the database. Now every time the "OnChange" event happens the from tries to insert the incomplete data into the database. I would greatly appreciate your help in finding a solution to this problem. Below is my code. Thanks.

<cfset CurrentPage=GetFileFromPath(GetTemplatePath())>
<cfif IsDefined("FORM.MM_InsertRecord") AND FORM.MM_InsertRecord EQ "form">
<cfquery datasource="ExerciseWebApplication">
INSERT INTO Users ("Lookup to RankID", FirstName, LastName, "Lookup to GroupID", "Lookup to UnitID", DutyPhone, HomePhone, DEROS, EmailAddress, EetTrainingDate, "Lookup to BadgeTypeID", BadgeNumber, GBSQualified, GBSTrainingDate, "Lookup to AccessLevelID", UserName)
VALUES (
<cfif IsDefined("FORM.RankID") AND #FORM.RankID# NEQ "">
#FORM.RankID#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.FirstName") AND #FORM.FirstName# NEQ "">
'#FORM.FirstName#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.LastName") AND #FORM.LastName# NEQ "">
'#FORM.LastName#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.GroupID") AND #FORM.GroupID# NEQ "">
#FORM.GroupID#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.UnitID") AND #FORM.UnitID# NEQ "">
#FORM.UnitID#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.DutyPhone") AND #FORM.DutyPhone# NEQ "">
'#FORM.DutyPhone#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.HomePhone") AND #FORM.HomePhone# NEQ "">
'#FORM.HomePhone#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.DEROS") AND #FORM.DEROS# NEQ "">
'#FORM.DEROS#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.EmailAddress") AND #FORM.EmailAddress# NEQ "">
'#FORM.EmailAddress#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.EetTrainingDate") AND #FORM.EetTrainingDate# NEQ "">
'#FORM.EetTrainingDate#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.BadgeTypeID") AND #FORM.BadgeTypeID# NEQ "">
#FORM.BadgeTypeID#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.BadgeNumber") AND #FORM.BadgeNumber# NEQ "">
#FORM.BadgeNumber#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.GBSQualified")>
'Y'
<cfelse>
'N'
</cfif>
,
<cfif IsDefined("FORM.GBSTrainingDate") AND #FORM.GBSTrainingDate# NEQ "">
'#FORM.GBSTrainingDate#'
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.AccessLevelID") AND #FORM.AccessLevelID# NEQ "">
#FORM.AccessLevelID#
<cfelse>
NULL
</cfif>
,
<cfif IsDefined("FORM.UserName") AND #FORM.UserName# NEQ "">
'#FORM.UserName#'
<cfelse>
NULL
</cfif>
)
</cfquery>
<cflocation url="registration_success.cfm">
</cfif>[/color red]
<cfquery name="GetRanks" datasource="#DSN#">
SELECT RankID, RankShort
FROM Ranks
</cfquery>
<cfquery name="GetGroups" datasource="#DSN#">
SELECT GroupID, GroupShort
FROM Groups
</cfquery>
<cfquery name="GetBadgeTypes" datasource="#DSN#">
SELECT BadgeTypeID, BadgeTypeShort
FROM BadgeTypes
</cfquery>
<cfquery name="GetAccessLevels" datasource="#DSN#">
SELECT AccessLevelID, AccessLevel
FROM AccessLevels
</cfquery>
<cfquery name="GetMonths" datasource="ExerciseWebApplication">
SELECT Month, MonthShort
FROM Months
</cfquery>
<cfquery name="GetYears" datasource="ExerciseWebApplication">
SELECT Years
FROM Years
</cfquery>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>User Registration</title>
<link href="/Exercise Web Application/CssFiles/SiteStyles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form id="form" name="form" method="POST" action="<cfoutput>#CurrentPage#</cfoutput>">
<table width="80%" id="UserInformation">
<tr>
<td><label class="InputLabel">Rank:</label></td>
<td><select name="RankID">
<cfoutput query="GetRanks">
<option value="#GetRanks.RankID#" <cfif (isDefined("form.RankID") AND form.RankID EQ GetRanks.RankID)>selected</cfif>>#GetRanks.RankShort#</option>
</cfoutput>
</select></td>
</tr>
<tr>
<td><label class="InputLabel">First Name:</label></td>
<td><input name="FirstName" type="text" <cfif (isDefined("form.FirstName") AND form.FirstName NEQ "")>
value="<cfoutput>#form.FirstName#</cfoutput>" maxlength="255"
<cfelse>
maxlength="255"
</cfif>/></td>
</tr>
<tr>
<td><label class="InputLabel">Last Name:</label></td>
<td><input name="LastName" type="text" <cfif (isDefined("form.LastName") AND form.LastName NEQ "")>
value="<cfoutput>#form.LastName#</cfoutput>" maxlength="255"
<cfelse>
maxlength="255"
</cfif>/></td>
</tr>
<tr>
<td><label class="InputLabel">Group:</label></td>
<td><select name="GroupID" onChange="this.form.submit()">
<option value=""></option>
<cfoutput query="GetGroups">
<option value="#GetGroups.GroupID#" <cfif (isDefined("form.GroupID") AND form.GroupID EQ GetGroups.GroupID)>selected</cfif>>#GetGroups.GroupShort#</option>
</cfoutput>
</select></td>
</tr>
<tr>
<td><label class="InputLabel">Unit:</label></td>
<td><select name="UnitID" <cfif (isDefined("form.GroupID") AND form.GroupID NEQ "")>
onChange="this.form.submit()"
<cfelse>
disabled="disabled" onChange="this.form.submit()"
</cfif>>
<cfif (isDefined("form.GroupID") AND #form.GroupID# NEQ "")>
<cfquery name="GetUnitsFromGroup" datasource="#DSN#">
SELECT UnitID, UnitShort
FROM Units
WHERE GroupID = #form.GroupID#
</cfquery>
<cfoutput query="GetUnitsFromGroup">
<option value="#GetUnitsFromGroup.UnitID#" <cfif (isDefined("form.UnitID") AND form.UnitID EQ GetUnitsFromGroup.UnitID)>selected</cfif>>#GetUnitsFromGroup.UnitShort#</option>
</cfoutput>
<cfelse>
<option selected="selected">Please Select a Group</option>
</cfif>
</select></td>
</tr>
<tr>
<td><label class="InputLabel">Duty Phone:</label></td>
<td><input name="DutyPhone" type="text" <cfif (isDefined("form.DutyPhone") AND form.DutyPhone NEQ "")>
value="<cfoutput>#form.DutyPhone#</cfoutput>" maxlength="255"
<cfelse>
maxlength="255"
</cfif>/></td>
</tr>
<tr>
<td><label class="InputLabel">Home Phone:</label></td>
<td><input name="HomePhone" type="text" <cfif (isDefined("form.HomePhone") AND form.HomePhone NEQ "")>
value="<cfoutput>#form.HomePhone#</cfoutput>" maxlength="255"
<cfelse>
maxlength="255"
</cfif>/></td>
</tr>
<tr>
<td><label class="InputLabel">DEROS:</label></td>
<td><select name="DEROSMonth">
<cfoutput query="GetMonths">
<option value="#GetMonths.MonthShort#" <cfif (isDefined("form.DEROSMonth") AND form.DEROSMonth EQ GetMonths.MonthShort)>selected</cfif>>#GetMonths.Month#</option>
</cfoutput>
</select>
<select name="DEROSYear">
<cfoutput query="GetYears">
<option value="#GetYears.Years#" <cfif (isDefined("form.DEROSYear") AND form.DEROSYear EQ GetYears.Years)>selected</cfif>>#GetYears.Years#</option>
</cfoutput>
</select></td>
</tr>
<tr>
<td><label class="InputLabel">Email Address:</label></td>
<td><input name="EmailAddress" type="text" <cfif (isDefined("form.EmailAddress") AND form.EmailAddress NEQ "")>
value="<cfoutput>#form.EmailAddress#</cfoutput>" maxlength="255"
<cfelse>
maxlength="255"
</cfif>/></td>
</tr>
<tr>
<td><label class="InputLabel">EET Training Date:</label></td>
<td><input name="EetTrainingDate" type="text" <cfif (isDefined("form.EetTrainingDate") AND form.EetTrainingDate NEQ "")>
value="<cfoutput>#form.EetTrainingDate#</cfoutput>" maxlength="255"
<cfelse>
maxlength="255" onfocus="if(this.value=='mm/dd/yyyy')this.value='';" value="mm/dd/yyyy" maxlength="255"
</cfif>/></td>
</tr>
<tr>
<td><label class="InputLabel">Badge Type:</label></td>
<td><select name="BadgeTypeID">
<cfoutput query="GetBadgeTypes">
<option value="#GetBadgeTypes.BadgeTypeID#" <cfif (isDefined("form.BadgeTypeID") AND form.BadgeTypeID EQ GetBadgeTypes.BadgeTypeID)>selected</cfif>>#GetBadgeTypes.BadgeTypeShort#</option>
</cfoutput>
</select></td>
</tr>
<tr>
<td><label class="InputLabel">Badge Number:</label></td>
<td><input name="BadgeNumber" type="text" <cfif (isDefined("form.BadgeNumber") AND form.BadgeNumber NEQ "")>
value="<cfoutput>#form.BadgeNumber#</cfoutput>" maxlength="255"
<cfelse>
maxlength="255"
</cfif>/></td>
</tr>
<tr>
<td><label class="InputLabel">GBS Qualified?:</label></td>
<td><p>
<label class="InputLabel">
<input name="GBSQualified" type="radio" value="Yes"
<cfif (isDefined("form.GBSQualified") AND form.GBSQualified EQ "Yes")>
checked="checked" onClick="this.form.submit()"
<cfelse>
onClick="this.form.submit()"
</cfif>/>
Yes</label>
<br />
<label class="InputLabel">
<input name="GBSQualified" type="radio" value="No"
<cfif (isDefined("form.GBSQualified") AND form.GBSQualified EQ "No")>
checked="checked" onClick="this.form.submit()"
<cfelse>
<cfif (isDefined("form.GBSQualified") AND form.GBSQualified EQ "Yes")>
onClick="this.form.submit()"
<cfelse>
checked="checked" onClick="this.form.submit()"
</cfif>
</cfif>/>
No</label>
<br />
</p></td>
</tr>
<tr>
<td><label class="InputLabel">GBS Training Date:</label></td>
<td><input name="GBSTrainingDate" type="text" <cfif (isDefined("form.GBSTrainingDate") AND form.GBSTrainingDate NEQ "")>
value="<cfoutput>#form.GBSTrainingDate#</cfoutput>" maxlength="255"
<cfelse>
<cfif (isDefined("form.GBSQualified") AND form.GBSQualified EQ "Yes")>
onfocus="if(this.value=='mm/dd/yyyy')this.value='';" value="mm/dd/yyyy"
<cfelse>
value="Not GBS Qualified" disabled="disabled"
</cfif>
maxlength="255"
</cfif>/></td>
</tr>
<tr>
<td><label class="InputLabel">Access Level:</label></td>
<td><select name="AccessLevelID">
<cfoutput query="GetAccessLevels">
<option value="#GetAccessLevels.AccessLevelID#" <cfif (isDefined("form.AccessLevelID") AND form.AccessLevelID EQ GetAccessLevels.AccessLevelID)>selected</cfif>>#GetAccessLevels.AccessLevel#</option>
</cfoutput>
</select></td>
</tr>
<tr>
<td><div align="right">
<input name="Submit" type="submit" value="Submit" />
</div></td>
<td><input name="Clear" type="reset" value="Clear" /></td>
</tr>
</table>
<cfset DEROS = "form.DEROSMonth""-""form.DEROSYear">
<cfset UserName = "form.FirstName"".""form.LastName">
<input name="DEROS" type="hidden" value="#DEROS#" />
<input name="UserName" type="hidden" value="#UserName#" />
<input type="hidden" name="MM_InsertRecord" value="form">[/color red]
</form>
</body>
</html>
 
I usualy put a hidden form field to use as an insert flag, or form change.

for example
<input type = "hidden" name = "insertForm" value = "false">


to submit and insert
... onchange = "document.forms[0].insertForm.value = "true"; document.forms[0].submit();"

to submit and get extra data
... onchange = "document.forms[0].submit();"

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
TruthInSatire,

I inserted the code at the end of the submit button with an onclick event. The form refreshes (submits) but it doesn't change the value of the insertForm, thus not actually inputing the data into the database. Any suggestions?
 
Sorry here is the code...


<cfset CurrentPage=GetFileFromPath(GetTemplatePath())>
<cfif IsDefined("FORM.insertFormd") AND FORM.insertForm EQ "true">
<cfquery datasource="ExerciseWebApplication">
INSERT INTO Users ("Lookup to RankID", FirstName, LastName, "Lookup to GroupID", "Lookup to UnitID", DutyPhone, HomePhone, DEROS, EmailAddress, EetTrainingDate, "Lookup to BadgeTypeID", BadgeNumber, GBSQualified, GBSTrainingDate, "Lookup to AccessLevelID", UserName)
VALUES (

...

<td><div align="right">
<input name="Submit" type="submit" value="Submit" onclick = "document.forms[0].insertForm.value = "true"; document.forms[0].submit();"/>
</div></td>
<td><input name="Clear" type="reset" value="Clear" /></td>
</tr>
</table>
<cfset DEROS = "form.DEROSMonth""-""form.DEROSYear">
<cfset UserName = "form.FirstName"".""form.LastName">
<input name="DEROS" type="hidden" value="#DEROS#" />
<input name="UserName" type="hidden" value="#UserName#" />
<!--<input type="hidden" name="MM_InsertRecord" value="">-->
<input type = "hidden" name = "insertForm" value = "false">
</form>
</body>
</html>
 
ewylam, you have a double-quote issue in your onClick event handeler.

Change it to:
Code:
<input name="Submit" type="submit" value="Submit" onclick = "document.forms[0].insertForm.value = [COLOR=red]'true'[/color]; document.forms[0].submit();"/>

[sub]
____________________________________
Just Imagine.
[sub]
 
Also, looking at the code I noticed that you're misspelled the hidden field var.

It should be
<cfif IsDefined("FORM.insertForm") AND FORM.insertForm EQ "true">

NOT

<cfif IsDefined("FORM.insertFormd") AND FORM.insertForm EQ "true">

(you typed in an extra d at the end)

[sub]
____________________________________
Just Imagine.
[sub]
 
GUJUm0Del,

Thank you for the help it worked perfectly. Perhaps you could answer thsi one for me. What do you fell is the best way to handle the action of a form? Example. The form above is to register a user to a site. I also need to use the form to edit a users info that has already entered the site. Is it best to create two seperate pages for this or just create a bunch of <cfif> statments? Also, should the action (is. updating/inserting into the database) be completed on the same page or on an action page? You help is greatly appreciated. Thank you.

Emmett
 
ewylam, glad I could have helped with the original porblem.

I always do registration and updating of forms as two seperate forms. Its not a good idea (or the least, a bad codin practice) to do all that in one form sperated by bunch of <cfif>'s.

What I do for registration is:
1. user fills out registration form
2. user hits submit, and the form goes to an action page where all SQL code and CF validation take effect (all relative logic is enclosed in <cfif>, <cftry>/<cfcatch>, <cfswitch> tags for easier processing, and error handeling).
3. if the form is legit and it gets entered into the dB, I do a <cflocation> to a thankyou page.

You never want to have the "thankyou" text on the same page as the backend processig code. The reason is if the user hits refresh they run the code again and again and again.

For updating:
1. user clicks on the edit link
2. i run a SELECT query to retrieve info from the dB for prepopulation of fields. The SELECT's WHERE clause can be the userid (for user info).
3. user makes and changes, and hits the submit button
4. same backend logic as above and if all is good (as in no errors or problems) the user is taken to a "thank-you" page, just like above.



[sub]
____________________________________
Just Imagine.
[sub]
 
GUJUm0deL,

Thanks again for you assistance. Your logic sounds good and it makes my job alot easier. I appreicate it. I wish there was more that I could offer people on this site. With more practice I will get there.

Thanks,

Emmett
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top