-1

I try to insert data using ajax, to submit and it runs perfectly, but I add required on input and select option but not running, if anything wrong?

<form role="form" id="form-add"  class="form-horizontal form-label-left">       
    <div class="form-group">                    
        <label for="no_surat">Name * :</label>
        <input type="text" name="nm"  class="form-control" required />
    </div>
    <div class="form-group">                    
        <label for="no_surat">Age * :</label>
        <input type="text" name="ag"  class="form-control" required />
    </div>
    <div class="form-group">                    
        <label for="no_surat">Addres * :</label>
        <input type="text" name="ad"  class="form-control" required />
    </div>
    <button type="button" id="submit" class="btn btn-success"> Download </button>
</form>

AJAX

$("#submit").on('click', function(event) {
    event.preventDefault();
        $.ajax({
            type: "POST",
            url: base_url+"add",
            dataType: 'json',
            data: $('#form-add').serialize(),
            success: function(res) {
                console.log('res');
            },
            error: function (request, jqXHR, textStatus, errorThrown) { 
                console.log(request.responseText);
            }       
        });
}); 
10
  • your submit is just a button not a actual submit behavior button. stackoverflow.com/questions/4139975/… Commented Dec 19, 2017 at 9:32
  • Have you debugged it? What errors are you getting? Commented Dec 19, 2017 at 9:34
  • @Rafee did you not see the js code to submit? $("#submit") Commented Dec 19, 2017 at 9:36
  • "but not running" - can you clarify what this means? Do you mean the form no longer submits? Your on("click") code doesn't fire? It all works fine except the fields are not blocking the submit? Commented Dec 19, 2017 at 9:37
  • 1
    I think what he meant is he is not getting HTML5 form validation even after giving required attribute. Commented Dec 19, 2017 at 9:38

1 Answer 1

0

Please change button type to submit

<button type="submit" id="submit" class="btn btn-success"> Download </button>

and add an event handler for submit

$('#form-add').submit(function(event){
    // cancels the form submission
    event.preventDefault();
});
Sign up to request clarification or add additional context in comments.

9 Comments

#submit is not a form, its a submit button element. Your previous answer was correct.
@Rafee did not understand your comment.My previous answer was wrong because once the button is made to submit ,it would submit the form and would navigate from the page.He is using ajax to do the insert.Hence this answer
@Rafee Got the mistake.Corrected it
Thanks All, I've tried changing both its type="submit" and type="button" but validation required is not working
which browser are you using ??
|

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.