how to differentiate between javascript submit and manually clicking submit
Asked Answered
M

1

6

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>
Musicology answered 19/5, 2015 at 9:8 Comment(6)
You know when my function is invoked it is javascript triggered submit.Moynahan
take a hidden input and supply a different value with javascript submit. ie <input type="hidden" value="manual" name="submittype"> in javascript set ...submittype.value = 'js'Condyloma
if i attach a onclick event to <input type="submit" name="submit" id="btnSearch" value="Search" class="button_example" onclick="validate()" > it will call myfunction() and validate()Musicology
and what does validate() do? only clicking the "real" submit button won't trigger myfunction() then...Leathaleather
but myfunction() triggers validate() and validate function may set some variable like real submit var realsubmit="yes"Musicology
any way using type="hidden" i could differentiate it ,thanks for ur answersMusicology
L
1

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

Liquefacient answered 19/5, 2015 at 19:14 Comment(1)
And by the way, when JS is not enabled in browser, the onsubmit event won't trigger. Then the form is not polluted with an hidden input for this case.Baboon

© 2022 - 2024 — McMap. All rights reserved.