1

So I saw a lot of answer on similar question but didn't found how to exactly make it work. I have a lot of functions in query and all of them didn't complete because the html event load the next page. So what I need is how exactly make the html event wait till the jquery check everything that it should check.

so example of jquery is

jQuery.noConflict();
jQuery(document).ready(function($) {
    $(document).on("click","input[name=open]", function () {
        var login = $("input[name=field1]").val();
        var pas = $("input[name=field2]").val();
        alert('I call it');

        $.post("index.php", { 
            st: 1, 
            field1: field1, 
            field2: field2
        }, function(data) {
            alert(data);
            if ($.trim(data) == 'er') {
                alert("Sorry incorrect data");
                return false;
            }
            else
                return true;
        });
    });
});
<div>   
    <form method="POST" action="index.php?cd=in">
        <!----code here --->
        <div>
            <input name="open" class="field_submit" type="submit" size="7" value="IN" ">
        </div>

So the function works fine when there is not line with action="index.php?cd=in". So what happen is just start the function but didn't wait the result and do redirection. So there is couple ways how I can prevent it:

  1. redirect by jQuery but it not what I need
  2. is make in HTML file on tag input event onclick="return function-name();"

But the problem how to correct realize the jquery file function because when I put the function inside the console gives me an error as I the function is not defined. So the problem is how to exactly realize the function in jquery file that it work for onclick event in HTML file. Or if you know other better variant to realize it. Thanks

1 Answer 1

2

What you need is:

// bind submit event on form, not click of submit
// because a form can be submited without clicking submit button
$(document).on("submit", "form:has(input[name=open])", function(e) {
  e.preventDefault();  // prevent default behaviour
  var self = this; // keep ref on form
  var login = $(this).find("input[name=field1]").val();
  var pass = $(this).find("input[name=field2]").val();
  alert('I call it');
  $.post("index.php", {
    st: 1,
    login: login,
    pass: pass
  }, function(data) {
    alert(data);
    if ($.trim(data) == 'er') {
      alert("Sorry incorrect data");
      return false; // if error, return whatever value
    }
    self.submit(); // submit the FORM calling submit DOM API method (doesn't trigger jq submit handler)
  });

});
Sign up to request clarification or add additional context in comments.

13 Comments

and what about HTML file ? some onclick event?
@alex_mike I don't understand what you mean?! Which onclick event? EDIT: i edited answer because this.submit(); must be self.submit();. It could be this.submit(); only if passing context: this, as option of ajax request, which isn't possible using shorthand $.post() method
Oops, i forgot to prevent default behaviour, check it again now: function(e) { e.preventDefault(); ... });
ok thanks, but some how it just skip the hole function I think I make some mistake
You mean submit event isn't triggered or what?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.