1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

need a little form logic (validation in a sense)

Discussion in 'Programming' started by bowie101, Jan 25, 2008.

  1. #1
    hi all. haven't used coldfusion in a loooong time, but now, with the new job, and new settings, i have to once again take the plunge.

    I am building a form/application that is for a registration process.

    they enter their info into a form, and if they aren't already in the database, their info gets entered.

    if their data is already in the database they get somethign back saying they've already registered.

    So, I'm thinking, Enter data --> query database, is data already in there ----if no ---> enter into database , and go to registration successful page -->end

    ---> if yes--->go to registration info already entered page -->end.

    Can someone help me out with how this logic is syntaxted in CF8?

    thankyou.
     
    bowie101, Jan 25, 2008 IP
  2. lespaul00

    lespaul00 Peon

    Messages:
    283
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I use CFMX7, but it should still work (someone else tell me otherwise if I am wrong).

    I would, (for beginning) create a registration page (register.cfm) and then an action page (register_submit.cfm).


    not tested...

    register.cfm:

    
    <cfform action="register_submit.cfm" method="post">	  
    
    <table width="95%">		
    	<tr>
    	
    		<td width="40%" valign="top">
    			<p>Desired username: <span class="style1 style5">*</span>		</td>
    		
    		<td width="55%" valign="top">
    			  <cfinput name="USER_NAME_NAME" 
    					type="text" 
    					value="" 
    					size="25" 
    					maxlength="50" 
    					required="true" 
    					message="Please enter a desired username!"/>
    		</td>
    	</tr>
    	<tr>
    		<td width="40%" valign="top">
    			<p>Password: <span class="style1 style5">*</span> </p>
    		</td>
    		
    		<td width="55%" valign="top">
    				<cfinput name="USER_PASS_PASS" 
    					type="text" 
    					value="" 
    					size="25" 
    					maxlength="25" 
    					required="true" 
    					message="Please enter a valid password!"/>
    		</td>
    	</tr>
    	
    	<tr>
    	
    		<td width="40%" valign="top">
    		<p>Email address: <span class="style1 style5">*</span> </p>
    		</td>
    
    	  <td width="55%">
    					<cfinput 
    					type="text" name="EMAIL" 
    					message="Please enter your email address!" 
    					required="true" 
    					value="" 
    					validate="email"
    					size="50" 
    					maxlength="200"/>
    					<br />
    	  </td>
    
    
    	
    	</tr>
    						<br />
    	<tr>
    		<td width="40%" valign="top">
    			<p>First name:</p>
    		</td>
    		<td width="55%">
    					<cfinput 
    					type="text" name="USER_FIRST_NAME" 
    					value="" 
    					size="50" 
    					maxlength="200"/>
    		</td>
    	</tr>
    	
    	<tr>
    		<td width="40%" valign="top">
    			<p>Last name:</p>
    		</td>
    		<td width="55%">
    					<cfinput 
    					type="text" name="USER_LAST_NAME" 
    					value="" 
    					size="50" 
    					maxlength="200"/>
    		</td>
    	</tr>	
    </table>
    
    </cfform>
    
    
    
    Code (markup):


    register_submit.cfm:

      <CFSET Valid = true>
      <CFSET Error = "">
       
    	  <cfoutput>
    	  <cfif isdefined("form.USER_NAME_NAME")>
    		  
    		  <cfquery datasource="YOURDATABASE" name="check">
    			SELECT USER_NAME_NAME
    			FROM TBLUSER
    			WHERE USER_NAME_NAME = '#form.USER_NAME_NAME#'
    			</cfquery>
    	  </cfif>
    	  </cfoutput>
       
    	  <cfif check.recordcount GT 0>
    		
    		
    		<CFSET Valid = False>
    		<cfelse>
    		
    	  </cfif>
       
    		<CFIF not Valid>
    
    <CFQUERY DATASOURCE="YOURDATABASE" NAME="TBLUSER">
    UPDATE TBLUSER
    
    SETUSER_NAME_NAME = '#form.USER_NAME_NAME#', 
    USER_PASS_PASS = '#form.USER_PASS_PASS#', 
    EMAIL='#form.EMAIL#', 
    USER_FIRST_NAME='#form.USER_FIRST_NAME#', 
    USER_LAST_NAME='#form.USER_LAST_NAME#' 
    
    WHERE USER_NAME_NAME = '#form.USER_NAME_NAME#'
    </CFQUERY>
    	   
    ....whatever else....
    	   
    		<CFELSE>
    		</CFIF> 
    
    
    
    <CFQUERY DATASOURCE="YOURDATABASE" NAME="TBLUSER">
      INSERT INTO TBLUSER(USER_NAME_NAME, USER_PASS_PASS, EMAIL, USER_FIRST_NAME, USER_LAST_NAME)
      VALUES
      ( 
      <cfqueryparam value="#form.USER_NAME_NAME#" cfsqltype="cf_sql_varchar">
      , 
      <cfqueryparam value="#form.USER_PASS_PASS#" cfsqltype="cf_sql_varchar">
      , 
      <cfqueryparam value="#form.EMAIL#" cfsqltype="cf_sql_varchar">
      , 
      <cfqueryparam value="#form.USER_FIRST_NAME#" cfsqltype="cf_sql_varchar">
      ,
      <cfqueryparam value="#form.USER_LAST_NAME#" cfsqltype="cf_sql_varchar">
    
    )
    </CFQUERY> 
    Code (markup):
     
    lespaul00, Jan 28, 2008 IP
  3. bowie101

    bowie101 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wow! I didn't get to look until now. But thanks! Right on time! I'll check it out!
     
    bowie101, Feb 4, 2008 IP
  4. bowie101

    bowie101 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Looking it over, and I just noticed this part on the register_submit.cfm page:


    ---------------------------------------------------------
    <cfif check.recordcount GT 0>


    <CFSET Valid = False>
    <cfelse>

    </cfif>

    <CFIF not Valid>

    ---------------------------------------------------

    is that right? should the opening cfelse tag have something after it before closing the cfif, or is the last choice to close the cfif? and then the <CFIF not Valid> , are you using shorthand there or is that to be taken literally like

    <CFIF NOT Valid> ?

    I know I'm really bad with syntax that I haven't thought about for a loooooong time.

    Thanks.
     
    bowie101, Feb 4, 2008 IP
  5. notUncool

    notUncool Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Syntax for something like this is largely the same between ColdFusion 7 and 8.

    Now, as far as that "Valid" condition check...

    Why not just cut out the middle man and do...

    <cfif check.recordcount>

    ColdFusion will treat that as a boolean (0 = false, greater than 0 = true)

    Also, I sure would like to see some more cfqueryparams in that beast.
     
    notUncool, Feb 4, 2008 IP
  6. lespaul00

    lespaul00 Peon

    Messages:
    283
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It shouldn't matter. I never even referenced the 'valid' or 'error' variables. I took it from a page I created, but deleted a bunch of the extra stuff I was doing (and, I forgot to delete these portions):

     <CFSET Valid = true>
      <CFSET Error = "">
    Code (markup):

      
    		
    		
    		<CFSET Valid = False>
    		<cfelse>
    		
    	  </cfif>
       
    		<CFIF not Valid>
    Code (markup):
    Leave it in or take it out. It should work the same either way (actually, I realized I improperly nested the last </cfif> in my first post. It should go after the final query).

    My initial application of this was as such:

    	  <CFSET Valid = true>
      <CFSET Error = "">
    
    ...
    
    
    <cfif check.recordcount GT 0>
    		
    		
    		<CFSET Valid = False>
    		<cfelse>
    		
    	  </cfif>
       
    		<CFIF not Valid>
    	   This username is taken.
    	   <A HREF="javascript:history.go(-1)"><br>
    		Click Here To Return</A>
    	   
    		<cfabort>
    	   
    		<CFELSE>
    		</CFIF>  
    Code (markup):
    The reason I did not add anything after the <cfelse> is because Valid is already TRUE. So, there's no need to write anything here. Then, I have the code:

    <CFIF not Valid>
    Code (markup):
    meaning, there is a record already in the database with this username, and therefore, the user is directed to go back to the register page and try again. The script aborts. Otherwise, the page script continues, and populates your datasource with the new user's information. (In your case, you'd like to update the user record if it exists, which is the main difference in this case).


    If you take a simplistic approach now, we can alter it as such:

    
    	  <cfoutput>
    	  <cfif isdefined("form.USER_NAME_NAME")>
    		  
    		  <cfquery datasource="YOURDATABASE" name="check">
    			SELECT USER_NAME_NAME
    			FROM TBLUSER
    			WHERE USER_NAME_NAME = '#form.USER_NAME_NAME#'
    			</cfquery>
    	  </cfif>
    	  </cfoutput>
       
    	  <cfif check.recordcount GT 0>
    		
    <!---------USERNAME IS TAKEN, USER EXISTS----->
    <!---------SIMPLY UPDATE USER INFO------------->
    
    <CFQUERY DATASOURCE="YOURDATABASE" NAME="TBLUSER">
    UPDATE TBLUSER
    
    SETUSER_NAME_NAME = '#form.USER_NAME_NAME#', 
    USER_PASS_PASS = '#form.USER_PASS_PASS#', 
    EMAIL='#form.EMAIL#', 
    USER_FIRST_NAME='#form.USER_FIRST_NAME#', 
    USER_LAST_NAME='#form.USER_LAST_NAME#' 
    
    WHERE USER_NAME_NAME = '#form.USER_NAME_NAME#'
    </CFQUERY>
    	   
    ....whatever else....
    	   
    <CFELSE>
    	
    <!---------USERNAME IS NOT TAKEN, USER DOES NOT EXIST----->
    <!---------ADD USER INFORMATION TO DATASOURCE------------->
    
    
    <CFQUERY DATASOURCE="YOURDATABASE" NAME="TBLUSER">
      INSERT INTO TBLUSER(USER_NAME_NAME, USER_PASS_PASS, EMAIL, USER_FIRST_NAME, USER_LAST_NAME)
      VALUES
      ( 
      <cfqueryparam value="#form.USER_NAME_NAME#" cfsqltype="cf_sql_varchar">
      , 
      <cfqueryparam value="#form.USER_PASS_PASS#" cfsqltype="cf_sql_varchar">
      , 
      <cfqueryparam value="#form.EMAIL#" cfsqltype="cf_sql_varchar">
      , 
      <cfqueryparam value="#form.USER_FIRST_NAME#" cfsqltype="cf_sql_varchar">
      ,
      <cfqueryparam value="#form.USER_LAST_NAME#" cfsqltype="cf_sql_varchar">
    
    )
    </CFQUERY>
    	</CFIF> 
    
    Code (markup):
    Use the code and first see if it works (with the above portion in or out). Then, we can move onto the next steps of what you're trying to accomplish.

    - Nick
     
    lespaul00, Feb 5, 2008 IP