Copy link to clipboard
Copied
I'm hoping there might be a easy answer to the issue below.
I have a page with a form and the form submit calls the exact same page the form is on. When I check the form variables after the button submit the form.button2 variable exist and has a value when I use a button to submit the page.
However, when submitting the page via onchange='this.form.submit()' no form variable is updated to reflect that the user submitted the page.
Is there a way to force a form variable to update so I can see that the user came form this control via javascript?
See sample code below and screen shot below
example code...
| xxxxxxxxxxxxxxx Example with submit button. xxxxxxxxxxxxxxxxx |
02 |
03 | <cfdump var= "#form#" label= "testit" > |
04 |
05 | <cfif isdefined( "form.button2" )> |
06 | <!--- Do somthing Here---> |
07 | </cfif> |
08 |
09 | <cfform name= "testit" action= "test.cfm" > |
10 | < select name= 'myfield' > |
11 | <option selected= "selected" > --- Pick Drink ---</option> |
12 | <option >Milk</option> |
13 | <option>Coffee</option> |
14 | <option>Tea</option> |
15 | </ select > |
16 | <input type = "submit" name= "button2" value= "Submit" > |
17 | </cfform> |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Notice, when I use the onchange='this.form.submit()' then there no button and nothing to test to see that the user came from this controlxxxxxxxxxxxxxxx Example with javascript no button.
xxxxxxxxxxxxxxxxx
| <cfdump var= "#form#" label= "testit" > |
02 | <cfform name= "testit" action= "test.cfm" > |
03 | < select name= 'myfield' onchange= 'this.form.submit()' > |
04 | <option selected= "selected" > --- Pick Drink ---</option> |
05 | <option >Milk</option> |
06 | <option>Coffee</option> |
07 | <option>Tea</option> |
08 | </ select > |
09 | </cfform> |
10 | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
**************
Here are the dump results with and without the submit button..
any Ideas would be greatly appreciated
Thanks
Use a hidden field instead of relying on the button value. Button values are only present if the user actually clicks the button. Using the form submit event via code or even the user pressing enter instead of clicking on the button will result is no button value being posted.
Copy link to clipboard
Copied
Use a hidden field instead of relying on the button value. Button values are only present if the user actually clicks the button. Using the form submit event via code or even the user pressing enter instead of clicking on the button will result is no button value being posted.
Copy link to clipboard
Copied
That worked great!!!
changed my code to be the following and all works perfect now.
I just check formfield savesw for the value "getorder"
Thank you
<cfdump var= "#form#" label= "testit" > |
02 |
<cfinput name="savesw" type="hidden" value="ss" /> |
03 | < select name= 'myfield' onchange= 'submitit()' > |
04 | <option selected= "selected" > --- Pick Drink ---</option> |
05 | <option >Milk</option> |
06 | <option>Coffee</option> |
07 | <option>Tea</option> |
08 | </ select > |
09 |
<script language="JavaScript"> function submitit() { document.getElementById('savesw').value='getorder' ; document.testit.submit(); }
|
Copy link to clipboard
Copied
<cfif isdefined(
"form.button2"
)>
06
<!--- Do somthing Here--->
07
</cfif>
<cfif isdefined("form.fieldnames")><!--- Form submitted --->
<cfif isdefined("form.button2")>
<!--- Button submission --->
<cfelse>
<!--- Javascript submission--->
</cfif>
</cfif>
Copy link to clipboard
Copied
Copy link to clipboard
Copied
WolfShade wrote:
<cfif StructKeyExists(form,"fieldnames")>
I've heard of isDefined() giving false responses.
As far as I know, isDefined("form.someFieldname") is practically equivalent to structKeyExists(form,"someFieldname").
Copy link to clipboard
Copied
Practically, maybe. But from all the researching that I've done on it, isDefined() will still search variables, query, cgi, and other scopes, even if you define the scope, because CF allows for dots in variable names.
So, even if you do <cfset myScope.varNameA = 'foo'>, then <cfif isDefined("myScope.varNameA")>, isDefined() will look through variables, query, cgi, cffile, url, form, cookie, and client scopes first, before realizing that there is a myScope "scope".
But StructKeyExists(myScope,"varNameA") will look for a scope called "myScope", and when it finds myScope will search only myScope for a variable called "varNameA". If myScope doesn't exist, StructKeyExists() returns false. So, off the bat isDefined() is inefficient and (IMHO) should only be used if you are not sure what scope a variable will be in. But, I scope all my variables, and always use the scope when referring to them.
Also, I've seen cases where isDefined() breaks (gives a false 'yes' or 'no') when it comes to session variables.
V/r,
^_^
Copy link to clipboard
Copied
WolfShade wrote:
Practically, maybe. But from all the researching that I've done on it, isDefined() will still search variables, query, cgi, and other scopes, even if you define the scope, because CF allows for dots in variable names.
If you use isDefined("form.someFieldname"), Coldfusion wont search through the scope variables, query, cgi, etc.
Copy link to clipboard
Copied
With all due respect, BKBK, and I do respect both you and your knowledge of CF - I've been searching Google for a while on this topic, and I have yet to see a single post, blog, or article that says isDefined() is as good or better than StructKeyExists() (the one exception being an Adobe forums back-and-forth between you and Adam Cameron, whereby you defend isDefined().)
The closest I've seen is someone stating that in this day and age, the difference is marginal - but that does not express or imply that StructKeyExists() is NOT better than isDefined().
What I've seen, so far, has stated that StructKeyExists() is better than isDefined() if for no other reason than (since CF8) isDefined() does search through all scopes (that's just one example), regardless of whether or not a scope has been defined within isDefined().
If you can find a reliable online resource that is contrary to this, I would be more than happy to look at it and keep an open mind.
V/r,
^_^
Copy link to clipboard
Copied
Hi WolfShade, I too do have respect for your Coldfusion knowledge. Your isDefined versus structKeyExists argument has a lot of sway, and is true in general. What I wished to add is that, in the particular case where one specifies the form scope, as in isDefined("form.someFieldname"), Coldfusion wont search through the various scopes query, cgi, variables, etc.
Similarly, if you use isDefined("variables.someVar") or isDefined("arguments.someArg") Coldfusion wont search through the various scopes either. Nevertheless, your point, that structKeyExists(someCustomStructure, "someKey") is more efficient than isDefined("someCustomStructure.someKey") is unimpeachable.
Copy link to clipboard
Copied
@BKBK, WolfShade is right. Check out the below url as it is validated by Adobe Community.
Thanks WolfShade.
https://community.adobe.com/t5/coldfusion/structkeyexists-vs-isdefined/td-p/2143183?page=1