Non Brazilian Non Brazilian - 7 months ago 25
PHP Question

Jquery send post form ajax not work

am try to send form ajax like this code

<script type='text/javascript'>
/* attach a submit handler to the form */
$("#non").submit(function(event) {

/* stop form from submitting normally */
event.preventDefault();

/* get some values from elements on the page: */
var $form = $( this ),
url = $form.attr( 'action' );

/* Send the data using post */
var posting = $.post( url, { title: $('#mak').val(),meaning: $('#za').val()} );

/* Alerts the results */
posting.done(function( data ) {
alert('success');
});
});
</script>


it not alert success,and i check action after click button is ok

Answer

If jquery.min.js added. Then please ignore it. Else add it. To get action of <form> Use var action_url = form.attr('action');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$('#non').on('submit', function(e) {
    e.preventDefault();
    var form = $(this);
    var formData = form.serialize();
    var action_url = form.attr('action');
    $.ajax({
      url: action_url,
      type: "POST",
      data: formData,
      success: function (data) {
                alert(data);
      },
      error: function () {
        alert("Oops..!! Problem Ocurred. Please Try Again..!!");
      }
  });
});  
</script>