Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Delete record

I am developing a guestbook for my site which I am trying to build right now. I have gotten the book up and running, including submitting and maintenance stuff like changing messages to get rid of some bad stuff in there. Now I only have a problem with deleting a record from the database in the same maintenancepage as changing it. There are 3 pages right now. I have put the code of both pages in here. The Delete button is already there, but not working yet.

Page 1: Get a list out of the database by name.
<html>
<head>
     <title>Maintenance List</title>
</head>
<body>
<cfquery name="GetMessages" datasource="GuestBook">
     SELECT FirstName, LastName, Message_ID
     FROM GuestBook
</cfquery>
<cfoutput query="GetMessages">
     <a href="MaintenanceForm.cfm?Message_ID=#GetMessages.Message_ID#">#FirstName# #LastName#</a><br>
</cfoutput>
</body>
</html>


Page 2: The Maintenance Form
<html>
<head>
<title>
     Guestbook Andre and Yulya
</title>
</head>
<body>
<cfquery name="GetMessageDetails" datasource="GuestBook">
     SELECT Message_ID, FirstName, LastName,
               HomeTown, Country, HomePage, Comment
     FROM GuestBook
     WHERE Message_ID = #URL.Message_ID#
</cfquery>
<form action="MaintenanceActionForm.cfm" method="post">
  <input type="Hidden" name="Message_ID" value="<cfoutput>#URL.Message_ID#</cfoutput>">
               <table cellspacing="2" cellpadding="2" border="0">
<tr>
<td>
<font face="Comic Sans MS" color="Red">
First Name:
</font>
</td>
<td>
<input type="Text" name="FirstName" size="30" maxlength="30" value="<cfoutput>#GetMessageDetails.FirstName#</cfoutput>">
</td>
</tr>
<tr>
<td>
<font face="Comic Sans MS" color="Red">
Last Name:
</font>
</td>
<td>
<input type="Text" name="LastName" size="30" maxlength="30" value="<cfoutput>#GetMessageDetails.LastName#</cfoutput>">
</td>
</tr>
<tr>
<td>
<font face="Comic Sans MS" color="Red">
Home Town:
</font>
</td>
<td>
<input type="Text" name="HomeTown" size="30" maxlength="30" value="<cfoutput>#GetMessageDetails.HomeTown#</cfoutput>">
</td>
</tr>
<tr>
<td>
<font face="Comic Sans MS" color="Red">
Country:
</font>
</td>
<td>
<input type="Text" name="Country" size="30" maxlength="30" value="<cfoutput>#GetMessageDetails.Country#</cfoutput>">
</td>
</tr>
<tr>
<td>
<font face="Comic Sans MS" color="Red">
Homepage URL:
</font>
</td>
<td>
<input type="Text" name="HomePage" size="70" maxlength="70" value="<cfoutput>#GetMessageDetails.HomePage#</cfoutput>">
</td>
</tr>
<tr>
<td>
<font face="Comic Sans MS" color="Red">
Comment:
</font>
</td>
<td>
<textarea cols="80" rows="4" name="Comment"><cfoutput>#GetMessageDetails.Comment#</cfoutput>"></textarea>
</td>
</tr>
<tr>
<td>
</td>
<td>
<center>
<input type="Submit" name="Submit" value="Change">
<input type="Reset" name="Reset" value="Reset">
<input type="Button" name="Delete" value="Delete">
</center>
</td>
</tr>
</table>
</form>
</body>
</html>


Page 3: The Maintenance Action Form
<html>
<head>
     <title>Maintenance Action Form</title>
</head>
<body>
<cfquery name="UpdateMessage" datasource="GuestBook">
     UPDATE GuestBook
     SET FirstName='#Form.FirstName#',LastName='#Form.LastName#', HomeTown='#Form.HomeTown#', Country='#Form.Country#', HomePage='#Form.HomePage#', Comment='#Form.Comment#'
     WHERE Message_ID=#Message_ID#
</cfquery>
</body>
</html>
0
Dreetje
Asked:
Dreetje
  • 2
1 Solution
 
hammond_davidCommented:
The simplest way to do this would be to make the delete button a submit button:

<input type="Submit" name="Delete" value="Delete">

When the button is pressed the form will be submitted and the form parameter "Delete" will be sent to the action page.  The parameter will only be sent if the button is clicked, so on the action form you can put:

<cfif IsDefined("form.Delete")>
  <cfquery name="DeleteMessage" datasource="GuestBook">
    Delete from GuestBook
    WHERE Message_ID=#Message_ID#
  </cfquery>
<cfelse>
  <!--- update --->
</cfif>

You probably want to add some confirmation logic to safeguard against someone hitting the delete button accidentally.  You can do this with javascript on the form page:

<input type="Submit" name="Delete" value="Delete" onclick="return confirm('This record will be permanently deleted.  Continue?')">

Hope that helps
- David
0
 
anandkpCommented:
HI there,

This is simple -

Ur maintenance page has the code for only updating a Table record & so no matter what u do - it would never delete ur record.

what u need to do is pass a parameter when u delete

like say on delte send Act="del"
remember Act is a varible passed thru ur form action

I'll modify ur code hereitself - wait ....

======================================================

Page 2: The Maintenance Form
<html>
<head>
<title>
    Guestbook Andre and Yulya
</title>

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--

function delme()
{
window.document.frm.action = "MaintenanceActionForm.cfm?Act=Del"
window.document.frm.submit();
}

//-->
</SCRIPT>
</head>
<body>
<cfquery name="GetMessageDetails" datasource="GuestBook">
    SELECT Message_ID, FirstName, LastName,
              HomeTown, Country, HomePage, Comment
    FROM GuestBook
    WHERE Message_ID = #URL.Message_ID#
</cfquery>

<!-- always give ur form a name -->

<form name="frm" action="MaintenanceActionForm.cfm" method="post">
 <input type="Hidden" name="Message_ID" value="<cfoutput>#URL.Message_ID#</cfoutput>">
              <table cellspacing="2" cellpadding="2" border="0">
<tr>
<td>
<font face="Comic Sans MS" color="Red">
First Name:
.
.
.
.

==============================================


Page 3: The Maintenance Action Form
<html>
<head>
    <title>Maintenance Action Form</title>
</head>
<body>
<cfif isdefined('Act')>

<cfquery name="deleteMessage" datasource="GuestBook">
    delete GuestBook
    WHERE Message_ID=#Message_ID#
</cfquery>

<cfelse>

<cfquery name="UpdateMessage" datasource="GuestBook">
    UPDATE GuestBook
    SET FirstName='#Form.FirstName#',LastName='#Form.LastName#', HomeTown='#Form.HomeTown#', Country='#Form.Country#', HomePage='#Form.HomePage#', Comment='#Form.Comment#'
    WHERE Message_ID=#Message_ID#
</cfquery>

</cfif>

</body>
</html>

0
 
anandkpCommented:
Sorry ignore the earlier one !!!

HI there,

This is simple -

Ur maintenance page has the code for only updating a Table record & so no matter what u do - it would never delete ur record.

what u need to do is pass a parameter when u delete

like say on delte send Act="del"
remember Act is a varible passed thru ur form action

I'll modify ur code hereitself - wait ....

======================================================

Page 2: The Maintenance Form
<html>
<head>
<title>
    Guestbook Andre and Yulya
</title>

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--

function delme()
{
window.document.frm.action = "MaintenanceActionForm.cfm?Act=Del"
window.document.frm.submit();
}

//-->
</SCRIPT>
</head>
<body>
<cfquery name="GetMessageDetails" datasource="GuestBook">
    SELECT Message_ID, FirstName, LastName,
              HomeTown, Country, HomePage, Comment
    FROM GuestBook
    WHERE Message_ID = #URL.Message_ID#
</cfquery>

<!-- always give ur form a name -->

<form name="frm" action="MaintenanceActionForm.cfm" method="post">
 <input type="Hidden" name="Message_ID" value="<cfoutput>#URL.Message_ID#</cfoutput>">
              <table cellspacing="2" cellpadding="2" border="0">
<tr>
<td>
<font face="Comic Sans MS" color="Red">
First Name:
.
.
.
.

</td>
</tr>
<tr>
<td>
</td>
<td>
<center>
<input type="Submit" name="Submit" value="Change">
<input type="Reset" name="Reset" value="Reset">
<input type="Button" name="Delete" value="Delete" onclick="javascript:delme()">
</center>
</td>
</tr>
</table>
</form>
</body>
</html>



==============================================


Page 3: The Maintenance Action Form
<html>
<head>
    <title>Maintenance Action Form</title>
</head>
<body>
<cfif isdefined('Act')>

<cfquery name="deleteMessage" datasource="GuestBook">
    delete GuestBook
    WHERE Message_ID=#Message_ID#
</cfquery>

<cfelse>

<cfquery name="UpdateMessage" datasource="GuestBook">
    UPDATE GuestBook
    SET FirstName='#Form.FirstName#',LastName='#Form.LastName#', HomeTown='#Form.HomeTown#', Country='#Form.Country#', HomePage='#Form.HomePage#', Comment='#Form.Comment#'
    WHERE Message_ID=#Message_ID#
</cfquery>

</cfif>

</body>
</html>

0
 
DreetjeAuthor Commented:
It helped a lot. I was almost there solving it myself, but I needed just the last peice! Yhanks, and I htink it was an easy one or someone who uses CF more often :-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now