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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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)

789 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