【问题标题】:javascript validation for php formphp表单的javascript验证
【发布时间】:2017-05-03 03:19:55
【问题描述】:

如何在提交到 php 之前验证此表单
例如:如果我不输入名称并单击提交,则该项目将保存在 数据库

<!DOCTYPE html>
<html>
<head>
	<title>Add Student</title>
</head>
<body  bgcolor="#192C3D" text="white">
<center>
<h2>Enter Student Details</h2><br><br>
<form action="student_add.php" method="post">
	Register no:  
	<input type="text" name="reg_no"><br><br>
	Name:
	<input type="text" name="name"><br><br>
	Gender:
	<input type="radio" name="gender" value="M">Male
	<input type="radio" name="gender" value="F">Female
	<br><br>Group:
	<select name="group" size="1">
		<option value="Computer Science" selected>Computer Science</option>
		<option value="Biology">Biology</option>
		<option value="Commerce">Commerce</option>
		<option value="Humanities">Humanities</option>
	</select><br><br>
	<input type="submit" value="Save">&nbsp
	<input type="reset" value="Clear">
</form>
</center>
</body>
</html>

这是

student_add.php

<html>
<head><title>Add Student</title></head>
<body  bgcolor="#192C3D" text="white" alink="#75EA7E" vlink="#75EA7E" link="#75EA7E">
<a href="add_student.php"><h3>Add Student</h3></a>
<a href="Show_student.php"><h3>Show Students</h3></a>

<?php

$link = mysqli_connect("localhost", "root", "rootuser", "githin");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
// Escape user inputs for security
$reg_no = mysqli_real_escape_string($link, $_REQUEST['reg_no']);
$name = mysqli_real_escape_string($link, $_REQUEST['name']);
$gender = mysqli_real_escape_string($link, $_REQUEST['gender']);
$batch = mysqli_real_escape_string($link, $_REQUEST['group']);
 
// attempt insert query execution
$sql = "INSERT INTO student (reg_no, name, gender, batch) VALUES ('$reg_no', '$name', '$gender', '$batch')";
if(mysqli_query($link, $sql)){
    echo '<script language="javascript">';
	echo 'alert("Record Successfully Added")';
	echo '</script>';
} else{
     echo '<script language="javascript">';
	echo 'alert("Record Could NOT be added Successfully" . mysqli_error($link))';
	echo '</script>';
}
 
// close connection
mysqli_close($link);
?>

</body>
</html>

请任何人帮助我验证上述表格 name、reg_no 和 gender 字段为必填项 如果我们尝试在不输入必填字段的情况下提交表单,则会出现一个 javascript alert() 要求填写必填字段

【问题讨论】:

  • 警告:当使用mysqli 时,您应该使用parameterized queriesbind_param 将用户数据添加到您的查询中。 请勿使用手动转义和字符串插值或连接来完成此操作,因为您将创建严重的SQL injection bugs。意外未转义的数据是一个严重的风险。使用绑定参数不那么冗长,并且更容易检查以检查您是否正确执行。
  • 最好不要依赖在客户端强制执行的表单验证,无论是通过 html5 还是 javascript。有人可以使用旧版浏览器,或者禁用 JS(如果您有 JS 验证模式),或者可以向您的站点发送 cURL 命令并绕过您的 html 验证,因此您需要利用像 trim() 这样的功能, empty()isset()preg_match()filter_var() 等服务器端。客户端验证更像是一种用户体验功能。

标签: javascript php html mysql


【解决方案1】:

您可以在此处添加html5 验证,您可以在下面找到更新的代码:

<!DOCTYPE html>
<html>
<head>
	<title>Add Student</title>
</head>
<body  bgcolor="#192C3D" text="white">
<center>
<h2>Enter Student Details</h2><br><br>
<form action="student_add.php" method="post">
	Register no:  
	<input type="number" name="reg_no" required><br><br>
	Name:
	<input type="text" name="name" required><br><br>
	Gender:
	<input type="radio" name="gender" value="M" checked>Male
	<input type="radio" name="gender" value="F">Female
	<br><br>Group:
	<select name="group" size="1"  required>
		<option value="Computer Science" selected>Computer Science</option>
		<option value="Biology">Biology</option>
		<option value="Commerce">Commerce</option>
		<option value="Humanities">Humanities</option>
	</select><br><br>
	<input type="submit" value="Save">&nbsp
	<input type="reset" value="Clear">
</form>
</center>
</body>
</html>

【讨论】:

  • 谢谢。你能帮我接受reg_no的数值吗
  • 你能帮我接受reg_no的数值吗
  • 只需添加type="number" 而不是type="text"
  • 你也可以在你的输入标签required onkeypress='return event.charCode &gt;= 48 &amp;&amp; event.charCode &lt;= 57'中使用它来去掉箭头按钮
  • 请您帮忙避免使用 -ve 整数
【解决方案2】:

如果您正在寻找用于验证的多功能插件,您可以使用 http://1000hz.github.io/bootstrap-validator/ 。使用这个插件比使用 HTML5 验证更安全。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-02
    • 2020-06-22
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 2015-07-18
    • 2011-02-17
    • 1970-01-01
    相关资源
    最近更新 更多