Submiting a form after clicking the submit button to perform an AJAX query, unexpected results

StackOverflow https://stackoverflow.com/questions/23404662

  •  13-07-2023
  •  | 
  •  

Question

I have a form, I have a submit button. On click of the submit button, I suspend the form from submitting and peform an AJAX request. This sometimes works and sometimes doesnt. Can you see any flaws in the code below?

$('#ambition_submit').click(function(e){
    e.preventDefault();
    var output = getTimelineEntryData(data.toJSON());//console.log(output);
    $.getJSON('http://www.test.com/x/api/add_subgoals.php',{action:'add.subgoals',ambition_id:ambition_id,subgoals:output}, function (response) {
        //var this_url = this.url; console.log(this_url);
        if (response.status == 200) {
            var result = response.message;//alert(response.message);
            $('form[name=testing747]').submit();//console.log('Test ' + $('form[name=testing747]').length);
        }
        else{
            alert(response.message);
        }
    });
});

Thanks

Was it helpful?

Solution

I previously had some issues with click in the past. I changed it to live and fixed those issues. Maybe it won't fix yours, but you could give it a try:

$('#ambition_submit').live('click',function(){
});

For the most recent jquery version, it has been replaced by .on though.

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top