I have a script that validates form entries all with one page and it works fine with PHP4 but running it on my server with PHP5 it will not show errors like it is supposed to, and there are actually only three required fields so for testing I naturally leave out various combinations, none of them show up like they are supposed to and even when filling out the entire required fields it will not go to the done.php file for further processing.

It does however return with the Username and password field filled out like it is supposed to after they have been entered. Here is the entire code along with the HTML form code.

<html>
<body>

<h3>PHP User Registration Form Example</h3>

<?php
// only validate form when form is submitted
if(isset($submit_button)){
	$error_msg='';
	if(trim($username_input)=='' || strlen(trim($username_input)) < 6 || strlen(trim($username_input)) > 15) {
		$error_msg.="Please enter a username between 6 to 15 characters long<br>";
	}
	if(trim($password_input)=='' || strlen(trim($password_input)) < 4) {
		$error_msg.="Please enter a password at least 4 characters long<br>";
	}
	if(trim($email_input)=='') {
		$error_msg.="Please enter an email<br>";
	} else {
		// check if email is a valid address in this format username@domain.com
		if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $email_input)) $error_msg.="Please enter a valid email address<br>";
	}

// display error message if any, if not, proceed to other processing
if($error_msg==''){
//other processs goes	here like process the form to database.
header("location:done.php");
//exit;
} else {
	echo "<font color=red>$error_msg</font>";
}
}
?>

<form method="POST" action="registration.php">
<table border="1" cellpadding="7" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
  <tr>
    <td width="16%" align="right">First Name</td>
    <td width="84%">
    <input type="text" name="first_name_input" size="20" value="<?php echo $first_name_input; ?>"></td>
  </tr>
  <tr>
    <td width="16%" align="right">Last Name</td>
    <td width="84%">
    <input type="text" name="last_name_input" size="20" value="<?php echo $last_name_input; ?>"></td>
  </tr>
  <tr>
    <td width="16%" align="right">Username</td>
    <td width="84%">
    <input type="text" name="username_input" size="20" value="<?php echo $username_input; ?>"> (between 
    6 to 15 characters)</td>
  </tr>
  <tr>
    <td width="16%" align="right">Password</td>
    <td width="84%">
    <input type="password" name="password_input" size="20" value="<?php echo $password_input; ?>"> 
    (must be at least 4 characters)</td>
  </tr>
  <tr>
    <td width="16%" align="right">Email</td>
    <td width="84%">
    <input type="text" name="email_input" size="20" value="<?php echo $email_input; ?>"></td>
  </tr>
  <tr>
    <td width="16%" align="right"> </td>
    <td width="84%">
    <input type="submit" value="   Register   " name="submit_button"></td>
  </tr>
</table>
</form>

</body>
</html>

    Probably because your PHP4 machine had register globals on, which is bad, and the PHP5 machine has them set to off, which it does by default. Adjust your script like this:

    <html> 
    <body> 
    
    <h3>PHP User Registration Form Example</h3> 
    
    <?php 
    $first_name_input = $_POST['first_name_input'];
    $last_name_input = $_POST['last_name_input'];
    $username_input = $_POST['username_input'];
    $password_input = $_POST['password_input'];
    $email_input = $_POST['email_input'];
    
    // only validate form when form is submitted 
    if(isset($submit_button)){ 
        $error_msg=''; 
        if(trim($username_input)=='' || strlen(trim($username_input)) < 6 || strlen(trim($username_input)) > 15) { 
            $error_msg.="Please enter a username between 6 to 15 characters long<br>"; 
        } 
        if(trim($password_input)=='' || strlen(trim($password_input)) < 4) { 
            $error_msg.="Please enter a password at least 4 characters long<br>"; 
        } 
        if(trim($email_input)=='') { 
            $error_msg.="Please enter an email<br>"; 
        } else { 
            // check if email is a valid address in this format username@domain.com 
            if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $email_input)) $error_msg.="Please enter a valid email address<br>"; 
        } 
    
    // display error message if any, if not, proceed to other processing 
    if($error_msg==''){ 
    //other processs goes    here like process the form to database. 
    header("location:done.php"); 
    //exit; 
    } else { 
        echo "<font color=red>$error_msg</font>"; 
    } 
    } 
    ?> 
    
    <form method="POST" action="registration.php"> 
    <table border="1" cellpadding="7" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1"> 
      <tr> 
        <td width="16%" align="right">First Name</td> 
        <td width="84%"> 
        <input type="text" name="first_name_input" size="20" value="<?php echo $_POST['first_name_input']; ?>"></td> 
      </tr> 
      <tr> 
        <td width="16%" align="right">Last Name</td> 
        <td width="84%"> 
        <input type="text" name="last_name_input" size="20" value="<?php echo $_POST['last_name_input']; ?>"></td> 
      </tr> 
      <tr> 
        <td width="16%" align="right">Username</td> 
        <td width="84%"> 
        <input type="text" name="username_input" size="20" value="<?php echo $_POST['username_input']; ?>"> (between 
        6 to 15 characters)</td> 
      </tr> 
      <tr> 
        <td width="16%" align="right">Password</td> 
        <td width="84%"> 
        <input type="password" name="password_input" size="20" value="<?php echo $_POST['password_input']; ?>"> 
        (must be at least 4 characters)</td> 
      </tr> 
      <tr> 
        <td width="16%" align="right">Email</td> 
        <td width="84%"> 
        <input type="text" name="email_input" size="20" value="<?php echo $_POST['email_input']; ?>"></td> 
      </tr> 
      <tr> 
        <td width="16%" align="right"> </td> 
        <td width="84%"> 
        <input type="submit" value="   Register   " name="submit_button"></td> 
      </tr> 
    </table> 
    </form>

      That made a difference, now it blanks all the fields out upon submission and the error message still doesn't come up. Even if all fields are properly filled out it never goes to the done.php file it seems to be in a constant loop.

        I missed the first one it should be this

        <html>
        <body>
        
        <h3>PHP User Registration Form Example</h3>
        
        <?php
        
        
        // only validate form when form is submitted
        if(isset($_POST['submit_button'])){
        	$first_name_input = $_POST['first_name_input'];
        	$last_name_input = $_POST['last_name_input'];
        	$username_input = $_POST['username_input'];
        	$password_input = $_POST['password_input'];
        	$email_input = $_POST['email_input'];
        
        $error_msg='';
        if(trim($username_input)=='' || strlen(trim($username_input)) < 6 || strlen(trim($username_input)) > 15) {
        	$error_msg.="Please enter a username between 6 to 15 characters long<br>";
        }
        if(trim($password_input)=='' || strlen(trim($password_input)) < 4) {
        	$error_msg.="Please enter a password at least 4 characters long<br>";
        }
        if(trim($email_input)=='') {
        	$error_msg.="Please enter an email<br>";
        } else {
        	// check if email is a valid address in this format username@domain.com
        	if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $email_input)) $error_msg.="Please enter a valid email address<br>";
        }
        
        // display error message if any, if not, proceed to other processing
        if($error_msg==''){
        	//other processs goes    here like process the form to database.
        	header("location:done.php");
        	//exit;
        } else {
        	echo "<font color=red>$error_msg</font>";
        }
        }
        ?>
        
        <form method="POST" action="registration.php">
        <table border="1" cellpadding="7" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
          <tr>
            <td width="16%" align="right">First Name</td>
            <td width="84%">
            <input type="text" name="first_name_input" size="20" value="<?php echo $_POST['first_name_input']; ?>"></td>
          </tr>
          <tr>
            <td width="16%" align="right">Last Name</td>
            <td width="84%">
            <input type="text" name="last_name_input" size="20" value="<?php echo $_POST['last_name_input']; ?>"></td>
          </tr>
          <tr>
            <td width="16%" align="right">Username</td>
            <td width="84%">
            <input type="text" name="username_input" size="20" value="<?php echo $_POST['username_input']; ?>"> (between
            6 to 15 characters)</td>
          </tr>
          <tr>
            <td width="16%" align="right">Password</td>
            <td width="84%">
            <input type="password" name="password_input" size="20" value="<?php echo $_POST['password_input']; ?>">
            (must be at least 4 characters)</td>
          </tr>
          <tr>
            <td width="16%" align="right">Email</td>
            <td width="84%">
            <input type="text" name="email_input" size="20" value="<?php echo $_POST['email_input']; ?>"></td>
          </tr>
          <tr>
            <td width="16%" align="right"> </td>
            <td width="84%">
            <input type="submit" value="   Register   " name="submit_button"></td>
          </tr>
        </table>
        </form> 

          Hmmm...no difference, I wonder what would this be. The other server is using PHP 5.1.1 on Apache 2.0.55. When server start using PHP 5 are you going to have to go back and rewrite a bunch of old code, if so then I need to know a whole lot more about PHP 5 as opposed to PHP4, which both my internet webservers are using presently?

            I'm running php 5.1.1 and Apache 2.0.53 and the script works as expected. Errors come up in red for email password and user name if they are incorrectly filled in, and if they aren't the script goes looking for done.php

              My fault I only saw the isset part not the other

              $_POST['password_input']

              Sorry I am kind of tired and only saw the first change. Also I had changed the name of the file by adding 2 to the end of it but guess what file that the script was looking for? The wrong original one...oh well it is time for more coffee!

                Write a Reply...