473,324 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 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 3282
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.