Coldfusion CFMESSAGEBOX Passing Variables

What im trying to do is simple, im sure.. Im just a bit rusty when it comes to these things..

Essentially all im trying to do is have a Confirm Delete Message Box popup when a button is pressed next to all my DB rows listed on the page -- then after "Confirm" is pressed it proceeds to reload the page and process the record delete

<script  type="text/javascript"> 

    var confirmDel = function(btn){ 


Open in new window

and heres the call button code:

<cfform action="ADMIN_LIST_DELETE.cfm?Delete=true&ID=#ID#" method="post" name="DelListing">
         <cfinput name="Prompt" type="button" value="DELETE LISTING"  onclick="showMB('confimdelete')"> 
               <cfmessagebox name="confimdelete" type="confirm"  message="Are you sure you wish to Delete this Listing?"  
                  labelNO="CANCEL" labelYES="CONFIRM DELETE"  callbackhandler="confirmDel" icon="warning"/> 

Open in new window

My problem is that the CF Box is not passing over the #ID# variable when it reloads the page.

another issue i just noticed is that it processes the fucntion regardless of which option you choose! Confirm = Run script and Cancel = also runs same script instead of just cancelling the message box.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pravin AsarPrincipal Systems EngineerCommented:

I happened have implemented similar case.
So here is fully functional code. Feel free to modify to your needs.

<cfset id=10/>
<cfset deleteLink = "cfmessageTest.cfm?Delete=true&ID=#ID#">

<!--- This is for test only --->      
<cfif isdefined ("url.delete")>
      <cfif url.delete eq true>
            <cfoutput>Now I will delete the file. and show the detailed deletetion message error</cfoutput>
            <cfdump var="#url#"/>
<script  type="text/javascript">

      var dbgFlag = false;
      // Save Coldfusion value as Javascript variable
      var deleteID = <cfoutput>#id#</cfoutput>;
      // show Message Box
      function showMB(mbox)  {;
      // Callback confirmation for Delete
    var confirmDel = function(btn){

            if (dbgFlag) {
                  alert ('You clicked on ' + btn + ' Button');
                  alert ("Delete Link " + <cfoutput>"#deleteLink#"</cfoutput> );
            // If yes is pressed, proceed to delete
            if (btn.match("yes")) {
             // Redirect the navigate action to CFWindow.
             // This is optional, but nice to have. User does not have to leave current page
             // This window can be closed.
             // Now show the window.
<style type="text/css">
      * { font-size: 24px; }
<!--- CF form --->
<cfform action="#deleteLink#" method="post" name="DelListing">
         <cfinput name="Prompt" type="button" value="DELETE LISTING"  onclick="showMB('confimdelete')">
               <cfmessagebox name="confimdelete" type="confirm"  message="Are you sure you wish to Delete this Listing?"  
                  labelNO="CANCEL" labelYES="CONFIRM DELETE"  callbackhandler="confirmDel" icon="warning"/>

<!--- CF Windows, hidden initailly --->
<cfwindow name="TestWindow" center="true" width="800" height="600" closable="true" title="ImageView" initshow="false">Initial Text Here</cfwindow>  

JCDesignsAuthor Commented:
Thanks. I understand what you've done there. That should do the trick. I'll give it a try once I'm back Infront of a computer.
JCDesignsAuthor Commented:
Sorry for the delay.. but i finally got a chance to try this.. but the problem is setting that initial #ID# value.
In your example, you simply cfset it to 10, but on my page i have every record being displayed, and each has its own ID & button. I need to pass that ID var into the URL depending on which "Delete" button was pressed.

Thats sorta where my problem lies initially.
The reason you're having troubles is that cfmessagebox really wasn't designed for this.  The callback function only passes the "text" of whatever button was clicked.  As you discovered, there's no simple way to pass extra parameters like an "ID".  You have to use some sort of work around. For example, you can use ColdFusion.MessageBox.update() to create the callback function dynamically, and pass the selected ID into that function (see working example below - tested with CF10)

Personally, I think you're better off using jQuery.  A simple Dialog will do what you need. Unfortunately, CF's UI stuff is notoriously brittle and the underlying JS out of date.  CF also has a nasty habit of changing the UI stuff from version to version, so there's no guarantee JS/CSS fixes will work from one version to the next.

Edit:  Fix typo in variable name

<!--- for demo only: sample query --->
<cfset qData = queryNew("")>
<cfset queryAddColumn(qData, "ID", [8,14,16])>
<cfset queryAddColumn(qData, "Name", ["Item A","Item B","Item C"])>

<script  type="text/javascript"> 
   function confirmDelete(id){ 
      // grab message box
      var box = ColdFusion.MessageBox.getMessageBoxObject('confimDelete');
      // create new callback function with selected ID
           , {callbackhandler : function(btn){ 
                               if (btn == "yes") {
                                      alert("Debug message: Deleting id = "+ id);
                                      ColdFusion.navigate("ADMIN_LIST_DELETE.cfm?Delete=true&ID="+ id);
             // finally, display it

   <!--- display delete option for each record --->
   <cfoutput query="qData">
        #Name# <input type="button" onClick="confirmDelete(#ID#)" value="DELETE LISTING"><br>

<!--- create single message box --->
<cfmessagebox name="confimDelete" type="confirm"  
	message="Are you sure you wish to Delete this Listing?"  
        labelYES="CONFIRM DELETE"  

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.