472,952 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

OOP: Problem with edit form

113 100+
Hi,

I'm having trouble fully implementing the edit section of a contact admin system, so far I have written the following:

- Bean (Contact.cfc)
- Data Access object (ContactDAO.cfc)
- Gateway (ContactGateway.cfc)

- index.cfm - Deals with the business logic
- display/form.cfm - Produces the form for both add and edit behaviour
- display/process.cfm - Deals with form data
- display/edit.cfm - Displays listing of contacts and allows user to select one to edit.

The add contact part is working and is accessed like this index.cfm?action=add.
On submission of the form, index.cfm?action=process is passed the form values and the data is then sent to the database.
The add page is also supposed to double up as an update / edit page, so that if an id is passed in then the corresponding contact will be retrieved from the database and displayed in the form. This would be in the form of index?action=update&id=1.

However what I'm struggling with is how to do that from a form submit on the index.cfm?action=edit page. I've probably got this structure wrong and need some pointers in the right direction.

My code is as follows:

index.cfm:
Expand|Select|Wrap|Line Numbers
  1. <cfset Request.Ses.User.protect("staff","../index.cfm") />
  2. <cfimport taglib="../../taglib" prefix="func">
  3. <cfinclude template="../../taglib/QueryStrings.cfm" />
  4. <cfparam name="url.action" default="add" />
  5. <cfparam name="url.id" type="string" default="" />
  6.  
  7. <cfswitch expression="#url.action#">
  8.     <cfcase value="add"> 
  9.         <cfinclude template="display/form.cfm">
  10.     </cfcase>
  11.     <cfcase value="edit">
  12.         <cfinclude template="display/edit.cfm">
  13.     </cfcase>
  14.     <cfcase value="delete">
  15.         <cfinclude template="display/delete.cfm">
  16.     </cfcase>    
  17.     <cfcase value="update">
  18.         <cfinclude template="display/form.cfm">
  19.     </cfcase>    
  20.     <cfcase value="process">
  21.         <cfinclude template="display/process.cfm">
  22.     </cfcase>
  23. </cfswitch>
  24.  
  25. <cfinclude template="../../taglib/pagefooter.cfm">
  26.  
display/form.cfm:
Expand|Select|Wrap|Line Numbers
  1. <cfimport taglib="../../taglib" prefix="func">
  2. <cfimport taglib="../../../taglib" prefix="func">
  3.  
  4.  
  5.         <!--- get instance of PersonDAO --->
  6.             <cfset contactDAO = CreateObject("component", 
  7.                                 "com.dataobjects.ContactDAO").init(Request.App.dsn) />
  8.  
  9.             <!--- if there isn't already a person in the request scope, get the person --->
  10.             <cfif Not StructKeyExists(request, "contact")>
  11.                 <cfset request.contact = CreateObject("component", 
  12.                                         "com.beans.Contact").init(url.id) />
  13.                 <cfset contactDAO.read(request.contact) />
  14.             </cfif> 
  15.  
  16.             <!--- if we have an id in either the URL or the person, we're doing an update --->
  17.             <cfif url.id NEQ "" OR request.contact.getId() NEQ "">
  18.                 <cfset action = "Update" />
  19.             </cfif>
  20.  
  21.             <!--- <cfif isDefined("url.action") AND url.action EQ "update" OR isDefined("url.action") AND StructKeyExists(request, "errors") EQ "true"> --->
  22.  
  23.             <h2>Add Contact Item</h2>
  24.  
  25.             <!--- output message if it exists --->
  26.             <cfif StructKeyExists(request, "message")>
  27.                 <cfoutput><p style="color:red;font-weight:bold;">#request.message#</p></cfoutput>
  28.             </cfif>
  29.  
  30.             <!--- output errors if they exist --->
  31.             <cfif StructKeyExists(request, "errors")>
  32.                 <p style="color:red;"><strong>The following errors occurred:</strong></p>
  33.  
  34.                 <cfloop collection="#request.errors#" item="error">
  35.                     <ul>
  36.                         <cfoutput><li>#request.errors[error]#</li></cfoutput>
  37.                     </ul>
  38.                 </cfloop>
  39.             </cfif>
  40.  
  41.             <!--- output form --->
  42.             <cfoutput>
  43.             <form action="index.cfm?action=process" method="post">
  44.                 <input type="hidden" name="id" id="id" size="36" required="no" value="#request.contact.getId()#" />            
  45.                 <p>First Name *</p>
  46.                 <input class="aswide" type="text" maxlength="64" name="firstName" id="firstName" value="#request.contact.getFirstName()#" />
  47.                 <p>Last Name *</p>
  48.                 <input class="aswide" type="text" maxlength="64" name="lastName" id="lastName" value="#request.contact.getLastName()#" />
  49.                 <p>Address *</p>                
  50.                 <textarea class="cms" type="textarea" cols="40" rows="5" name="address" id="address">#request.contact.getAddress()#</textarea>
  51.                 <p>Email Address *</p>
  52.                 <input class="cms" type="text" maxlength="320" name="email" id="email" value="#request.contact.getEmail()#"/>
  53.                 <p>Tel No. *</p>
  54.                 <input class="cms" type="text" maxlength="16" name="telNo" id="telNo" value="#request.contact.getTelNo()#">                
  55.                 <div class="submitBox"><input class="cms" type="submit" value="#action# Contact"></div>
  56.                 <input type="hidden" id="action" name="action" value="#action#" />
  57.             </form>        
  58.             </cfoutput>
  59.  
display/process.cfm:
Expand|Select|Wrap|Line Numbers
  1. <cfscript>
  2.     // initialize variables
  3.     results = StructNew();
  4.     errors = StructNew();
  5.  
  6.     // get instance of DAO
  7.     contactDAO = CreateObject("component", 
  8.                 "com.dataobjects.ContactDAO").init(Request.App.dsn);
  9.  
  10.     // initialize person object with form data
  11.     contact = CreateObject("component", 
  12.                 "com.beans.Contact").init(form.id, form.firstName, 
  13.                                                         form.lastName, form.address, form.email, 
  14.                                                         form.telNo);
  15.  
  16.     // validate contact data using contact bean's validate method
  17.     errors = contact.validate();
  18.  
  19.     // if no errors, take action based on value of action
  20.     if (StructIsEmpty(errors)) {
  21.         switch(form.action) {
  22.             case "Add":
  23.                 // set person id
  24.                 contact.setID(CreateUUID());
  25.  
  26.                 // call create method of DAO
  27.                 results = contactDAO.create(contact);
  28.                 break;
  29.  
  30.             case "Update":
  31.                 // call update method of DAO
  32.                 results = contactDAO.update(contact);
  33.                 break;
  34.  
  35.             case "Delete":
  36.                 // call delete method of DAO
  37.                 results = contactDAO.delete(contact);
  38.                 break;
  39.         }
  40.     } else {
  41.         // errors, so put struct and object in request scope and route back to form
  42.         request.errors = errors;
  43.     }
  44.  
  45.     // if there's a message, put it in the request scope
  46.     if (StructKeyExists(results, "message")) {
  47.         request.message = results.message;
  48.     }
  49.  
  50.     // if this wasn't a delete, put the person in the request scope
  51.     if (form.action NEQ "Delete") {
  52.         request.contact = contact;
  53.     }
  54.  
  55.     // server-side redirect back to form page
  56.     if(StructIsEmpty(errors)) {
  57.         getPageContext().forward("index.cfm?action=success");
  58.     }
  59.     else {
  60.         getPageContext().forward("index.cfm?action=edit");            
  61.     }
  62. </cfscript>
  63.  
display/edit.cfm:
Expand|Select|Wrap|Line Numbers
  1. <cfimport taglib="../../taglib" prefix="func">
  2. <cfimport taglib="../../../taglib" prefix="func">
  3. <cfparam name="url.item" default="">
  4.  
  5.                 <h2>Edit Contact Item</h2>
  6.                 <p>Select a contact to edit from the list below and press edit.</p>
  7.  
  8.                 <!--- get instance of ContactGateway --->
  9.                 <cfset ContactGateway = CreateObject("component", 
  10.                                     "com.dataobjects.ContactGateway").init(Request.App.dsn) />
  11.                 <!--- get all contacts --->                                
  12.                 <cfset request.allContacts = ContactGateway.getAllContacts() />
  13.  
  14.                 <cfset RowsPerPage = 5>
  15.                 <cfparam name="URL.StartRow" default="1" type="numeric">
  16.                 <cfif isNumeric(URL.StartRow) NEQ "yes">
  17.                     <cfset URL.StartRow = 1>
  18.                 </cfif>
  19.                 <cfset StartRowBack = StartRow - RowsPerPage>
  20.                 <cfset StartRowNext = StartRow + RowsPerPage>
  21.                 <cfset TotalRows = request.allContacts.RecordCount>
  22.                 <cfset EndRow = Min(URL.StartRow + RowsPerPage - 1, TotalRows)>
  23.  
  24.                 <cfoutput><div class="cms_results">Displaying: #StartRow# to #EndRow# of #TotalRows#</div></cfoutput>
  25.                 <cfif TotalRows GT RowsPerPage><div class="cms_results">Pages: <cfinclude template="../../../taglib/pageLinksInc.cfm"></div></cfif>
  26.  
  27.                 <cfif StructKeyExists(request,"allContacts")>         
  28.                 <form action="index.cfm?action=update" method="post" enctype="application/x-www-form-urlencoded">
  29.                     <table class="propertiesList">
  30.                         <tr>
  31.                             <td><p>First Name</p></td>
  32.                             <td><p>Last Name</p></td>
  33.                             <td><p>Address</p></td>
  34.                             <td><p>Email</p></td>
  35.                             <td><p>Tel No.</p></td>
  36.                         </tr>                    
  37.                     <cfoutput>
  38.                         <cfloop query="request.allContacts" StartRow="#URL.StartRow#" EndRow="#EndRow#">
  39.                         <tr <cfif currentRow MOD 2>class="even"</cfif>>
  40.                             <td width="20px"><input type="radio" name="id" id="id" value="#id#"></td>
  41.                             <td><p>#firstName#</p></td>
  42.                             <td><p>#lastName#</p></td>    
  43.                             <td><p>#address#</p></td>
  44.                             <td><p>#email#</p></td>
  45.                             <td><p>#telNo#</p></td>                        
  46.                         </tr>
  47.                         </cfloop>
  48.                     </cfoutput>
  49.                     </table>
  50.                     <cfif TotalRows GT RowsPerPage><div class="cms_results"><cfinclude template="../../../taglib/cmsNextPrevInc.cfm"></div></cfif>    
  51.                     <div class="submitBox"><input class="cms" type="submit" value="Edit"></div>         
  52.                 </form>       
  53.                 </cfif>
  54.  
Thanks,

Chromis
Aug 13 '08 #1
1 3245
acoder
16,027 Expert Mod 8TB
The add page is also supposed to double up as an update / edit page, so that if an id is passed in then the corresponding contact will be retrieved from the database and displayed in the form. This would be in the form of index?action=update&id=1.

However what I'm struggling with is how to do that from a form submit on the index.cfm?action=edit page.
Do you mean how to pass an ID?
Aug 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

56
by: Xah Lee | last post by:
What are OOP's Jargons and Complexities Xah Lee, 20050128 The Rise of Classes, Methods, Objects In computer languages, often a function definition looks like this: subroutine f (x1, x2, ...)...
34
by: Pmb | last post by:
Hi. I'm new to this group. I'm refreshing/learning C++ and am starting to learn Object Oriented Programming (OOP). In discussing this with people I came up short as to what the benefits of OOP are....
6
by: Ivan Weiss | last post by:
I am designing a class to represent an item in a database (containing model's of products). I need to display a listing of all of these items in a listview to be displayed to the user (so they can...
7
by: Fred Exley | last post by:
I have a traditional application using a database and procedural code that works fine. I'm re-writing it using the OOP methodology, just to see how this may be the better way to do things. I...
3
by: Shawn Ferguson | last post by:
Hello All, I'm starting to learn C# and OOP to become a better programmer, however I;m getting frustrated. It's tough, but what is making it really tough for me is trying to do everything with...
19
by: adriancico | last post by:
Hi I am working on a python app, an outliner(a window with a TreeCtrl on the left to select a document, and a RichTextBox at the right to edit the current doc). I am familiarized with OOP...
65
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective...
2
by: chromis | last post by:
Hi there, I've been reading an OOP book recently and it gives some nice Adaptor / Template patttern code to wrap around the php Mysql functions. I thought that I'd try and create a Simple Address...
8
by: chromis | last post by:
Hi, I'm writing a contacts section for a cms on a website, I've decided to write the section in OO code. So far I have my Contacts object and a page structure I would use for a procedural site. ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.