0

I have a form with a disabled submit via return false on form submission. The problem is that I want to store data with ajax. When I applied ajax it's submitting the form which I don't want.

This is my jquery function.

function RegisterCourse(form)
{

    $('.barlittle').show();
    var all_data = $('#'+form).serialize();

    $.ajax({
        url:AjaxRecord.php,
        type:get,
        data:'all_data='+all_data,
        success:function(response){
            $('.barlittle').hide();
            var mesg="Successed";
            $('#temp').val(mesg);
            return false;
            },
        error:function(){
            $('.barlittle').hide();
            var error="ERROR";
            $('#temp').val(error);
            return false;
        }
    });
    return false;
}
1
  • 2
    url:AjaxRecord.php, is probably causing a syntax error. Check your JS console.
    – Blender
    Commented Jan 19, 2013 at 9:29

3 Answers 3

1

Give your submit button an ID

<input type ='submit' id ='sbtForm' value = 'submit' />
//jquery
$("#sbtForm").click(function(e){
  e.preventDefault();
  $.ajax({.... //place your ajax code or call your ajax function
});

hope this will work for you.

1
  • Very poor idea. the submit event is much safer and makes more sense. If you do not want a submit button to submit, make it a button
    – mplungjan
    Commented Jan 19, 2013 at 10:04
1

The value of the url parameter should be a string

$("#your_button_id").click(function(e){
    e.preventDefault();
    $.ajax({
        url: 'AjaxRecord.php',
        type: get,
        data: 'all_data=' + all_data,
        success: function (response) {
            $('.barlittle').hide();
            var mesg = "Successed";
            $('#temp').val(mesg);
        },
        error: function () {
            $('.barlittle').hide();
            var error = "ERROR";
            $('#temp').val(error);
        }
    });
});
0
0

Try checking the value of the form you passed to the function.

if you are sending a different form that mean$('#'+form).serialize() is not the form you want

Not the answer you're looking for? Browse other questions tagged or ask your own question.