实时验证在表中添加/更新数据php、ajax、javascript
你能帮我解决这个问题吗 我有一个表格,我添加了两个按钮来添加或更新信息 我想在“描述”字段中进行验证**您只能写字母且少于255个字符,在实时验证在表中添加/更新数据php、ajax、javascript,javascript,php,mysql,ajax,mysqli,Javascript,Php,Mysql,Ajax,Mysqli,你能帮我解决这个问题吗 我有一个表格,我添加了两个按钮来添加或更新信息 我想在“描述”字段中进行验证**您只能写字母且少于255个字符,在“名称”字段中您只能写少于50个字符。无论如何,我不能分别点击“保存”或“添加”按钮(在一切正常之前,按钮是锁定的) 此外,页面必须显示“实时(在单击按钮之前)每个字段中的错误” 这是我在表中添加新信息时的代码: <?php include('connect.php'); $error=""; if(isset($_POST['btn
“名称”
字段中您只能写少于50个字符。无论如何,我不能分别点击“保存”或“添加”按钮(在一切正常之前,按钮是锁定的)
此外,页面必须显示“实时(在单击按钮之前)每个字段中的错误”
这是我在表中添加新信息时的代码:
<?php include('connect.php');
$error="";
if(isset($_POST['btnsave']))
{
$career_id=$_POST['carrera'];
$name=$_POST['txtname'];
$description=$_POST['txtdescription'];
$hourss=$_POST['txthours'];
if($_POST['txtid']=="0")
{
$a_sql=mysql_query("INSERT INTO subjects VALUES('','$career_id','$name','$description','$hourss')");
if($a_sql)
{
header("location:index.php");//
}
}else{
echo "Update";
}
}
$sqlm = mysql_query("SELECT * FROM careers");
$options = "";
while($result = mysql_fetch_array($sqlm)){
$options .= "<option value='".$result['id']."'>".$result['name']."</option>";
}
?>
<h2 align="center">ADD NEW SUBJECT</h2>
<form method="Post">
<table align="center">
<tr>
<td>Career:</td>
<td><select name='carrera'><?php echo $options; ?></select><input type="hidden" name="txtid" value="0" /></td>
</tr>
<tr>
<td>Name:</td>
<td><input type='text' name='txtname'/></td>
</tr>
<tr>
<td>Description:</td>
<td><input type='text' name='txtdesription'/></td>
</tr>
<tr>
<td>Hours:</td>
<td>
<select name='txthours'/>
<option <?php if($hourss=='1') echo 'selected' ; ?> value="2">2 (two)</option>
<option <?php if($hourss=='1') echo 'selected' ; ?> value="4">4 (our)</option>
<option <?php if($hourss=='1') echo 'selected' ; ?> value="6">6 (six)</option>
<option <?php if($hourss=='1') echo 'selected' ; ?> value="8">8 (eight)</option>
<option <?php if($hourss=='1') echo 'selected' ; ?> value="10">10 (ten)</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type='submit' value=save name='btnsave'/></td>
</tr>
</table>
</form>
<?php include('connect.php');
$error="";
if(isset($_GET['edit']))
{
$ident=$_GET['iden'];
$row=mysql_query("SELECT * FROM subjects WHERE id=$ident");
$st_row=mysql_fetch_array($row);
}
?>
<h2 align="center">UPDATE SUBJECT</h2>
<form method="Post" action=''>
<table align="center">
<tr>
<td>Career:</td>
<td><input type='text' name='txtcarreras_id' value="<?PHP echo $st_row['career'] ?>"/></td>
</tr>
<tr>
<td>Name:</td>
<td><input type='text' name='txtnombre' value="<?PHP echo $st_row['name'] ?>"/></td>
</tr>
<tr>
<td>Description:</td>
<td><input type='text' name='txtdescripcion' value="<?PHP echo $st_row['description'] ?>"/></td>
</tr>
<tr>
<td>Hours:</td>
<td><input type='text' name='txtcarga_horaria' value="<?PHP echo $st_row['hours'] ?>"/></td>
</tr>
<tr>
<td></td>
<td><input type='submit' value="save" name='btnsave'/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['btnsave']))
{
$career_id=$_POST['txtcarreras_id'];
$name=$_POST['txtnombre'];
$description=$_POST['txtdescripcion'];
$hours=$_POST['txtcarga_horaria'];
$a_sql=mysql_query("UPDATE materias SET career='$career_id', name='$name', description='$description', hours='$hours' WHERE id='$ident'");
if($a_sql)
{
header("location:index.php");
}
}
?>
添加新主题
职业:
姓名:
说明:
小时:
value=“4”>4(我们的)
value=“8”>8(八)
更新主题
职业:
您可以使用Javascript实现这一点。使用Jquery或其他库更容易
使用jQuery:
$('input[name=txtname]').keyup(function() {
if ($(this).val().length < 50) {
// Do Error stuff here
} else {
// Remove error stuff here
}
});
$('input[name=txtname]).keyup(function(){
if($(this).val().length<50){
//在这里做些错误的事情
}否则{
//在这里删除错误内容
}
});
如果您有jQuery并使用上面的keyup事件处理程序,那么它应该在您键入时显示