0

我在使用此脚本进行表单验证时遇到了一些问题,最初使用 JavaScript,然后再次验证,然后使用 PHP 提交。任何人都可以在这里看到这个问题吗?打开文件时,我什么也看不到。我对 PHP 相当陌生。忽略表格格式。这是一本书的实验室,所以重点是 PHP 和 JavaScript。我知道 CSS 布局等。

任何帮助将不胜感激。提前致谢

<?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

回声<<<_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;

//最后是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);

 }

?>

4

3 回答 3

1
if($fail=""){

您分配而不是检查是否相等。然后,由于分配的值是假的,相应的成功代码将永远不会被执行。

于 2012-09-01T13:31:44.493 回答
1

看起来您$fail=""在实际需要时使用$fail=="". 第一个是赋值表达式,第二个 with==是比较表达式。false无论$fail之前设置什么,您的使用将始终解析为(通过您刚刚设置的空字符串),因为您只是将其重置:

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

所以你的 if 语句然后变成

if("") {
    //never get here
} else {
    //always get here, but you have nothing defined
}
于 2012-09-01T13:32:18.313 回答
1

如果你没有看到任何东西,你有一个 php 错误

转到 php.ini 并将错误报告设置为 E_ALL 并将错误显示为 on 或

<?php
     error_reporting(E_ALL);
     ini_set("display_errors", 1);
?>
于 2012-09-01T12:05:44.040 回答