It seems that I miss out some basic concept of jquery, since this script won't do what I want it to do:
$(document).ready(function(){
$('#form').submit(function(event) {
console.log( "submitted form, checking #domain_url_input ");
// event.preventDefault();
var submit_error=false;
var is_primary_url = false;
var url = $('#domain_url_input').val();
var get_url = '/i.php?a=check&url2check=' + escape(url);
var valid_data = $.ajax({
event: event,
url: get_url,
success: function(response) {
// alert('ajax successful');
is_primary_url = $(response).find('#check_div').text();
// alert(is_primary_url);
var dns_zone = $('#dns_zone_sel').val();
if (is_primary_url == 'YES' && dns_zone=='')
{
event.preventDefault();
alert('Choosing a DNS ZONE is required!');
$('#dns_zone_sel').focus();
this.submit_error=true;
// if (submit_error==true) event.preventDefault();
// return 'error';
} //
} // end func success
}); // ajax
}); // submit
}); // rdy
(I reduced the code a bit of course.)
The code works perfectly, except for the:
event.preventDefault();
won't take effect.
IF i put it into the second line right after
$('#form').submit(function(event) {...
it will work though.
The ajax-call works, I get a positive result. It is as if it would not be possible to PREVENT the SUBMIT any more?
The Alert after:
event.preventDefault(); alert('Choosing a DNS ZONE is required!');
works perfectly, meaning, the script works, except for that the preventDefault() does not have an effect. That may be due to the event "event" to which I don't have proper access?
The intent is:
I want to stop the submit event. I older js scripts I did that via "return false;" but that won't work either. Btw: I know the difference between preventDefault() and "return false",... I am just adding the information that I tried. In this case the "return false" would return from the $(...).ajax() to the $(...).submit() event.
Point is: the form submit() is already in progress when the $().ajax part is still doing the request. So somehow I need to make the submit() wait until the ajax is finished?
Thanx for any ideas!
AFTER ALL THIS following SOLUTION WORKS excactly how I wished - Thanks to my friend Andreas, @Cory and @Michael:
`<script type="text/javascript">
$(document).ready(function(){
var disableSubmit = false;
var transmitNow = false;
$('#form').submit(function(event) {
console.log( "submitted form, checking #domain_url_input ");
console.log( "disableSubmit=" + disableSubmit);
if( transmitNow === true ) return;
event.preventDefault();
if (disableSubmit ===true) return;
$('#form').prop( 'disabled', true );
var submit_error=false;
var is_primary_url = false;
var url = $('#domain_url_input').val();
if (url=='') { alert('Error! A domain/url must be provided!'); return false; }
var get_url = '/i.php?a=check&ajax=1&url2check='
+ escape(url) + '&t='+ Date.now();
disableSubmit = true; // Formular ausgrauen .... als todo
var valid_data = $.ajax({
url: get_url,
error: function(){ disableSubmit = false; $('#form').prop( 'disabled', false ); },
complete: function(){ disableSubmit = false; $('#form').prop( 'disabled', false ); },
success: function(response) {
disableSubmit = false;
$('#form').prop( 'disabled', false );
is_primary_url = $(response).find('#is_url_primary_check_div').text();
var registrar = $('hd[registrar]').val();
var webserver = $('hd[webserver]').val();
var dns_zone = $('#dns_zone_sel').val();
if (is_primary_url == 'YES' && dns_zone=='')
{
// event.preventDefault();
alert('You entered a primary domain. Choosing a DNS ZONE is required!');
$('#dns_zone_sel').focus();
submit_error=true;
}
if (submit_error != true )
{
transmitNow = true;
$('#form').submit();
}
} // end event func success
}); // ajax
}); // submit
}); // rdy
</script>`
THANX TO ALL!! :) happy!
event_submitdefined? It's first usage is you callingpreventDefault()on it. If it'snullorundefinedthen it certainly won't work. Also, AJAX is asynchronous. Your submit event is completing long before the success callback of your AJAX call, which is why it doesn't work there.