Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to differentiate between javascript submit and manually clicking submit

how to differentiate between javascript triggered submit and manually clicking form submit
sample code below

 function myfunction()
 {
    document.getElementById("id_searchform").submit();
    return true;
 }

form:

<div class='row'>
   <div class='col-md-4'>
      <div class='clszipcode' ><span>Enter Zipcode</span></div>
   </div>
   <div class='col-md-4'>
      <div class='clstxtzipcode' ><input type="text" name="zip_code" id="txtZipcode"></div>
   </div>
   <div class='col-md-4'>
      <div class='clsbtnzip' ><input type="submit" name="submit" id="btnSearch" value="Search" class="button_example"  ></div>
   </div>
</div>
<a href="#"  onclick="return myfunction();" >click to submit</a>
like image 969
srinidhi Avatar asked May 19 '15 09:05

srinidhi


People also ask

How do you tell the difference between two submit buttons?

Put a hidden field. And when one of the buttons are clicked before submitting, populate the value of hidden field with like say 1 when first button clicked and 2 if second one is clicked. and in submit page check for the value of this hidden field to determine which one is clicked.

How do I automatically submit a form without clicking?

Submit a Form Using JavaScript The most simple way to submit a form without the submit button is to trigger the submit event of a form using JavaScript. In the below example we are going to create a function to submit a form. We will set that function at onclick event of a div tag.

What happens when the user clicks on the submit button?

5. Most HTML forms have a submit button at the bottom of the form. Once all of the fields in the form have been filled in, the user clicks on the submit button to record the form data. The standard behaviour is to gather all of the data that were entered into the form and send it to another program to be processed.

What is correct way to submit a form using JavaScript?

When we click on the link, the function submitForm() will get executed. This function will get the element object using DOM getElementById() method by passing the form id to this method, then the form will be submitted by using submit() method. Example: Create a form and submit it using the above approach.


1 Answers

Let me see if I understand:

  1. You want to detect whether the user clicked the link to submit the form.
  2. You want to detect if the user clicked the submit button to submit the form.
  3. You have another function called validate() which will use this information in some way.

If this is the case, consider using a variable to store whether the link was clicked before triggering the form to submit.

  1. Initialize global variable wasClicked to false
  2. When link is clicked set wasClicked to true
  3. Trigger form submit after wasClicked is set.
  4. Run validate() when form is submitted
  5. Check if(wasClicked){...} in validate()

Here is a Working Example

like image 155
Zeal Avatar answered Oct 04 '22 21:10

Zeal