bbram Posted February 26, 2011 Share Posted February 26, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/228880-suggestions-on-how-to-write-this-code-better/ Share on other sites More sharing options...
requinix Posted February 26, 2011 Share Posted February 26, 2011 Not really. Not beyond shrinking the code - fewer lines, fewer variables, that kind of thing. You can write stuff that looks cooler and can be a bit more flexible, but you don't really get anything out of it. Arguably worse, even. Quote Link to comment https://forums.phpfreaks.com/topic/228880-suggestions-on-how-to-write-this-code-better/#findComment-1179904 Share on other sites More sharing options...
Psycho Posted February 26, 2011 Share Posted February 26, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/228880-suggestions-on-how-to-write-this-code-better/#findComment-1179908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.