ambo Posted December 13, 2008 Share Posted December 13, 2008 I have a register form process page and i would like to know how to set up a if then stament i have all the error checking done So if no errors then submit sql command <?php //This makes sure they did not leave any fields blank if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] ) { die('You did not complete all of the required fields'); } // checks if the username is in use if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); } $usercheck = $_POST['username']; $check = mysql_query("SELECT username FROM users WHERE username = '$usercheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the username '.$_POST['username'].' is already in use.'); } // checks if the username is in use if (!get_magic_quotes_gpc()) { $_POST['email'] = addslashes($_POST['email']); } $emailcheck = $_POST['email']; $check = mysql_query("SELECT email FROM users WHERE email = '$emailcheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the email '.$_POST['email'].' is already in use.'); } // this makes sure both passwords entered match if ($_POST['pass'] != $_POST['pass2']) { die('Your passwords did not match. '); } Link to comment https://forums.phpfreaks.com/topic/136788-if-and-then/ Share on other sites More sharing options...
xtopolis Posted December 13, 2008 Share Posted December 13, 2008 A method I like to use: (set the form as ready, if anything stops it, make it not ready, and accumulate errors) <?php $readyForm = true; $errors = array(); if(empty($_POST['username']) || empty($_POST['password'])){ $readyForm = false; $errors[] = "A username AND password is required"; } //more if statements; //more if statements; //more if statements; if($readyForm) { //submit }else{ //display form + errors } ?> Link to comment https://forums.phpfreaks.com/topic/136788-if-and-then/#findComment-714404 Share on other sites More sharing options...
chronister Posted December 13, 2008 Share Posted December 13, 2008 Yeah, you should have better error handling than just using a die statement. A die statement gives a blank page with your message and the user has to hit the back button. I am one of those developers who HATES bad form design, and as such refuses to make any of my forms with those problems. In my opinion a form should do the following things. 1. If there are errors, show the form again and re-fill the values of the form elements to what was already entered so your user don't have to fill in the form AGAIN, they only need to fix the errors. 2. Call out the errors by making the field in error state VERY noticeable. 3. Show a summary of all the errors so that all errors are seen, and not just 1 at a time. I have attached a screenshot of a "good" form design error handler. It is from a form class I am working on to create forms and handle the processing of them all in one step. This one is VERY bright. I will eventually tone down the colors and such, but you catch my drift. Notice, that the invalidEmail address was filled back in, the selection of No in the radio buttons was checked again. All the errors are shown at the top in the summary. Each field that has errors is highlighted and an Icon is displayed in that field. Nate [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/136788-if-and-then/#findComment-714411 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.