3 Replies Latest reply on Jan 21, 2008 5:35 PM by Newsgroup_User

    Using a java script comfirmation in delete record function?

    Irish-Phoenix
      Hello;
      I am trying to write a confirmation script in Java to delete a record. I don't want it to delete the whole table of information, just the record teh delete button is attached to using the ID. Here is my script, and then the code on the page:

      <script language="JavaScript">
      function confirmDelete(id) {
      if (confirm ("Are you sure you want to delete this record?"))
      {
      document.HREF.action="ProjectCat-Action.cfm?id="+id;
      document.HREF.submit();
      }
      return false;
      }

      </script>

      Here is the query I am trying to use it with:
      <cfquery name="catMan" datasource="#sitedatasource#" username="#siteUserID#" password="#sitePassword#">
      SELECT Categories.Name AS ViewField1, Categories.CategoryID AS ID
      FROM Categories
      </cfquery>
      <head>
      </head>
      <body>
      <cfloop query="catMan" startRow="#URL.startRow#" endrow="#endRow#">
      <cfoutput>
      #ViewField1#
      <form action="ProjectCat-Action.cfm" method="post">
      <input type="hidden" name="ID" value="#ID#">
      <input type="submit" name="proj_Delete" onClick="return confirmDelete('#ViewField1#')" value="Delete"></form>
      </cfoutput>
      </cfloop>
      </body>

      #Note: there is code here in my loop query for next and previous buttons, I just didn't post that code because it doesn't pertain to this question.

      Is there also a way to make the Java Script state in the spot where it says, "Are you sure you want to delete this record" to make it say "Are you sure you want to delete #ViewField1#?" (the name of the record being deleted) ?

      Thank you!

      Phoenix
        • 1. Re: Using a java script comfirmation in delete record function?
          Dan Bracuk Level 5
          Since you can only submit one form at a time, it's probably simpler to put your js code right into the input tag. Here is one that I once wrote

          <input
          type="button"
          value="Delete"
          name="DeleteOneRecord" onclick="if(confirm('Delete this record?')){this.form.WhatToDo.value='DeleteOneRecord';this.form.submit()}">

          • 2. Re: Using a java script comfirmation in delete record function?
            Irish-Phoenix Level 1
            when you hit the cancle button on your code, it still sends you off to the action page. Is there a way to fix that? Or make mine just say do you want to delete ViewField1? (ViewField1 should state the name of the record being deleted.

            Right now, mine does delete the one record I am trying to delete and when you hit cancle, it doesn't do anything but close the script box, now i just want it to say the name of the record.
            • 3. Re: Using a java script comfirmation in delete record   function?
              Level 7
              just pass 2 arguments to your js function - id AND name of category:

              <input type="submit" name="proj_Delete" onClick="return
              confirmDelete(#ID#,'#ViewField1#')" value="Delete">
              and the function:
              function confirmDelete(id,name) {
              if (confirm ("Are you sure you want to delete " + name + " category?"))
              {
              document.HREF.action="ProjectCat-Action.cfm?id="+id;
              document.HREF.submit();
              }
              return false;
              }


              but creating a separate form for each category displayed on the page is
              very inefficient - you should revise your code... you do not even need a
              form, really... just use <a href=...> to call your js function, or an
              <input type=button ...> (without any <form ..> tags) if you absolutely
              must have a button to click...

              ---
              Azadi Saryev
              Sabai-dee.com
              http://www.sabai-dee.com