简体   繁体   English

PHP和JavaScript表单验证脚本的问题

[英]Problems with PHP & JavaScript form validation script

I'm having some trouble with this script for form validation, using initially JavaScript and then validating again, and then submitting with PHP. 我在使用此脚本进行表单验证时遇到了一些麻烦,最初使用JavaScript,然后再次进行验证,然后使用PHP提交。 Can anyone see the issue here?. 有人可以在这里看到问题吗? I don't see anything when I open the file. 打开文件时我什么也没看到。 I'm fairly new to PHP. 我对PHP相当陌生。 Ignore the tabled format. 忽略表格格式。 It's a lab from a book so the emphasis is on the PHP & JavaScript. 这是一本书的实验室,因此重点是PHP和JavaScript。 I'm aware of CSS layout etc. 我知道CSS布局等。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks in advance 提前致谢

<?php

error_reporting(~0); ini_set('display_errors', 1);

//PHP adduser.php

//Start with the PHP code

$forename=$surname=$password=$age=$email="";

if(isset($_POST['forename']))
$forename = fix_string($_POST['forename']);
if(isset($_POST['surname']))
$surname = fix_string($_POST['surname']);
if(isset($_POST['password']))
$password = fix_string($_POST['password']);
if(isset($_POST['age']))
$age = fix_string($_POST['age']);
if(isset($_POST['email']))
$email = fix_string($_POST['email']);

$fail= validate_forename($forename);
$fail.= validate_surname($surname);
$fail.= validate_password($password);
$fail.= validate_age($age);
$fail.= validate_email($email);

echo "<html><head><title>An example form</title></head><body>";

if($fail==""){
  echo "Form data successfully validated: $forename,
  $surname, $password, $age, $email";        
}



//This is where you'd enter post fields to the DB
exit;

//Now the HTML & JavaScript goes here

echo<<<_SOQ 回声<<< _ SOQ

<style type="text/css">.signup{
  border:1px solid #999999;
  font:normal 14px helvetica;
  color:#444444;
  }
</style>


 <script type="text/javascript">

   function validate(form){

    fail = validateForename(form.forename.value);
    fail += validateSurname(form.surname.value);
    fail += validatePassword(form.password.value);
    fail += validateAge(form.age.value);
    fail += validateEmail(form.email.value);

      if(fail=="") return true;
      else alert(fail); return false;

    }

</script>

</head>
    <body>
      <table class="signup" border="0" cellpadding="2" cellspacing="5" 
       bgcolor="#eeeeee">
        <th colspan="2" align="center">Sign up form</th>

  <tr>
    <td colspan="2">Sorry, the following errors were found<br/>
     in your form: <i>$fail</i>
    </td>
  </tr>


<form method="post" action="adduser.php" onsubmit="validate(this.form)">

<tr><td>Forename:</td><td><input type="text" maxlength="32"
  name="forename" value="$forename"/></td>
</tr>

<tr><td>Surname:</td><td><input type="text" maxlength="32"
  name="surname" value="$surname"/></td>
</tr>

<tr><td>Password:</td><td><input type="text" maxlength="32"
  name="password" value="$password"/></td>
</tr>

<tr><td>Age:</td><td><input type="text" maxlength="32"
  name="age" value="$age"/></td>
</tr>

<tr><td>Email:</td><td><input type="text" maxlength="32"
  name="email" value="$email"/></td>
</tr>

<tr><td colspan="2" align="center">
   <input type="submit" value="Sign-up"/></td>
</tr>

  </form>
</table>
<script type="text/javascript">

function validateForename(field){
  if(field=="") return "No surname was entered";
  return "";
}

function validateSurname(field){
  if(field=="") return "No surname was entered";
  return "";
}

function validatePassword(field){
   if(field=="") return "No surname was entered";
   else if(field.length<6) return "Passwords, must be at least 6 characters";
   else if([^/a-zA-Z0-9_-/]) return "Only a-zA-Z0-9_- characters are allowed ";
   return "";
 }

function validateAge(field){
   if((field=="") || (isNaN(field)) return "No age was entered";
   else if((field<18) || (field>101))  return "Age must be between 18 and 101 years";
   else if(/[^a-zA-Z0-9_-]/.test(field)) 
   return "Only a-zA-Z0-9_- characters are allowed ";
   return "";
 }

 function validateEmail(field){
   if(field=="") return "No surname was entered";
   else if(!((field.indexOf('@')>0) && (field.indexOf('.')>0)) ||
   /[^a-zA-Z0-9-]/.test(field)) return "E-mail address invalid";
   return "";
 }

</script>

  </body>
</html>

_SOQ; _SOQ;

//Finally the PHP functions //最后是PHP函数

function validate_forename($field){
  if($field=="") return "No forename was entered";
  return "";
}

function validate_surname($field){
  if($field=="") return "No surname was entered";
  return "";
}

function validate_password($field){
  if($field=="") return "No password was entered";
  elseif(strlen($field) <6) return "Passwords, must be at least 6 characters";
  elseif(!preg_match("/[^a-zA-Z0-9_-]/", $field)) 
  return "Only a-zA-Z0-9_- characters are allowed ";
  return "";
}

function validate_age($field){
  if($field<18 || field>101) return "Age must be between 18 and 101 years";
  return "";
}

function validate_email($field){
  if($field=="") return "No surname was entered";
  elseif(!(strpos($field, ".")>0) && 
         (strpos($field, "@")>0) ||
          preg_match("/[^a-zA-Z0-9_]/",$field)) 
        return "E-mail address invalid";
        return "";
}

//sanitise the PHP input

function fix_string($string){
  if(get_magic_quotes_gpc($string)) stripslashes($string);
  return htmlentities($string);

 }

?> ?>

if you dont see anything, you have a php error 如果您什么都没有看到,则说明您的PHP错误

go to php.ini and set the error reporting to E_ALL and display error to on or 转到php.ini并将错误报告设置为E_ALL并将错误显示为on或

<?php
     error_reporting(E_ALL);
     ini_set("display_errors", 1);
?>
if($fail=""){

You assign instead of check for equality. 您分配而不是检查是否相等。 And then, since the assigned value is falsey, the corresponding success code will never be executed. 然后,由于分配的值为false,因此将永远不会执行相应的成功代码。

It looks like you are using $fail="" when you actually want $fail=="" . 当您实际上想要$fail==""时,您似乎正在使用$fail="" $fail=="" The first is an assignment expression and the second with == is a comparison expression. 第一个是赋值表达式,第二个带有==的表达式是比较表达式。 Your usage will always resolve to false (via the empty string you just set) no matter what $fail was set to before because you just reset it: 不管之前将$fail设置为什么,您的用法将始终解析为false (通过您刚刚设置的空字符串),因为您只是将其重置:

$fail = "hello";
var_dump($fail); //string(5) "hello"
var_dump($fail=""); //string(0) ""
var_dump(""); //string(0) ""

so your if statement then becomes 所以你的if语句就变成

if("") {
    //never get here
} else {
    //always get here, but you have nothing defined
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM