brianlange Posted January 11, 2011 Share Posted January 11, 2011 I would like to run preventDefault() if the ajax call fails. How do I set the sf variable inside of my ajax complete function? $(document).ready(function() { $('#login').submit(function(e){ var sf = true; $.ajax({type: 'POST', url: '/index/check-email', async: 'false', data: {email: 'brianrlange@gmail.com' }, complete: function(XMLHttpRequest, textStatus) { if (XMLHttpRequest.responseText == 'fail') { $('#norisk').css("display", "none"); $('p.error').css("display", "block"); sf = false; } else { alert(XMLHttpRequest.responseText); } }}); if (!sf) { e.preventDefault(); } }); }); Quote Link to comment https://forums.phpfreaks.com/topic/224120-prevent-form-from-submitting-if-ajax-call-returns-false/ Share on other sites More sharing options...
Omirion Posted January 11, 2011 Share Posted January 11, 2011 sf seems to be in the scope of the ajax call. So you should have no trouble. Or i am not understanding what outcome you want to achieve. Quote Link to comment https://forums.phpfreaks.com/topic/224120-prevent-form-from-submitting-if-ajax-call-returns-false/#findComment-1158130 Share on other sites More sharing options...
brianlange Posted January 12, 2011 Author Share Posted January 12, 2011 Thanks for the response. I want to call e.preventDefault() based upon the results of the ajax call. e.preventDefault() only appears to work outside of the ajax call and I can't get the result of the ajax call outside of the scope of the ajax function. Quote Link to comment https://forums.phpfreaks.com/topic/224120-prevent-form-from-submitting-if-ajax-call-returns-false/#findComment-1158192 Share on other sites More sharing options...
Omirion Posted January 12, 2011 Share Posted January 12, 2011 so when you do this sf = false; Nothing happenes? Also you might try the $.data() method. Just attach $(whatever).data('failed') And check if($(whatever).data('failed')){ do your shtuff} This is more of a workaround than a direct solution to you method. Quote Link to comment https://forums.phpfreaks.com/topic/224120-prevent-form-from-submitting-if-ajax-call-returns-false/#findComment-1158203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.