0
votes

I have a form with some html elements. It has a check box which is by default checked. On click of the submit button it calls the submitForm function Based on the checkbox condition it has to do the action. If the checkbox is Y, then it has to do one form action and if not checked then another action. Using javascript I have checked whether its checked or not. But I am not able to set the coldfusion variable for this. Always it overwrites the variable. Below is the code snippet

This is the ColdFusion variable which is used. This is by default set to Y

 <cfset form_condn_var = 'Y'/>

 function submitForm(){
  if (document.getElementById('Show_Final_Model').checked) 
  {
form.Show_Final_Model.value = 'Y';
  }
 else{
<cfset form_condn_var = 'N'/>
 }
}

 <cfif '<cfoutput>#form_condn_var#</cfoutput>' eq 'Y'>
<form id="form1" action="test.cfm" method="POST" target="testmain">
 <cfelse>
<form id="form1" action="<cfoutput>#something#</cfoutput>" method="POST" target="_blank" onSubmit="">
</cfif>

It always set the variable form_condn_var as N and it goes to the else condition of the form irrespective of the condition. . But when I alert the value its comes correctly. I cannot use hidden variable also as the form is not being called initially. Based on the checked condition only it is accessed. Could somebody please tell me why the form_condn_var gets overwritten irrespective of the condition being checked. Or is there any other way I can achieve this?

Thanks in advance

3
I have read this a few times and your question is still unclear :) Forget about code for a moment. What is your goal in plain english? Here is my take: "My form has one checkbox. When the box is checked, I want to submit the form to somePage.cfm, otherwise I want to submit it to someOtherPage.cfm". Does that sound right?Leigh
You are mixing JS logic with CFML in your submitForm() function. You cannot use the JS condition to set a CF variable.Tristan Lee

3 Answers

0
votes

Short version If I have understood your logic correctly then you can replace all of what you pasted with the following <cfif>

<cfif IsDefined('form.Show_Final_Model') AND form.Show_Final_Model EQ 'Y'>
    <form id="form1" action="test.cfm" method="POST" target="testmain">
<cfelse>
    <form id="form1" action="<cfoutput>#something#</cfoutput>" method="POST" target="_blank" onSubmit="">
</cfif>

Explanation

Your main problem here is that you have got Javascript outside of tags with a coldfusion within them and seem to be confusing what each language does.

First of all Coldfusion renders the HTML. As far as Coldfusion is concerned, the javascript if statement is just text, and so it sees the following logic

<cfset form_condn_var = 'Y'>
<cfset form_condn_var = 'N'>
<cfif form_condn_var EQ 'Y'>  <!--- form_condn_var === '<cfoutput>#form_condn_var#</cfoutput>' but much cleaner --->
    .......
<cfelse>
    .......
</cfif>

In turn leading to the following HTML being rendered

function submitForm(){
    if (document.getElementById('Show_Final_Model').checked) 
    {
        form.Show_Final_Model.value = 'Y';
    }
    else{

    }
}

<form id="form1" action="<cfoutput>#something#</cfoutput>" method="POST" target="_blank" onSubmit="">

I suspect in your example you trimmed out some logic though as otherwise that Javascript would be output as plaintext, as it is not within <script> tags.

0
votes

If you are submitting your checkbox and then setting the form action for the next form (form 2 - after you've chosen the checkbox) I think what you want to do is the following:

<cfparam name="form.Show_Final_Model" default="N"/>

<cfif form.Show_Final_Model IS 'Y'>
<form id="form1" action="test.cfm" method="POST" target="testmain">
 <cfelse>
<form id="form1" action="<cfoutput>#something#</cfoutput>" 
    method="POST" target="_blank" onSubmit="">
</cfif>

This would be on the handler page (after the form is submitted). You would not need the JS function.

If however you are trying to change the action param of your form to something else based on the check or uncheck of the checkbox (in other words - withing the same form) then ALL of your code needs to be javascript and CF has little to do with it. Set 2 variables - action a and action b, check the value of the checked form element and change the form.action value to whichever one you want.

But most importantly get settled in your mind on what is "server side" and what is "client side" .. that's where you are slipping up. good luck :)

0
votes

I'm not sure if I'm understanding the question correctly, but it would seem that this situation could be handled purely on the action page based on conditional processing based on the result of the form checkbox value. Instead of sending the form to two different action templates based on the value of the checkbox, just process the form accordingly on the action template based on conditional processing of the checkbox form value.