Javascript tests do not continue when reaching the ajax code
In the onClick attribute of a button of my form there is a call to a javascript function. In this function there are tests of whether field开发者_高级运维s are not filled in. There is also a call to an Ajax function when a textfield is filled with a value , to test if the value already exists in the Mysql database. My problem is that when arriving at the ajax function then the remaining of the javascript tests are not preformed , although there are still fields that are not filled in. So how to make the remaining tests executed ?
Here is my code :
<input type="button" name="btn_Valider" id="btn_Valider" value="Valider" class="bouton2" onClick="testerDonneesForm();"/>
function testerDonneesForm(){
if (document.frm_Produit.prod_code.value == "") {
alert( "Le champ ''Code produit'' ne doit pas être vide!");
document.frm_Produit.prod_code.focus;
return false;
}
else if (document.frm_Produit.prod_code.value != "") {
ajaxRechercherCodeProduit();
}
else if (document.frm_Produit.type_prod_code.value == "") {
alert( "Le champ ''Type code produit'' ne doit pas être vide!");
document.frm_Produit.prosp_cin.focus;
return false;
}
else {
document.getElementById("frm_Produit").submit() ;
}
}
function trim (myString)
{
return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}
function ajaxRechercherCodeProduit() {
var g_XhrObject = get_Xhr() ;
var prod_code = document.getElementById('prod_code').value;
var url = '<?php echo HTTP_AJAX ?>Produit/ajaxVerifieCodeProduit.php?prod_code='+prod_code;
if (g_XhrObject) {
g_XhrObject.onreadystatechange = function()
{
if (g_XhrObject.readyState == 4 && g_XhrObject.status == 200) {
if (trim(g_XhrObject.responseText) != "0")
{
alert(g_XhrObject.responseText);
return false;
}
}
}
g_XhrObject.open("POST", url, true) ;
g_XhrObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') ;
g_XhrObject.send(null);
}
}
You want the tests to be individual if
statements, not else if
parts of a single if
statement. With an if-else if-else
statement, the execution moves past the end of the if structure as soon as one of the conditions is found to be true, or it has executed the else condition (none of the preceding conditions were true).
精彩评论