We help IT Professionals succeed at work.

Coldfusion Delete button question

roger v
roger v used Ask the Experts™
on
I have a CF 9 form that has add and update submit buttons. I display a bunch of rows from the database and I need the ability to delete each individual rows. My idea is to display a "delete" submit button for each row, name it dynamically with #currentrow#, and then when user clicks delete, ask for confirmation and if yes, then delete that row.

I have the javascript to do this but I'm not quite sure on how to handle this in the coldfusion form action section. Currently my action page looks like this:

<cfif IsDefined('form.btnSubmit')>
    <!---do a bunch of stuff--->
<cfelseif IsDefined('form.btnUpdate')>
   <!---do a bunch of updates--->
<!---this is where my delete code needs to go--->
<cfelseif IsDefined('form.btnDelete#currentrow#')>
  <!---delete query--->
</cfif>

<!---and my form page looks like this--->
<table>
<cfoutput query="myQuery">
    <tr>
         <td>#name#</td>
        <td>#division#</td>
       <!---this is where I need the delete button--->
        <td>
            <input type="submit" name="btnDelete#myQuery.currentrow#" value="Delete" onclick="return validateForm(this.id,this.form);">
     </td>
     </tr>
</cfoutput>
<tr>
     <td>
            <input type="submit" name="btnSubmit" value="Save" onclick="return validateForm(this.id,this.form);">
     </td>
    <td>
            <input type="submit" name="btnUpdate" value="Update" onclick="return validateForm(this.id,this.form);">
     </td>

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
There are a few different ways to handle this.

This way uses the delete button and changes a hidden ID field that identifies which record to delete...

 
<cfelseif IsDefined('form.btnDelete')>
  delete where ID = #form.deleteID#
  <!---delete query--->
</cfif>

<!---and my form page looks like this--->
<table>
<cfoutput query="myQuery">
    <tr>
        <td>#name#</td>
        <td>#division#</td>
       <!---this is where I need the delete button--->
        <td>
            <input type="submit" name="btnDelete" value="Delete" onclick="return validateForm(this.id,this.form);document.getElementById('deleteID')='#myQuery.PrimaryKey#';">
     </td>

Open in new window


I think this is a nicer way.   Put a checkbox next to each item, if the user checks it, they want to delete it.  That allows the user to delete more than one item at a time (rather than delete, refresh page, delete, refresh page...)

To do this, it's very easy... just a checkbox with the ID of the record to delete..

 
<cfif isDefined("form.deleteID")>
 <cfquery...>
  delete from myTable where ID in (#form.deleteID#)
 </cfquery>
</cfif>
<!---and my form page looks like this--->
<table>
<cfoutput query="myQuery">
    <tr>
        <td>#name#</td>
        <td>#division#</td>
       <!---this is where I need the delete button--->
        <td>
         <input type="checkbox" name="deleteID" value="#myQuery.primaryKey#">
     </td>
     </tr>
</cfoutput>
<tr>

Open in new window

Author

Commented:
@gde:

With the first method, don't I need to set the name of each delete button dynamically, like so:

 <input type="submit" name="btnDelete#myQuery.currentrow#" value="Delete" onclick="return validateForm(this.id,this.form);document.getElementById('deleteID')='#myQuery.PrimaryKey#';">

And the same with second method, don't I have to set the name of the checkbox dynamically?

<input type="checkbox" name="deleteID#myquery.currentrow#" value="#myQuery.primaryKey#">

> With the first method, don't I need to set the name of each delete button dynamically, like so:

nope, the name of the button is used only to tell your action page what to do.

Here, we test to see if the "delete" button was pressed, you only need one name for that..

<cfelseif IsDefined('form.btnDelete')>

   delete where ID = #form.deleteID#   <!----- then here, you use the ID form field to know which to delete
   <!---delete query--->
</cfif>


>  And the same with second method, don't I have to set the name of the checkbox dynamically?

nope, I like this approach better...

This is how checkboxes are designed to work.   If you have 10 checkboxes all named "favoriteColors" and the user clicks on Red, Green and Blue, you will get a variable  form.favoriteColors with the value "red,green,blue"

Just like in this case, you will have one form field FORM.DELETEID which will contain a comma delimited list of all the IDs you have to delete...

then it's easy to do the delete statements...

 delete form myTable where ID in (#form.deleteID#)  

which is just like ...

 delete form myTable where ID in (12, 34, 102, 234)  

so you delete a list of them at once..