简体   繁体   中英

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. Can anyone see the issue here?. I don't see anything when I open the file. I'm fairly new to PHP. Ignore the tabled format. It's a lab from a book so the emphasis is on the PHP & JavaScript. I'm aware of CSS layout etc.

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

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

//Finally the PHP functions

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

go to php.ini and set the error reporting to E_ALL and display error to on or

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

It looks like you are using $fail="" when you actually want $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 = "hello";
var_dump($fail); //string(5) "hello"
var_dump($fail=""); //string(0) ""
var_dump(""); //string(0) ""

so your if statement then becomes

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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