Creating a Registration Page - Exercise

Contact Us or call 1-877-932-8228
Creating a Registration Page - Exercise

Creating a Registration Page

Duration: 20 to 30 minutes.

In this exercise, you will create a self-submitting registration page that displays a registration form the first time a user visits the page and processes the form when the user submits it.

  1. Open DatabaseBasics/Exercises/Register.cfm in your editor. Much of the file is already complete.
  2. In the main <cfelse> block follow the directions in the comments. You will write code that:
    • Validates the passwords.
    • Inserts the new record into the database (the fields in the database have the same names as those in the form: firstname, lastname, email, password).

Code Sample:

DatabaseBasics/Exercises/Register.cfm
<html>
<head>
<title>Register</title>
</head>
<body>

<cfif NOT isDefined("FORM.submitted")>

	<h2>Registration Form</h2>
	<cfoutput><form method="post" action="#CGI.SCRIPT_NAME#"></cfoutput>
	<input type="hidden" name="submitted" value="true">
	<table>
	<tr>
		<td>Email:</td>
		<td>
			<input type="text" name="email" size="30">
		</td>
	</tr>
	<tr>
		<td>Password:</td>
		<td>
			<input type="password" name="password" size="10">
		</td>
	</tr>
	<tr>
		<td>Repeat Password:</td>
		<td>
			<input type="password" name="password2" size="10">
		</td>
	</tr>
	<tr>
		<td>First name:</td>
		<td>
			<input type="text" name="firstname" size="10">
		</td>
	</tr>
	<tr>
		<td>Last name:</td>
		<td>
			<input type="text" name="lastname" size="10">
		</td>
	</tr>
	<tr>
		<td colspan="2" align="center">
			<input type="submit" value="Register">
		</td>
	</tr>
	</table>
	</form>
<cfelse>
	
<!---
	Write an if statement that checks to make
	sure the passwords are the same.
--->
	<cfif WRITE_CONDITION_HERE>
		<!---
			Write a query that inserts the new record
			into the Users table. The fields in the
			database have the same names as those in
			the form: firstname, lastname, email, password
		--->
		<cfquery datasource="Runners">
		
		</cfquery>
		You have registered successfully.
			<p><a href="index.cfm">Home Page</a></p>
	<cfelse>
		<p class="errors"><b>Your passwords
		do not match. Please <a href=
		"Register.cfm">try again</a>.</p>
	</cfif>
</cfif>

</body>
</html>

Write code on the registration page that first checks to see if that email is already in the Users table. If it is, let the user know she is already registered and do not insert a new record.

Solution:

DatabaseBasics/Solutions/Register.cfm
---- C O D E   O M I T T E D ----
<cfelse>
	
	<cfif FORM.password EQ FORM.password2>
		<cfquery datasource="runners">
			INSERT INTO Users
			(firstname, lastname, email, password)
			VALUES ('#FORM.firstname#', '#FORM.lastname#', '#FORM.email#', '#FORM.password#')
		</cfquery>
		You have registered successfully.
			<p><a href="index.cfm">Home Page</a></p>
	<cfelse>
		<p class="errors"><b>Your passwords
		do not match. Please <a href=
		"Register.cfm">try again</a>.</p>
	</cfif>
</cfif>

</body>
</html>

Challenge Solution:

DatabaseBasics/Solutions/Register-challenge.cfm
---- C O D E   O M I T T E D ----
<cfelse>
	
	<cfif FORM.password EQ FORM.password2>
		<cfquery name="emailcheck" datasource="runners">
			SELECT *
			FROM Users
			WHERE email='#FORM.email#'
		</cfquery>
		<cfif emailcheck.RecordCount EQ 0>
			<cfquery datasource="runners">
				INSERT INTO Users
				(firstname, lastname, email, password)
				VALUES ('#FORM.firstname#', '#FORM.lastname#', '#FORM.email#', '#FORM.password#')
			</cfquery>
			
			<cfquery name="emailcheck2" datasource="runners">
				SELECT *
				FROM Users
				WHERE email='#FORM.email#'
			</cfquery>
			<cfif emailcheck2.RecordCount NEQ 1>
				Registration Failed
			<cfelse>
				You have registered successfully.
				<p><a href="index.cfm">Home Page</a></p>
			</cfif>
		
		<cfelse>
			<p>It appears you have already registered.</p>
		</cfif>
	<cfelse>
		<p class="errors"><b>Your passwords
		do not match. Please <a href=
		"Register.cfm">try again</a>.</p>
	</cfif>
</cfif>

</div>
</body>
</html>
Next