Php 我想知道当验证失败时如何停止表单提交?
我是php新手,下面是我的代码 当任何字段为空时,我希望停止表单提交Php 我想知道当验证失败时如何停止表单提交?,php,forms,validation,Php,Forms,Validation,我是php新手,下面是我的代码 当任何字段为空时,我希望停止表单提交 <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php // define variables and set to empty values $nameErr = $emailErr = $gender
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$nameErr = "Name is required";}
else if (empty($_POST["email"]))
{$emailErr = "Email is required";}
else if (empty($_POST["website"]))
{$website = "";}
else if (empty($_POST["comment"]))
{$comment = "";}
else if (empty($_POST["gender"]))
{$genderErr = "Gender is required";}
}
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="welcome.php">
<label>Name:</label> <input type="text" name="name"> <span class="error">* <?php echo $nameErr;?></span>
<br><br>
<label>E-mail:</label> <input type="text" name="email"> <span class="error">* <?php echo $emailErr;?></span>
<br><br>
<label>Website:</label> <input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span>
<br><br>
<label>Comment:</label> <input type="text" name="comment">
<br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
.错误{color:#FF0000;}
PHP表单验证示例
*必填字段
姓名:*
电子邮件:*
网站:
评论:
性别:
女性
男性
*
听起来您想用PHP进行验证,但停止向PHP提交数据。这是不可能的。如果您希望在PHP中进行验证,则无论发生什么情况,都将提交所有数据。如果需要,可以使用exit()
停止PHP的执行。否则,您将需要使用JavaScript(您可以在这里或通过Google找到大量关于表单的信息)在客户端验证表单。如果您想打印所有错误,那么您的代码应该如下所示
<?php session_start(); error_reporting(0);?>
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$_SESSION['name']= "Name is required";}
if (empty($_POST["email"]))
{$_SESSION['email'] = "Email is required";}
if (empty($_POST["website"]))
{$_SESSION['website'] = "Website is required";}
if (empty($_POST["comment"]))
{$_SESSION['comment'] = "comment is required";}
if (empty($_POST["gender"]))
{$_SESSION['gender'] = "Gender is required";}
}
if($_POST['name']!="" && $_POST['email']!="" && $_POST['website']!="" &&
$_POST['gender']!="")
{
header("Location: welcome.php");
}
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="">
<label>Name:</label> <input type="text" name="name"> <span class="error">* <?php echo $_SESSION['name'];?></span>
<br><br>
<label>E-mail:</label> <input type="text" name="email"> <span class="error">* <?php echo $_SESSION['email'];?></span>
<br><br>
<label>Website:</label> <input type="text" name="website"> <span class="error"><?php echo $_SESSION['website'];?></span>
<br><br>
<label>Comment:</label> <input type="text" name="comment">
<br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $_SESSION['gender'];?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
unset($_SESSION['name']);
unset($_SESSION['email']);
unset($_SESSION['website']);
unset($_SESSION['comment']);
unset($_SESSION['gender']);
?>
.错误{color:#FF0000;}
PHP表单验证示例
*必填字段
姓名:*
电子邮件:*
网站:
评论:
性别:
女性
男性
*
如果要访问欢迎页面中的所有变量。代码如下所示
home.php
<?php session_start(); error_reporting(0);?>
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="welcome.php">
<label>Name:</label> <input type="text" name="name"> <span class="error">* <?php echo $_SESSION['name'];?></span>
<br><br>
<label>E-mail:</label> <input type="text" name="email"> <span class="error">* <?php echo $_SESSION['email'];?></span>
<br><br>
<label>Website:</label> <input type="text" name="website"> <span class="error"><?php echo $_SESSION['website'];?></span>
<br><br>
<label>Comment:</label> <input type="text" name="comment">
<br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $_SESSION['gender'];?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
unset($_SESSION['name']);
unset($_SESSION['email']);
unset($_SESSION['website']);
unset($_SESSION['comment']);
unset($_SESSION['gender']);
?>
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$_SESSION['name']= "Name is required";}
if (empty($_POST["email"]))
{$_SESSION['email'] = "Email is required";}
if (empty($_POST["website"]))
{$_SESSION['website'] = "Website is required";}
if (empty($_POST["comment"]))
{$_SESSION['comment'] = "comment is required";}
if (empty($_POST["gender"]))
{$_SESSION['gender'] = "Gender is required";}
}
if(empty($_POST["name"]) || empty($_POST["email"]) || empty($_POST["website"]) || empty($_POST["gender"]))
{
header("Location: home.php");
}
echo $_POST['name'];
?>
.错误{color:#FF0000;}
PHP表单验证示例
*必填字段
姓名:*
电子邮件:*
网站:
评论:
性别:
女性
男性
*
welcome.php
<?php session_start(); error_reporting(0);?>
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="welcome.php">
<label>Name:</label> <input type="text" name="name"> <span class="error">* <?php echo $_SESSION['name'];?></span>
<br><br>
<label>E-mail:</label> <input type="text" name="email"> <span class="error">* <?php echo $_SESSION['email'];?></span>
<br><br>
<label>Website:</label> <input type="text" name="website"> <span class="error"><?php echo $_SESSION['website'];?></span>
<br><br>
<label>Comment:</label> <input type="text" name="comment">
<br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $_SESSION['gender'];?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
unset($_SESSION['name']);
unset($_SESSION['email']);
unset($_SESSION['website']);
unset($_SESSION['comment']);
unset($_SESSION['gender']);
?>
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{$_SESSION['name']= "Name is required";}
if (empty($_POST["email"]))
{$_SESSION['email'] = "Email is required";}
if (empty($_POST["website"]))
{$_SESSION['website'] = "Website is required";}
if (empty($_POST["comment"]))
{$_SESSION['comment'] = "comment is required";}
if (empty($_POST["gender"]))
{$_SESSION['gender'] = "Gender is required";}
}
if(empty($_POST["name"]) || empty($_POST["email"]) || empty($_POST["website"]) || empty($_POST["gender"]))
{
header("Location: home.php");
}
echo $_POST['name'];
?>
如果您想在提交之前验证数据,您应该更具体地使用javascript jquery来验证数据客户端本身 给表单一个这样的id method=“post”action=“welcome.php”id=“form1” 和所有表单元素的ID
$('#form1').submit(function() {
your validation rules here
if($('#email').val().length == 0)
return false;
else
return true;
});
return false停止提交
如果你要做前端,而不仅仅是php,你真的应该尝试一下jquery,它会让你的生活更轻松我你需要的表单不会被提交如果任何字段都是空的,为什么不试试这个
<label>Name:</label> <input type="text" name="name" required> <span class="error">* <?php echo $nameErr;?></span>
<br><br>
<label>E-mail:</label> <input type="text" name="email" required> <span class="error">* <?php echo $emailErr;?></span>
<br><br>
<label>Website:</label> <input type="text" name="website" required> <span class="error"><?php echo $websiteErr;?></span>
<br><br>
<label>Comment:</label> <input type="text" name="comment" required>
<br><br>
<label>Gender:</label>
<input type="radio" name="gender" value="female" required>Female
<input type="radio" name="gender" value="male" required>Male
Name:*
电子邮件:*
网站:
评论:
性别:
女性
男性
Javascript是客户端,PHP是服务器端,因此在未按下提交按钮“将数据发布到服务器”之前,您可以使用PHP验证表单。。检查字段执行不同的操作,如数据库插入、计算等,您不能将响应发送回客户端并告诉他您在这里遇到了此错误,我不打算处理此类数据。您可以使用ajax在服务器端实时验证表单。最好的方法是验证客户端,然后在使用来自总是撒谎的客户端的所有数据(因为每个人都撒谎)之前,在服务器上进行另一次检查。下面是一个这是另一种使用PHP实现PDO数据库的方法:
- 在您完成所有必需的字段并显示必需的输入错误消息之前,它不会提交到您的数据库
- 如果忘记填写一个必填字段并提交,则不会清除所有字段
<?php
// define variables and set to empty values
$nameErr = $emailErr = $cityErr = $commentErr = $genderErr = "";
$name = $email = $city = $comment = $gender = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Please add a name";
} else {
$name = validateInput($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]+/",$name)) {$nameErr = "Only letters and white
space allowed";}
}
if (empty($_POST["email"])) {
$emailErr = "Please add an email";
} else {
$email = validateInput($_POST["email"]);
// check if email is an email format
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$emailErr = "Invalid email format";
}
}
if (empty($_POST["city"])) {
$cityErr = "Please add your city";
} else {
$city = validateInput($_POST["city"]);
// check if city only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
$cityErr = "Only letters and white space allowed";
}
}
if (empty($_POST["comment"])) {
$commentErr = "Please add your comment";
} else {
$comment = validateInput($_POST["comment"]);
// check if comment only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$comment)) {
$commentErr = 'Only "/", "-", "+", and numbers';
}
}
if (empty($_POST["gender"])) {
$genderErr = "Please pick your gender";
} else {
$gender = validateInput($_POST["gender"]);
}
}
// Validate Form Data
function validateInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if(!empty($_POST["name"]) && !empty($_POST["email"]) && !empty($_POST["city"]) && !empty($_POST["comment"]) && !empty($_POST["gender"]))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO info (name, email, city, comment, gender)
VALUES ('$name', '$email', '$city', '$comment', '$gender')";
// use exec() because no results are returned
$conn->exec($sql);
echo "Success! Form Submitted!";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
?>
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<h2>PHP Form</h2>
<p>Doesn't submit until the required fields you want are filled</p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="error">
<p><span>* required field</span></p>
<div><?php echo $nameErr;?></div>
<div><?php echo $emailErr;?></div>
<div><?php echo $cityErr;?></div>
<div><?php echo $commentErr;?></div>
<div><?php echo $genderErr;?></div>
</div>
<label for="name">Name:
<input type="text" name="name" id="name" placeholder="" value="<?php echo $name;?>">
<span class="error">*</span>
</label>
<label for="email">Email:
<input type="email" name="email" id="email" placeholder="" value="<?php echo $email;?>">
<span class="error">*</span>
</label>
<label for="city">city:
<input type="text" name="city" id="city" placeholder="" value="<?php echo $city;?>">
<span class="error">*</span>
</label>
<label for="comment">comment:
<input type="text" name="comment" id="comment" value="<?php echo $comment;?>">
<span class="error">*</span>
</label>
<label for="gender">Gender:<br>
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="other") echo "checked";?> value="other">Other
<span class="error">*</span>
</label>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
您可以对错误执行数组
$errors = []; // empty array
if(isset($_POST['username']) && empty($_POST['username'])) {
$errors['userName'] = "The userName is empty";
}
// then check if no errors , insert it to your DB :
if(count($errors) <= 0) {
// after filtering the username from XSS , insert it to DB.
}else {
// If there are ERRORS , even if one error :
foreach($errors as $error) {
// you can print all your errors
}
}
// or use then in onther place like this :
if(isset($errors['username'])) {
echo $erros['username'];
}
$errors=[];//空数组
if(设置($\u POST['username'])和空($\u POST['username'])){
$errors['userName']=“用户名为空”;
}
//然后检查是否没有错误,将其插入数据库:
如果(count($errors)您可以使用此函数停止表单:
$("form").submit(function(a){
a.preventDefault();
});
我想在这个页面中显示错误,并阻止将其导航到“welcome.php”???是的,错误将显示在页面上,请尝试这种方式。最后不要忘记取消设置所有会话变量。最后的意思是确切的位置?因为我对phpi JU粘贴整个代码非常陌生…但是如果我在所有d字段都为空的情况下单击提交按钮,页面将移动到欢迎。php..我觉得@shashank太懒了ng的人只会为他们做他们的工作……他们询问的答案很清楚。这篇文章对我的目的很有帮助,谢谢Sumit:)…仅供参考…这可能会被接受为答案。我不想接受html5的帮助,我知道使用js或jquery实现这一点的其他各种方法,我想知道它在php中是否可能。我不想接受js或jquery的帮助,我想知道它在php中是否可能。然后在最后添加一个else并添加您的提交代码那里