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
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

705 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