[AJAX] contrôle ajax - submit
Bonjour,
J'ai un formulaire d'édition sur lequel j'effectue une requete asynchrone pour savoir si un couple de deux champs associés (nom et statut) existe dans ma bdd ou non.
ex: si le couple NomFichier - Statut1 existe déja alors l'upload de fichier sera facultative dans le cas contraire elle sera obligatoire.
cela est fait et fonctionne. cependant, le soucis est que je dois passer sur ce un des champs pour que le controle fonctionne.
ce meme champs NomFichier est en lecture seule pour éviter de le changer.
si je ne passe pas dessus pour que le controle s'active il me mettra la valeur false par défaut quand je soumettrai le formulaire de modification.
voici un bout du formulaire :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
<form name="insertion" action="modification3.php" method="post"
onsubmit="if (testUpload()) return (verif_before_valid_form() && check()); else return false;"
enctype="multipart/form-data" accept-charset="UTF-8">
<table>
<tr>
<td><label>Statut du corpus:</label></td>
<td><select name="Statut_corpus" id="StatutCorpus">
<?php
//recuperation du statut actuel
$Statut_corpus = isset($result->Statut_corpus) ? $result->Statut_corpus : '';
// si le statut est Transcrit alors on affiche T VNA OU VA
if($Statut_corpus == "Transcrit"){
echo '<option value="Transcrit" selected >Transcrit</option>
<option value="Verifie_Non_Anonymise">Verifié - Non Anonymisé</option>
<option value="Verifie_Anonymise">Vérifié - Anonymisé</option>
';
}
// si le statut est VNA alors on affiche VNA OU VA
if($Statut_corpus == "Verifie_Non_Anonymise"){
echo '
<option value="Verifie_Non_Anonymise" selected >Verifié - Non Anonymisé</option>
<option value="Verifie_Anonymise">Vérifié - Anonymisé</option>
';
}
// si le statut est VA alors on affiche VA
if($Statut_corpus == "Verifie_Anonymise"){
echo '
<option value="Verifie_Anonymise" selected>Vérifié - Anonymisé</option>
';
}
?>
</select></td>
</tr>
<tr>
<td><label>Nom du dossier contenant le corpus informatisé
(identifiant):</label></td>
<td><input readonly="readonly" type="text" name="NomFichierSource"
id="NomFichierSource"
value="<?php echo($result->NomFichierSource) ;?>"
onblur="req_NomFichierSource();" /> <span id="NomFichierSource_check"></span></td>
</tr>
<fieldset class="action"><input type="submit" name="modifier"
value="Modifier" /></fieldset>
</table>
</form> |
le code ajax :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| // ce code va vérifier la disponibilité du Nom de fichier SOURCE (attention le statut est associé au fichier)
//on pourra avoir deux meme noms de fichiers source mais le statut sera différent
//adaptation au form edition : si le statut est modifier lupload est obligatoire sinon il est facultatif
var bUpload = false;
function req_NomFichierSource(NomFichierSource) {
//XMLHttpRequest est supporté par Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Netscape 7
if (document.all)
var XhrObj = new ActiveXObject("Microsoft.XMLHTTP"); // Internet
// Explorer
else
var XhrObj = new XMLHttpRequest(); // Mozilla
// content = document.getElementById("NomFichierSource_check"); //zone
// d'affichage
var content = document.getElementById("NomFichierSource_check"); // zone
// d'affichage
var srcFile = document.getElementById("NomFichierSource");
var statut = document.getElementById("StatutCorpus");
// fichierSON
var fichierSON = document.getElementById("fichierSON");
var fichierTRS = document.getElementById("fichierTRS");
XhrObj.open("POST", "../verif_modif_js/verif_StatutCorpus.php");
// XhrObj.open("POST", "verif_NomFichierSource.php", false);
// Ok pour la page cible
XhrObj.onreadystatechange = function() {
if (XhrObj.readyState == 4 && XhrObj.status == 200) {
if (XhrObj.responseText == 'OK') {
content.innerHTML = '<img src="../css/images/accepter.png" alt=""/><br><label>Upload obligatoire</label>';
// ok nouveau pseudo
// =================================verification champs
// parcourir non vide=====================================//
$.fn.copyTo = function(to) {
var to = $(to);
for ( var i = 1; i < arguments.length; i++)
to.set(arguments[i], this.get(0)[arguments[i]]);
return this;
};
new function() {
// $.fn.validate = validate() {};
$.fn.validate = {
init : function(o) {
if (o.name == 'fichierSON') {
this.fichierSON(o)
}
;
if (o.name == 'fichierTRS') {
this.fichierTRS(o)
}
;
},
///fichierSON
fichierSON : function(o) {
var fichierSON = "^.{2,}(.wav|.mp3|.wma)$";
if (o.value.match(fichierSON)) {
bUpload =true;
doSuccess(o);
} else {
bUpload = false;
doError(o,
'<label>Joindre un fichier son correct !</label>');
}
;
},
fichierTRS : function(o) {
var fichierTRS = "^.{2,}(.trs|.doc|.txt)$";
if (o.value.match(fichierTRS)) {
bUpload =true;
doSuccess(o);
} else {
bUpload = false;
doError(o,
'<label>Joindre un fichier transcription correct !</label>');
}
;
}
};
function doSuccess(o) {
$('#' + o.id + '_img')
.html(
'<img src="../css/images/accept.gif" border="0" style="float:left;" />');
$('#' + o.id + '_li').removeClass("error");
$('#' + o.id + '_msg').html("");
$('#' + o.id + '_li').addClass("success");
}
function doError(o, m) {
$('#' + o.id + '_img')
.html(
'<img src="../css/images/exclamation.gif" border="0" style="float:left;" />');
$('#' + o.id + '_li').addClass("error");
$('#' + o.id + '_msg').html(m);
$('#' + o.id + '_li').removeClass("success");
}
//private helper, validates each type after check
function doValidate(o) {
$('#' + o.id + '_img')
.html(
'<img src="../css/images/loading.gif" border="0" style="float:left;" />');
$.post('ajax.php', {
id : o.id,
value : o.value
}, function(json) {
eval("var args = " + json);
if (args.success == true) {
doSuccess(args);
} else {
doError(args, args.msg);
}
});
}
;
};
$(document).ready( function() {
$("//[@class=validated]/input").blur( function() {
$(this).validate.init(this);
});
// This Used To Be HOVER, But I.E. Didnt Like It
// $(".form
// li").hover(function(){$(this).addClass("selected");},function(){$(this).removeClass("selected");});
$(".form li").mouseover( function() {
$(this).addClass("selected");
});
$(".form li").mouseout( function() {
$(this).removeClass("selected");
});
});
// ==============================fin de la vérification des
// champs====================================//
} else {
content.innerHTML = '<img src="../css/images/refuser.png" alt=""/><br><label>Upload facultatif</label>';
bUpload = true; // erreur pseudo déjà existant
}
}
}
XhrObj
.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
// XhrObj.send('p='+encodeURIComponent(NomFichierSource));
XhrObj.send('p=' + encodeURIComponent(srcFile.value) + '&s='
+ encodeURIComponent(statut.value));
}
///A METTRE DANS LE ON SUBMIT AVEC LES CONDITIONS VALABLES
function testUpload(f) {
if (!bUpload) {
alert('Joindre le fichier son et/ou transcription!!');
return false;
}
return true;
} |
merci de votre aide.
j'aimerai en cliquant sur submit que le controle s'effectue et ne pas devoir passer par le champs pour qu'il soit controlé merci