?
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
Medium Priority
?
219 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 1500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 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