[英]Multiple input fields Validation with AJAX / PHP on Mysql
i've created a form with several fields, input, select, box etc. i've 2 fields that must be validated agaist DB. 我创建了一个包含多个字段,输入,选择,框等的表单。我有2个必须通过验证DB验证的字段。 If exists show some details if not display some aditional input fields to add to DB.
如果存在,请显示一些详细信息,如果不显示,请显示一些附加输入字段以添加到DB。
Ex: Make, model, licenseplate. 例如:品牌,型号,车牌。 IF licenseplate exists -> Honda Accord IF NOT -> input to add .
如果车牌存在->本田雅阁如果不->输入要添加。
$(document).ready(function(){
$("licplate").change(function(){
var lp = $("licplate").val();
if(lp.length <= 8) { //my country have a 8 digit plate
$("#stat").html('<image> Verifing');
$.ajax({
type: "POST",
url: "check.php",
data: "licplate="+lp,
success: function(msg) {
$("#stat"),ajaxComplete(function(event,request,settings){
if(msg=='OK'){
$(this).html('message to ignore');
} else {
$(this).html(msg);
}
In my check.php i connect to db, validate and return data. 在我的check.php中,我连接到数据库,验证并返回数据。 If exist return
如果存在返回
echo "<span class="msg">$row_sql_chk['make']." - ".$row_sql_chk['model']</span>";
if not 如果不
<li><label>Make</label><input id="make" name="make" type="text" />
<li><label>Modele</label><input id="model" name="model" type="text" />
It work just fine, however i've got a new validation to make in another input field on form. 它工作正常,但是我需要在表单的另一个输入字段中进行新的验证。 I must validate the driver the same way.. is exits the "driverlicense" show name and address if not include fields to insert it.
我必须以相同的方式验证驱动程序。如果没有包含要插入的字段,则退出“ driverlicense”显示名称和地址。 I did the same code, change fieldnames, the div name etc.. And work just fine.
我做了同样的代码,更改了字段名,div名称等。工作正常。
But then came the problem... if both of the fields dont exist. 但是随后出现了问题……如果两个字段都不存在。 I fill the fields (make, model) and when validating the driver, if not exist it clears the top fields.
我填写了字段(制造商,模型),并且在验证驱动程序时(如果不存在)会清除顶部的字段。 After that if i fill (again) all the fields (make, model, name..) works just as expected.
之后,如果我再次填写(制造商,型号,名称..)的所有字段,则按预期方式工作。 So how can i do the 2nd field validation without cleaning the other?
那么,如何在不清洁其他部件的情况下进行第二现场验证? tks
ks
File checkcom.php (the file check.com is almost equal, the diference is in the table to test.) 文件checkcom.php(文件check.com几乎相等,区别在于要测试的表中。)
<?php
if(isset($_POST['comand']))
{
$comand = $_POST['comand'];
$dbHost = 'localhost';
$dbUsername = 'name';
$dbPassword = 'XXXXXXX';
$dbDatabase = 'MYDB';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$sql_check = mysql_query("select nome, licenca, from pilotos where licenca='".$comand."'") or die(mysql_error());
// in check.com is "select matricula, marca ,modelo from veiculos where matricula='".$mat."'") or die(mysql_error());
$row_sql_check = mysql_fetch_assoc($sql_check);
$totalRows_sql_check = mysql_num_rows($row_sql_check);
if(mysql_num_rows($sql_check))
{
echo '<font color="#003366"> <STRONG>'.$row_sql_check['nome'].'</STRONG> </font>';
}
else
{
echo '<div class="rectang">
<li><label>Nome:</label> <input id="nome_pil" name="nome_pil" type="text" title="Nome Piloto" required/></li>
<li><label>Morada:</label> <input id="morada_pil" name="morada_pil" type="text" title="Morada Piloto"/><br /></li>
<li><label>Localidade:</label> <input id="local_pil" name="local_pil" type="text" title="Localidade Piloto"/><br /></li>
<font color="#003366"> A licença <STRONG>'.$comand.'</STRONG> NÃO está registada na tabela. Vai ser adicionado o registo provisório.</font>
</div>';
}
}
?>
---- SCript ---- ----斯克里普特----
<SCRIPT type="text/javascript">
pic1 = new Image(16, 16);
pic1.src = "images/loader.gif";
$(document).ready(function(){
$("#licplate").change(function(){
var lp = $("#licplate").val();
if(lp.length >= 4) {
$("#status").html('<img src="images/loader.gif" align="absmiddle"> A verificar...');
$.ajax({
type: "POST",
url: "check.php",
data: "licplate="+ lp,
success: function(msg){
$("#status").ajaxComplete(function(event, request, settings){
if(msg == 'OK'){
$("#licplate").removeClass('object_error');
$("#licplate").addClass("object_ok");
$(this).html('Não existe <img src="images/alerta.gif" align="absmiddle">');
}
else {
$("#licplate").removeClass('object_ok');
$("#licplate").addClass("object_error");
$(this).html(msg);
}
});
}
});
}
else{
$("#status").html('<font color="red">A matrícula tem de ter pelo menos <strong>4</strong> caracteres.</font>');
$("#licplate").removeClass('object_ok');
$("#licplate").addClass("object_error");
}
});
<!-- -- aqui muda para a licença de condutor
$("#comand").change(function(){
var cmd = $("#comand").val();
if(cmd.length >= 6){
$("#status1").html('<img src="images/loader.gif" align="absmiddle"> A verificar...');
$.ajax({
type: "POST",
url: "checkcom.php",
data: "comand="+ cmd,
success: function(msg1){
$("#status1").ajaxComplete(function(event, request, settings){
if(msg1 == 'OK'){
$("#comand").removeClass('object_error1');
$("#comand").addClass("object_ok1");
$(this).html('Não existe <img src="images/alerta.gif" align="absmiddle">');
}
else
{
$("#comand").removeClass('object_ok1');
$("#comand").addClass("object_error1");
$(this).html(msg1);
}
});
}
});
}
else
{
$("#status1").html('<font color="red">A Licença de piloto tem de ter pelo menos <strong>6</strong> caracteres.</font>');
$("#comand").removeClass('object_ok1');
$("#comand").addClass("object_error1");
}
});
});
//-->
function tratamat(mat)
{
var tipo = mat.substr(3, 1);
switch(tipo) {
case "U":
document.getElementById("tipo_veic").value="DP";
break;
case "u":
document.getElementById("tipo_veic").value="DP";
break;
case "H":
document.getElementById("tipo_veic").value="GT";
break;
case "h":
document.getElementById("tipo_veic").value="GT";
break;
case "A":
document.getElementById("tipo_veic").value="SR";
break;
case "a":
document.getElementById("tipo_veic").value="SR";
break;
}
}
</SCRIPT>
And the HTML form 和HTML表单
<form action="<?php echo $editFormAction; ?>" method="POST" name="entrada" class="formgeral" id="formgeral">
<ul>
<li><label>Matricula</label><input id="licplate" size="20" type="text" name="licplate" title="Matricula" style="text-transform:uppercase;" onBlur="javascript:tratamat(this.value)"><br>
<div id="status"> </div></li>
<li>
<label>Tipo Veiculo</label><input type="text" name="tipo_veic" id="tipo_veic" title="Tipo de Aeronave" style="text-transform:uppercase;">
</li>
(...)
<li><label>Licença</label><input id="comand" name="comand" type="text" size="15" maxlength="15"><div id="status1"> </div></li>
(...) (...)
Is this enough? 这够了吗?
solved 解决了
Add a validation to field to licplate 'undefined' 将验证添加到字段“未定义”
if(msg != 'ERR'){
$("#licplate").removeClass('object_error');
$("#licplate").addClass("object_ok");
var lic = $("#matricula"),val(); // assing the value of 'new' input
if(typeof lic === 'undefined') { // verify the value of 'new' input
$(this).html(msg);
}
}
and it works!! 而且有效!!
you guys are awesome... did not answer so I can use my best tool .. my brain !! 你们真棒...没有回答,所以我可以使用我最好的工具..我的大脑! :D
:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.