Solved

When submitting a form using OnChange how can I pass a variable to determine if the submit button was clicked or the option field was changeed?

Posted on 2008-10-11
1
214 Views
Last Modified: 2012-05-05
My current script submits the form if a certain option field is changed.  The form is submitted back to the same page for processing with PHP.  The problem is that this triggers my form validation code, and puts a message on the screen prompting the user to correct those errors.

I need to be able to tell if the submit button was pressed, or if 'onchange' submitted the form so that the program logic can decide whether or not to validate the data.  How can I do this?
<form id="invoiceInput" name="invoiceInput" method="POST" action="<?php echo $editFormAction; ?>">
  <select name="invoiceMonth" id="invoiceMonth" onChange="JavaScript:form.submit()">
    <option value=""> </option>
    <option value="months">Months</option>
  </select>
</form>

Open in new window

0
Comment
Question by:CheeserTheAverage
1 Comment
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22695508
>>The problem is that this triggers my form validation code
If this validation is taking place on the server, include a hidden field named "trigger" and when select changes, set it to the id of the select. IF you see trigger has the id of the select you know it was set by the select, otherwiser it was sent by the submit:
<form id="invoiceInput" name="invoiceInput" method="POST" action="<?php echo $editFormAction; ?>">
 <input type="hidden" name="trigger" value="submit"/>
  <select name="invoiceMonth" id="invoiceMonth" onChange="trigger.value=this.id; form.submit()">
    <option value=""> </option>
    <option value="months">Months</option>
  </select>
 <input type="submit" value="Submit" onclick="trigger.value=this.value;"/>
</form> 
NOTE: even if the validation is taking place on the browser via javascript, the principle still applies. On your javascript, if you see trigger=="invoiceMonth" then you know it was NOT initiated by the submit.

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question