Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

submit form using ajax

// add jquery
// <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
$(document).ready(function() {
  $("#form").on('submit', (function(e) {
    e.preventDefault();
    $.ajax({
      url: $(this).attr('action'),
      type: "POST",
      data: new FormData(this),
      contentType: false,
      cache: false,
      processData: false,
      success: function(response) {
        $("#form").trigger("reset"); // to reset form input fields
      },
      error: function(e) {
        console.log(e);
      }
    });
  }));
});
Comment

jquery ajax form submission

$(function() {
  $('form.my_form').submit(function(event) {
    event.preventDefault(); // Prevent the form from submitting via the browser
    var form = $(this);
    $.ajax({
      type: form.attr('method'),
      url: form.attr('action'),
      data: form.serialize()
    }).done(function(data) {
      // Optionally alert the user of success here...
    }).fail(function(data) {
      // Optionally alert the user of an error here...
    });
  });
});
Comment

how to submit form using ajax

// First add jquery
// <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

$(document).ready(function() {
  $("#myForm").on('submit', (function(e) {
    e.preventDefault();
    $.ajax({
      url: $(this).attr('action'),
      type: "POST",
      data: new FormData(this),
      contentType: false,
      cache: false,
      processData: false,
      success: function(response) {
        $("#myForm").trigger("reset"); // this line is to reset form input fields
        alert('submitted');
      },
      error: function(e) {
      	alert('Failed to sumit');
      }
    });
  }));
});
//don't forget to add csrf token
Comment

ajax submit form data

<form id="contactForm1" action="/your_url" method="post">
    <!-- Form input fields here (do not forget your name attributes). -->
</form>

<script type="text/javascript">
    var frm = $('#contactForm1');

    frm.submit(function (e) {

        e.preventDefault();

        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                console.log('Submission was successful.');
                console.log(data);
            },
            error: function (data) {
                console.log('An error occurred.');
                console.log(data);
            },
        });
    });
</script>
Comment

jquery form submit ajax

    $(document).on('click', '#save_province', function () {
        var a = $("#province").val(), b = $("#district").val(), c = getParameterByName('supplier_id');
        $.ajax({
            url: "postavshik_save.php",
            type: "POST",
            data: {
                province: a,
                district: b,
                supplier_id: c,
            },
            beforeSend: function () {
                $("#province").attr("disabled", !0), $("#district").attr("disabled", !0);
            },
            success: function (a) {
                alert("Post so`rov yuborildi javob olindi");
            }
        });
    });
Comment

jquery ajax form submit example

$.ajax({
			    type: "POST",
			    url: "upload.php",
			    data: { name:name, mobile:mobile, address:address, city:city },		    
			    dataType: "json",
			    success: function(result){
			        			    }
			});
Comment

submit form with ajax

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

  <script>
      $(document).ready(function () {
          // отслеживаем событие отправки формы
          $('#id_username').keyup(function () {
              // создаем AJAX-вызов
              $.ajax({
                  data: $(this).serialize(), // получаяем данные формы
                  url: "{% url 'validate_username' %}",
                  // если успешно, то
                  success: function (response) {
                      if (response.is_taken == true) {
                          $('#id_username').removeClass('is-valid').addClass('is-invalid');
                          $('#id_username').after('<div class="invalid-feedback d-block" id="usernameError">This username is not available!</div>')
                      }
                      else {
                          $('#id_username').removeClass('is-invalid').addClass('is-valid');
                          $('#usernameError').remove();

                      }
                  },
                  // если ошибка, то
                  error: function (response) {
                      // предупредим об ошибке
                      console.log(response.responseJSON.errors)
                  }
              });
              return false;
          });
      })
  </script>
Comment

submit form using ajax

$.post("test.php", $("#testform").serialize());
Comment

ajax form submit

Ajax jquery form submit
Comment

PREVIOUS NEXT
Code Example
Javascript :: es module __dirname alternative 
Javascript :: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. 
Javascript :: javacript contextmenu preventDefault 
Javascript :: js remove falsey values from array 
Javascript :: ERROR in ./server/server.js Module build failed (from ./node_modules/babel-loader/lib/index.js): 
Javascript :: disable right click jquery 
Javascript :: how to see in how many servers your discord bot is d.js 
Javascript :: install latest npm for react 
Javascript :: how to remove dash from string in javascript 
Javascript :: thousands by comma javascript 
Javascript :: javascript run document ready 
Javascript :: p5 map function 
Javascript :: jquery on body click 
Javascript :: onclick javascript confirm 
Javascript :: execute javascript on page load jquery 
Javascript :: react.js installation 
Javascript :: how to refresh slick after tab function 
Javascript :: select random element js array 
Javascript :: how to get value of button that click on it jquery 
Javascript :: validate Alphabet Letter javascript 
Javascript :: Setting a background Image With React Inline Styles 
Javascript :: how to add react icons using npm 
Javascript :: how to select html body in javascript 
Javascript :: jquery get data attribute value 
Javascript :: react doesnt load local images but external does 
Javascript :: generate random number jquery 
Javascript :: get window size javascript 
Javascript :: check if the method is not called in jest 
Javascript :: update node to latest version 
Javascript :: slider is not a function jquery 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =