We help IT Professionals succeed at work.

check that a checkbox is checked on click of a button

armanbena
armanbena asked
on
478 Views
Last Modified: 2012-08-14
Hi,

I am working with a form, which contains a checkbox.  The user clicks a button.
I want to run a specific query, based on whether or not the checkbox has been checked.  So far I have been testing the value of the checkbox with no luck, and have a cfparam set to default ="" so that I don't get an error when loading to the page.
What am I doing wrong?
<form name="FrmOBI" method="post" action="save2.cfm"> 

      
          <table width="66%"  align="center" border="0" >
            <tr>  <td> <div align="center">Ready for review? <input name="ready" type="checkbox"  id="ready" value="1"/></div></td></tr>
          </table>

Open in new window

<script type="text/javascript">
function ChkData()
{

//if(document.FrmOBI.ready.checked == true)



//alert(document.FrmOBI.ready.Checked==true);
//
<cfif multipleList.RecordCount gt 0>
<cfloop query="multipleList">
		<cfif len(multipleList.NPSGroupNumber) gt 0>
			 <cffile action="write"  
		file="\\nwn401av317\y_b_g-dev\intranet\Agent_Editor\OBI\#multipleList.LOB#\#multipleList.NPSGroupNumber##multipleList.NPSGroupSection##multipleList.BPIDReg#bnftchart.obi" 
		output="#data1#" />
		<cfelse>
			 <cffile action="write"  
			file="\\nwn401av317\y_b_g-dev\intranet\Agent_Editor\OBI\#multipleList.LOB#\#multipleList.GroupNo#bnftchart.obi" 
			output="#data1#" />
		</cfif>
	<!---<cfif form.ready eq 1>--->
	<!---<cfif "FORM.ready.checked">--->
	<!---<cfif form.ready.checked>--->
	<cfif form.ready EQ "1">


			<CFQUERY DATASOURCE="Obi-default"  NAME="UpdateList" >
			  UPDATE OBI_Agent_Editor
			  SET Modified_By = #submittedByQry.employeeNumber#,
			  Modified_Date =  #Now()#,
			  Ready_Review = 1
			 WHERE TrackingNumber = #multipleList.TrackingNumber#
			</CFQUERY> 
	<cfelse>
			 <CFQUERY DATASOURCE="Obi-default"  NAME="UpdateList" >
			  UPDATE OBI_Agent_Editor
			  SET Modified_By = #submittedByQry.employeeNumber#,
			  Modified_Date =  #Now()#
			 WHERE TrackingNumber = #multipleList.TrackingNumber#
			</CFQUERY>
	</cfif>	
	</cfloop>  
	
<cfelse>
		<cfif form.ready EQ "1">
				<CFQUERY DATASOURCE="Obi-default"  NAME="Update" >
				  UPDATE OBI_Agent_Editor
				  SET Ready_Review = 1
				 WHERE TrackingNumber = #ListResult.TrackingNumber#
				</CFQUERY>  
	</cfif>

</cfif>	
	
	document.FrmOBI.submit();
}

</script>

Open in new window

Comment
Watch Question

Top Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
check Request("ready") on server side, if it is 1 use one query, o/w use another query...
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
The user clicks a button. I want to run a specific query, based on whether or not the checkbox has been checked.  

If you mean run a query whenthey check the button, it can't be done on the same page because javascript code runs long after the CF code is finished.  You have to submit another request to the server via ajax OR run the query when the form is submitted

ie

<!--- the form was submitted --->
<cfparam name="form.ready" default="">
<cfif stuctKeyExists(FORM, "submitBtn")>
      .....
     <cfif form.ready EQ "1">
            run a query
     <cfelse>
            run some other query
     </cfif>
     ....
</cfif>


<form name="FrmOBI" method="post" action="save2.cfm">
       ....
            <tr>  <td> <div align="center">Ready for review? <input name="ready" type="checkbox"  id="ready" value="1"/></div></td></tr>
       ....
      <input type="submit" name="submitBtn">
</form>

Author

Commented:
'The user clicks a button. I want to run a specific query, based on whether or not the checkbox has been checked.  '

Sorry, what I meant to say is the user will have already checked the box before clicking ont he button. When they click on the button, that is where I need to check, the code for queries is in place before I submit().
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
(Arghh... the new site ate my last post. I'll try and rephrase it)

Maybe I'm misunderstanding, but if you mean run the queries after the page is displayed - but before the user submits the form - it's not possible without ajax.  

CF code always runs 1st no matter where it's placed.  So what's actually happening with the original code is:

1) User opens their browser and requests the page
2) CF server grabs the *.cfm script and runs all the CF code it finds.
    <cfif multipleList.RecordCount gt 0>
       <cfloop query="multipleList">
      ...
     </cfif>

3) The CF server then returns html/javascript back to the browser.  So all the browser ever sees is:

<script type="text/javascript">
    function ChkData()
   {
       //if(document.FrmOBI.ready.checked == true)
      //alert(document.FrmOBI.ready.Checked==true);
     //
     document.FrmOBI.submit();
  }
</script>
<form name="FrmOBI" method="post" action="save2.cfm">
....

Author

Commented:
Okay, @agx, I tried using the <cfif structKeyExists(FORM, "submitBtn")> as you said, and now when I get to the page, I am looking at a blank screen.  I want to say, it just ran through all of that as the page loaded. Here is the full-code of what I have now:
<cfsilent>
<cfif not isdefined('FORM.ready') >
        <cfparam name="form.ready" default=""/>
</cfif>


<cfinvoke component="wf-test" method="getEmployeeInfo" returnvariable="employeeInfo">
      <cfinvokeargument name="employeeNumber" value="#form.submitted_By#">
</cfinvoke>

<cfset form.submit = 0 />

<cfset userObj = createObject("component", "wf-test")>
	<!--- userObj is now component --->
	<cfset submittedByQry = userObj.getEmployeeInfo(val("#form.submitted_By#"))>

	<CFQUERY DATASOURCE="Obi-default"  NAME="ListResult"  >
		select *
		from OBI_Agent_Editor
        where TrackingNumber = #url.id#
	</CFQUERY>
    
    <cfif ListResult.RecordCount>
    	<cfset multipleList.RecordCount = 0>
    </cfif>
    
<!---     <CFQUERY DATASOURCE="Obi-default"  NAME="UpdateList" >
      UPDATE OBI_Agent_Editor
      SET Modified_By = #submittedByQry.employeeNumber#,
      Modified_Date =  #Now()#
      WHERE TrackingNumber = #url.id#
    </CFQUERY>--->
                    
     <cfif len(ListResult.NPSGroupNumber) eq 0> 
    <cffile action="read"  
    file="\\nwn401av317\y_b_g-dev\intranet\Agent_Editor\OBI_Status_Check-MA\Xinha\examples\temp#ListResult.GroupNo#bnftchart.obi" 
    variable="data"/>
    
    <cfelse>
    
    <cffile action="read"  
    file="\\nwn401av317\y_b_g-dev\intranet\Agent_Editor\OBI_Status_Check-MA\Xinha\examples\temp#ListResult.NPSGroupNumber##ListResult.NPSGroupSection##ListResult.BPIDReg#bnftchart.obi" 
    variable="data"/>
    
    </cfif>
    
    
<cfinvoke component="createFileC" method="createEditorFile1" returnvariable="data1">
      <cfinvokeargument name="data" value="#data#">
      <cfinvokeargument name="GroupNo" value="#ListResult.GroupNo#">
      
    <cfinvokeargument name="NPSGroupNumber" value="#ListResult.NPSGroupNumber#">
    <cfinvokeargument name="NPSGroupSection" value="#ListResult.NPSGroupSection#">
    <cfinvokeargument name="BPIDReg" value="#ListResult.BPIDReg#">
   
</cfinvoke>
             
    
    
   <cfif len(ListResult.NPSGroupNumber) eq 0>
         <CFQUERY DATASOURCE="Obi-default"  NAME="multipleList" >
  
      SELECT * 
FROM OBI_Agent_Editor as a
where a.SheildServiceKeyReg 

				 in (select SheildServiceKeyReg
						from OBI_Agent_Editor
					where TrackingNumber = #url.id#)
								
and  a.SheildServiceKey65 

				 in (select SheildServiceKey65
						from OBI_Agent_Editor
					where TrackingNumber = #url.id#)

and  a.CrossServiceKeyReg 

				 in (select CrossServiceKeyReg
						from OBI_Agent_Editor
					where TrackingNumber = #url.id#)

and  a.CrossServiceKey65 

				 in (select CrossServiceKey65
						from OBI_Agent_Editor
					where TrackingNumber = #url.id#)
 and TrackingNumber!= #ListResult.TrackingNumber#                   
                    
    </CFQUERY>
   
     <cffile action="write"  
    file="\\nwn401av317\y_b_g-dev\intranet\Agent_Editor\OBI\#ListResult.LOB#\#ListResult.GroupNo#bnftchart.obi" 
    output="#data1#" />

<cffile action="delete"
file="\\nwn401av317\y_b_g-dev\intranet\Agent_Editor\OBI_Status_Check-MA\Xinha\examples\temp#ListResult.GroupNo#bnftchart.obi" />

	<cfelse>
      <CFQUERY DATASOURCE="Obi-default"  NAME="multipleList" >
     select *
		from NF_WMI
      where BPIDReg = '#ListResult.BPIDReg#'
      and TrackingNumber != #url.id#
    </CFQUERY>
    
        <cffile action="write"  
    file="\\nwn401av317\y_b_g-dev\intranet\Agent_Editor\OBI\#ListResult.LOB#\#ListResult.NPSGroupNumber##ListResult.NPSGroupSection##ListResult.BPIDReg#bnftchart.obi" 
    output="#data1#" />

<cffile action="delete"
file="\\nwn401av317\y_b_g-dev\intranet\Agent_Editor\OBI_Status_Check-MA\Xinha\examples\temp#ListResult.NPSGroupNumber##ListResult.NPSGroupSection##ListResult.BPIDReg#bnftchart.obi" />

    
    </cfif>
	
    
    <cfparam name="url.id" default=0 >

</cfsilent><head>
<script type="text/javascript">
function submit()
{
document.FrmOBI.submit();

}

<!---<head>
<script type="text/javascript">
function ChkData()
{--->

//}

//if(document.FrmOBI.ready.checked == true)

//document.FrmOBI.submit();

//alert(document.FrmOBI.ready.Checked==true);
//--->

<cfif structKeyExists(FORM, "submitBtn")>
<!---<cfif IsDefined("Form.submitBtn")>--->

<cfif multipleList.RecordCount gt 0>
<cfloop query="multipleList">
		<cfif len(multipleList.NPSGroupNumber) gt 0>
			 <cffile action="write"  
		file="\\nwn401av317\y_b_g-dev\intranet\Agent_Editor\OBI\#multipleList.LOB#\#multipleList.NPSGroupNumber##multipleList.NPSGroupSection##multipleList.BPIDReg#bnftchart.obi" 
		output="#data1#" />
		<cfelse>
			 <cffile action="write"  
			file="\\nwn401av317\y_b_g-dev\intranet\Agent_Editor\OBI\#multipleList.LOB#\#multipleList.GroupNo#bnftchart.obi" 
			output="#data1#" />
		</cfif>
	<!---<cfif form.ready eq 1>--->
	<!---<cfif "FORM.ready.checked">--->
	<!---<cfif form.ready.checked>--->
	<cfif form.ready EQ "1">


			<CFQUERY DATASOURCE="Obi-default"  NAME="UpdateList" >
			  UPDATE OBI_Agent_Editor
			  SET Modified_By = #submittedByQry.employeeNumber#,
			  Modified_Date =  #Now()#,
			  Ready_Review = 1
			 WHERE TrackingNumber = #multipleList.TrackingNumber#
			</CFQUERY> 
	<cfelse>
			 <CFQUERY DATASOURCE="Obi-default"  NAME="UpdateList" >
			  UPDATE OBI_Agent_Editor
			  SET Modified_By = #submittedByQry.employeeNumber#,
			  Modified_Date =  #Now()#
			 WHERE TrackingNumber = #multipleList.TrackingNumber#
			</CFQUERY>
	</cfif>	
	</cfloop>  
	
<cfelse>
		<cfif form.ready EQ "1">
				<CFQUERY DATASOURCE="Obi-default"  NAME="Update" >
				  UPDATE OBI_Agent_Editor
				  SET Ready_Review = 1
				 WHERE TrackingNumber = #ListResult.TrackingNumber#
				</CFQUERY>  
	</cfif>

</cfif>	
</cfif> 
<!---	document.FrmOBI.submit();
	//document.FrmOBI.submit();
}

</script>
--->



<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>OBI Status Check Tool</title>
    
	<link rel="stylesheet" type="text/css" href="../../css/structure.css">
	<link rel="stylesheet" type="text/css" href="../../css/form.css">

</head>
<img src="images/trial1.jpg" align="CENTER" width="100%">

  <body id="public">

		<cfset userObj = createObject("component", "wf-test")>
			<!--- userObj is now component --->
	<cfset submittedByQry = userObj.getEmployeeInfo(val("#form.submitted_By#"))>
 <form name="FrmOBI" method="post" action="save2.cfm"> 

	<div id="container">
			<h1 id="logo">
 		 	</h1>
 
    <div align="center">
		<cfset name ="test" /> 
    </div>
    
    <div class="info" >
    	<h2 align="center" >Benefits Modified</h2>
    </div>

    
    <div align="center">  
     <div class="info">
 <br/> 
      
          <table width="66%"  align="center" border="0" >
		    <tr>
             <td colspan="4"><div align="center"><b>The Benefit Chart has been saved.</b></div></td></tr>
            <tr>  <td> <div align="center">Ready for review? <input name="ready" type="checkbox"  id="ready" value="1"/></div></td></tr>
          </table>
          
		  <cfif multipleList.RecordCount gte 1>
		   		<!---<table>
            			<tr>
		      			<td colspan="4">---><b>The following are a list of benefits with matching Service Keys/BPIDs. Use benefit saved for the following benefits?</b><!---</td></tr>--->
            
           				 <div style="overflow:auto; height:300px;">
            
                        <table class="table-data-list" >
                          <tr>
                            <cfif len(ListResult.NPSGroupNumber) eq 0>
                                <th>Group Number</th>
                            <cfelse>
                                <th>NPS Group Number</th>
                            </cfif>
                            <th>Group Name</th>
                          </tr>
						<cfoutput query="multipleList" >
                         
                         <tr class="#IIf(CurrentRow Mod 2, DE('odd'), DE('even'))#" onMouseOver="this.className='on';"
                         <cfif CurrentRow Mod 2>					      
                            onmouseout="this.className='odd'"
                        <cfelse>
                            onmouseout="this.className='even'"</cfif>>
                          
                                <cfif len(ListResult.NPSGroupNumber) eq 0>
                                    <td>#multipleList.GroupNo#</td>
                                <cfelse>
                                    <td>#multipleList.NPSGroupNumber##multipleList.NPSGroupSection#</td>
                                </cfif>
                                <td>#multipleList.GroupNameLong#</td>
                          </tr>  
                               </cfoutput>
                             </table>   
                               </div>
              </cfif>

     
     <table>
           <tr>
             <td width="20%">&nbsp;</td>
          <td width="40%"><div align="center">  <input type="submit" name="submitBtn">
<!---<input type = "submit" value="submit" name="submit" onclick="ChkData();" />--->  <!---<a href="javascript: ChkData()" class="button" title="Save">Save/Process</a>---></td>    <!--- <a name="test" href="#nogo" onclick="submit()"  class="button" title="Save" >Save/Process</a>      
		<input name="test" type="submit" value="test" onclick="submit()" />--->
		 <!--- <cfif form.submitBtn eq submit>
		  	<input type="hidden" name="trial" value="5" />
		<cfelse>
			<input type="hidden" name="trial1" value="3" />
		</cfif>	 --->
			
		     </td>
          
          <td width="40%"> <div align="center"> <a href="javascript: window.close()" class="button" title="Cancel">Close Window Without Saving</a>            </div>
         
          <p>&nbsp;</p></td>
          <td width="36%">&nbsp;</td>
        </tr>
       </table>
            


</div>
         </form>
         </body>

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
The code should placed on the action page (unless you're posting to the same page). If so, move the update code outside of the <script> tags and add some debugging to see what's happening when you submit theform.

<cfif structKeyExists(FORM, "submitBtn")>
     button defined<br>
    <cfif multipleList.RecordCount gt 0>
         beginning loop....<br>
        <cfloop query="multipleList">  
           .... loop and query code ....
    <cfelse>
         query not defined. <br>
    </cfif>
<cfelse>
         button not defined
</cfif>

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.