您好我正在尝试在php中验证表单。表单将验证名称,电子邮件,性别和密码。出于某种原因,我仍然可以提交数据,并且不会显示任何消息,这意味着我在php中创建的验证代码不起作用。我还在表单上添加了php代码,因此它可以显示错误但没有。我已经在网上看过教程和内容但仍然无法找到错误。可以帮我弄清楚为什么会发生这种情况?谢谢!
<!DOCTYPE html>
<html>
<head>
<title>Form Validation with PHP - Demo Preview</title>
<style>
.error{
color:red
}
</style>
</head>
<body>
<?php
// Initialize variables to null.
$nameError ="";
$emailError ="";
$genderError ="";
$passwordError ="";
//kur shtypim butonin submit
if(isset($_POST['submit'])){
//ne rast se emri eshte bosh
if (empty($name)) {
$nameError = "Vendosni emrin";
}
// emri permban vetem shkronja dhe hapesira
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameError = "Lejohen vetem shkronja dhe hapesire te bardha";
}
//ne rast se email eshte bosh
if (empty($email)) {
$emailError = "Vendosni email";
}
// pattern email
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailError = "Format i pavlefshem email";
}
if(empty($password)){
$passwordError="Vendosni password";
}
//pattern password
if(!preg_match("/^\w{6,10}$/", $password)){
$passwordError="Passwordi duhet te jete 6 deri ne 10 karaktere";
}
if (empty($gender)) {
$genderError = "Vendosni gjinine";
}
}
//php code ends here
?>
<h2>Form Validation with PHP.</h2>
<form action="Ushtrimi3_2014.php" method="post">
<h2>Form</h2>
Emri:
<input name="name" type="text" value="">
<?php echo $nameError;?>
E-mail:
<input name="email" type="text" value="">
<?php echo $emailError;?>
Password:
<input name="password" type="text" value="">
<?php echo $passwordError;?>
Gender:
<input name="gender" type="radio" value="female">Female
<input name="gender" type="radio" value="male">Male
<?php echo $genderError;?>
<input class="submit" name="submit" type="submit" value="Submit">
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
我注意到您的代码中存在一些错误。 你忘了关闭主要条件。 按下提交按钮后,变量应该分配 我重写了你的代码 它可能会有所帮助
<!DOCTYPE html>
<html>
<head>
<title>Form Validation with PHP - Demo Preview</title>
<style>
.error{
color:red
}
</style>
</head>
<body>
<?php
// Initialize variables to null.
$nameError ="";
$emailError ="";
$genderError ="";
$passwordError ="";
//kur shtypim butonin submit
if(isset($_POST['submit'])){
//ne rast se emri eshte bosh
if (empty($_POST['emri'])) {
$nameError = "Vendosni emrin";
}
// emri permban vetem shkronja dhe hapesira
else if (!preg_match("/^[a-zA-Z ]*$/",$_POST['emri'])) {
$nameError = "Lejohen vetem shkronja dhe hapesire te bardha";
}
else{
$name=$_POST['emri'];
}
//ne rast se email eshte bosh
if (empty($_POST['email'])) {
$emailError = "Vendosni email";
}
// pattern email
else if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST['email'])) {
$emailError = "Format i pavlefshem email";
}
else{
$email=$_POST['email'];
}
if(empty($_POST['password'])){
$passwordError="Vendosni password";
}
//pattern password
else if(!preg_match("/^\w{6,10}$/", $_POST['password'])){
$passwordError="Passwordi duhet te jete 6 deri ne 10 karaktere";
}
else{
$password=$_POST['password'];
}
if (empty($_POST['gender'])) {
$genderError = "Vendosni gjinine";
}
else{
$gender=$_POST['gender'];
}
}
//php code ends here
?>
<h2>Form Validation with PHP.</h2>
<form action="" method="post">
<h2>Form</h2>
Emri:
<input name="emri" type="text" value="<?=(isset($name)?$name:"")?>">
<?php echo $nameError;?>
E-mail:
<input name="email" type="text" value="<?=(isset($email)?$email:"")?>">
<?php echo $emailError;?>
Password:
<input name="password" type="text" value="<?=(isset($password)? $password:"")?>">
<?php echo $passwordError;?>
Gender:
<input name="gender" type="radio" <?=(isset($gender) && ($gender=="female"))?"checked":""?> value="female">Female
<input name="gender" type="radio" <?=(isset($gender) &&
($gender=="male"))?"checked":""?> value="male">Male
<?php echo $genderError;?>
<input class="submit" name="submit" type="submit" value="Submit">
</form>
</div>
</body>
</html>
答案 1 :(得分:0)
请尝试以下操作,在输入中显示错误和正确的值,而不是删除它们:
<!DOCTYPE html>
<html>
<head>
<title>Form Validation with PHP - Demo Preview</title>
<style>
.error{
color:red
}
</style>
</head>
<body>
<?php
// Initialize variables to null.
$nameError ="";
$emailError ="";
$genderError ="";
$passwordError ="";
//kur shtypim butonin submit
if(isset($_POST['submit'])){
$name = $_POST['emri'];
$email = $_POST['email'];
$password = $_POST['password'];
$gender = $_POST['gender'];
//ne rast se emri eshte bosh
if (empty($name)) {
$nameError = "Vendosni emrin";
}
// emri permban vetem shkronja dhe hapesira
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameError = "Lejohen vetem shkronja dhe hapesire te bardha";
}
//ne rast se email eshte bosh
if (empty($email)) {
$emailError = "Vendosni email";
}
// pattern email
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailError = "Format i pavlefshem email";
}
if(empty($password)){
$passwordError="Vendosni password";
}
//pattern password
if(!preg_match("/^\w{6,10}$/", $password)){
$passwordError="Passwordi duhet te jete 6 deri ne 10 karaktere";
}
if (empty($gender)) {
$genderError = "Vendosni gjinine";
}
}
//php code ends here
?>
<h2>Form Validation with PHP.</h2>
<form action="Ushtrimi3_2014.php" method="post">
<h2>Form</h2>
Emri:
<input name="name" type="text" value="<?php (isset($name)) ? echo $name : '';?>">
<?php echo $nameError;?>
E-mail:
<input name="email" type="text" value="<?php (isset($email)) ? echo $email: '';?>">
<?php echo $emailError;?>
Password:
<input name="password" type="text" value="<?php (isset($password)) ? echo $password: '';?>">
<?php echo $passwordError;?>
Gender:
<input name="gender" type="radio" value="female" <?php (isset($gender) && $gender == 'female') ? echo selected: '';?>>Female
<input name="gender" type="radio" value="male" <?php (isset($gender) && $gender == 'male') ? echo selected: '';?>>Male
<?php echo $genderError;?>
<input class="submit" name="submit" type="submit" value="Submit">
</form>
</div>
</body>
</html>