• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to know a form was submitted via: onchange='this.form.submit()'

New Here ,
Mar 25, 2016 Mar 25, 2016

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...

01

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

01

<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>

10XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

**************


Here are the dump results with and without the submit button..


any Ideas would be greatly appreciated

Thanks


question-javascript-submit.png

Views

2.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Mar 25, 2016 Mar 25, 2016

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.

Votes

Translate

Translate
Advocate ,
Mar 25, 2016 Mar 25, 2016

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 28, 2016 Mar 28, 2016

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

<cfform name="testit" action="test.cfm">

<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

</cfform>


<script language="JavaScript">

function submitit()

{

   document.getElementById('savesw').value='getorder' ;

   document.testit.submit();

}

<script>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 26, 2016 Mar 26, 2016

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 28, 2016 Mar 28, 2016

Copy link to clipboard

Copied

If I may be a bit pedantic.

<cfif StructKeyExists(form,"fieldnames")>

I've heard of  isDefined() giving false responses.

HTH,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2016 Mar 28, 2016

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").

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 28, 2016 Mar 28, 2016

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2016 Mar 28, 2016

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 28, 2016 Mar 28, 2016

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2016 Mar 28, 2016

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 07, 2020 Feb 07, 2020

Copy link to clipboard

Copied

LATEST

@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

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation