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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
onOpen 14 43
After moving to webstite page stopped working 7 52
two tables one button 11 20
Datepicker in PHP 9 21
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now