Jump to content

suggestions on how to write this code better


bbram

Recommended Posts

suggestions on how to write this code better

I have a form that has 2 textboxes in it. The php listed below...confirms that the passwords match, confirms that there are at least 6 characters in in, confirms that there is at least 1 number. I am doing mostly if statements with it, but I was wondering if there was a better way to write.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
     <form name="form1" method="post" action="<?PHP $_SERVER['PHP_SELF'] ?>">

<table width="100%" border="1">
  <tr>
    <th scope="row"> <strong>New password:  </strong>
            <input type="password" maxlength="15" name="password"></th>
<th scope="row"> <strong>New password confirmed: </strong>
            <input type="password" maxlength="15" name="confirm_password"></th>
<th scope="row"><input type="submit" name="update" value="Update Password" /></th>
  </tr>
</table>

</form>
<?PHP
//verify that the update button is pressed
if (isset($_POST['update']))
{
//verify that the passwords match
if($_POST['password'] == $_POST['confirm_password']) 
{
	echo "passwords match";
	echo "<br>";

}
//verify that the password and password confirmation was entered
elseif ((isset($_POST['password'])) && ($_POST['confirm_password']))
{
	echo "Password and confirm password set";
	echo "<br>";
}		
//verify that the # of characters was entered
elseif (strlen($_POST['password'])<6)
{
		$num_char=strlen($_POST['password']); 
		echo "Please enter more characters";
		echo "<br>";
}
//find out if number exsist
if (strlen($_POST['password'])>5)
{
	$subject = $_POST['password'];
	preg_match_all('/[0-9]/', $subject, $matches);
	$count = count($matches[0]);
	//echo $count;
	if ($count <=1)
	echo "Please enter in at least 1 number you currently have: " .$count;
}//close of verification of both boxes entered

}//close of $_POST
?> 

</body>
</html>

Link to comment
Share on other sites

Typically you wouldn't have a bunch of confirmation statements for all the validations. If you really want all those messages for each validation, then you need separate logic for each. But, if you only want to provide messages when there is a problem - as is typical - then here is another method:

 

Put the validation logic at the topo of the page. Then you can provide any error messages at the top of the page and repopulate form fields with the entered values if appropriate.

 

<?php

//verify that the update button is pressed
if (isset($_POST['update']))
{
    //Parse submitted data
    $password = (isset($_POST['password'])) ? $_POST['password'] : '';
    $confirm = (isset($_POST['password'])) ? $_POST['confirm'] : '';
    
    //Perform validation and determine if errors exist
    $error = false;
    //Verify that required fields have values
    if(empty($password) || empty($confirm))
    {
        $error = "All fields are required";
    }
    //Verify length
    elseif(strlen($password) < 6)
    {
        $error = "Passwords must be at least 6 characters long.";
    }
    //Verify content
    elseif(preg_match('/[0-9]/', $password)==0)
    {
        $error = "Passwords must contain at least one number.";
    }
    //Verify match with confirmation
    elseif($password != $confirm)
    {
        $error = "Passwords do not match";
    }
    //Validation complete, check if error occured
    if(!$error)
    {
        //Do something with the data and redirect to confirmation page
    }
}

?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[url=http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<html xmlns="[url=http://www.w3.org/1999/xhtml]http://www.w3.org/1999/xhtml[/url]">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<div style=\"color:red;\"><?php echo $error; ?></div>
<form name="form1" method="post" action="<?PHP $_SERVER['PHP_SELF'] ?>">
<table width="100%" border="1">
  <tr>
    <th scope="row"> <strong>New password:  </strong>
            <input type="password" maxlength="15" name="password"></th>
   <th scope="row"> <strong>New password confirmed: </strong>
            <input type="password" maxlength="15" name="confirm"></th>
   <th scope="row"><input type="submit" name="update" value="Update Password" /></th>
  </tr>
</table>
</form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.