9 Replies Latest reply on Mar 28, 2016 12:48 PM by BKBK

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

    Heff2000 Level 1

      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

        • 1. Re: How to know a form was submitted via:  onchange='this.form.submit()'
          Steve Sommers Level 4

          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.

          • 2. Re: How to know a form was submitted via:  onchange='this.form.submit()'
            BKBK Adobe Community Professional & MVP

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

            • 3. Re: How to know a form was submitted via:  onchange='this.form.submit()'
              WolfShade Level 4

              If I may be a bit pedantic.

               

              <cfif StructKeyExists(form,"fieldnames")>
              

               

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

               

              HTH,

               

              ^_^

              • 4. Re: How to know a form was submitted via:  onchange='this.form.submit()'
                BKBK Adobe Community Professional & MVP

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

                • 5. Re: How to know a form was submitted via:  onchange='this.form.submit()'
                  WolfShade Level 4

                  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,

                   

                  ^_^

                  • 6. Re: How to know a form was submitted via:  onchange='this.form.submit()'
                    Heff2000 Level 1

                    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>

                    • 7. Re: How to know a form was submitted via:  onchange='this.form.submit()'
                      BKBK Adobe Community Professional & MVP

                      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.

                      • 8. Re: How to know a form was submitted via:  onchange='this.form.submit()'
                        WolfShade Level 4

                        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,

                         

                        ^_^

                        • 9. Re: How to know a form was submitted via:  onchange='this.form.submit()'
                          BKBK Adobe Community Professional & MVP

                          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.