0

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

 }

?>

0

3 Answers 3

1

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);
?>
Sign up to request clarification or add additional context in comments.

9 Comments

Psst: error_reporting(~0); ini_set('display_errors', 1); - otherwise your code will be the fatal error itself.
or: error_reporting(E_ALL); ini_set("display_errors", 1); it was out of my head...tnx
Nah, that hasn't done anything. I placed it at the top of my file.
then you have an error before that(do you include something ?) check the error log
you have this _SOQ; closing HEREDOC references (_SOQ) are both indented by a space - you can't have any whitespace at the start of the line with the closing tag. fix that
|
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.

1 Comment

Yeah, had a few minor syntax errors which I corrected (that one included) but still having some issues.
1

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
}

2 Comments

Yeah I saw that silly mistake early on. Thanks. Still not working and I've been over it several times.
You could try to look through the Apache error logs, PHP and other errors are logged there and your server might be swallowing something important. I've also run into some odd SELinux permission limits before; I usually turn SELinux off in dev environments. Also that exit seems like it could easily cause some problems, but it's hard to tell from the code fragments (instead of the whole script).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.