kapz22 Posted April 17, 2010 Share Posted April 17, 2010 I am fairly new to php, altho do have 1 years experience in php coding. I am having great difficulty in trying to sort out a problem. I am making a simple registration form. If the user doesnt fill out the requiered fields, how am i supposed to bring errors back for this? Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/ Share on other sites More sharing options...
TeddyKiller Posted April 17, 2010 Share Posted April 17, 2010 if(empty($_POST['username'])) { echo 'Username is required'; } OR if(empty($_POST['username'])) { $errors[] = 'Username is required'; } foreach($errors as $error) { echo $error; } Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043435 Share on other sites More sharing options...
TeddyKiller Posted April 17, 2010 Share Posted April 17, 2010 Second method is the best. So that when you come to the query etc, you can do .. if(!$errors) { //QUERY CAN GO HERE.. ETC ETC ETC AS NO ERRORS ARE FOUND } Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043437 Share on other sites More sharing options...
kapz22 Posted April 17, 2010 Author Share Posted April 17, 2010 I want these errors to be displayed on the form so the user knows that a certain field is empty. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043440 Share on other sites More sharing options...
TeddyKiller Posted April 17, 2010 Share Posted April 17, 2010 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043442 Share on other sites More sharing options...
kapz22 Posted April 17, 2010 Author Share Posted April 17, 2010 currently, i have this and it doesnt seem to be doing what its supposed to do: switch ($_POST["submit"]) { case "Register": if(empty($_POST["firstName"])) { $errors[] = "please insert a name"; } //some sql goes here break; } some of the html code <div id="register"> <h2>Register</h2> <?php foreach($errors as $error) { echo $error; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043443 Share on other sites More sharing options...
mrMarcus Posted April 17, 2010 Share Posted April 17, 2010 <?php $errors['first_name'] = (empty($_POST['firstName']) ? 'Please enter a name.' : ''); //...then... ?> <form> <?php echo (!empty($errors['first_name']) ? $errors['first_name'] .'<br />' : ''); ?> <input type="firstName" /> </form> you get the idea? Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043446 Share on other sites More sharing options...
kapz22 Posted April 17, 2010 Author Share Posted April 17, 2010 I have tried doing exactly the way shown, but the page seems to reload and no errors are echoed. I am baffled on why this is not working. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043451 Share on other sites More sharing options...
PFMaBiSmAd Posted April 17, 2010 Share Posted April 17, 2010 but the page seems to reload Your form is probably not submitting the data you think it is or you have some other logic error in your code. You are having a page-wide problem. It would take seeing the code on that page to be able to help with what it is or is not doing. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043455 Share on other sites More sharing options...
mrMarcus Posted April 17, 2010 Share Posted April 17, 2010 in case you did a copy paste of my code, there is an error in the input tag: <input type="firstName" /> should at least be: <input name="firstName" type="text" /> i had type="firstName" .. i fear my code gets taken too literally sometimes. had to make sure and clear that up. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043460 Share on other sites More sharing options...
kapz22 Posted April 17, 2010 Author Share Posted April 17, 2010 here is the page that i have... <?php include($_SERVER['DOCUMENT_ROOT'] . "kers/includes/session-set.php"); if (!isset($_POST["submit"])){ $_POST["submit"] = ""; } $errors = array(); switch ($_POST["submit"]) { case "Register": $errors['first_name'] = (empty($_POST['emailAddress']) ? 'Please enter a name.' : ''); if(!$errors) { $sqlInsertMember = " INSERT INTO member (firstName, lastName, emailAddress, password) VALUES ('" . mysql_real_escape_string($_POST["firstName"]) . "', '" . mysql_real_escape_string($_POST["lastName"]) . "', '" . mysql_real_escape_string($_POST["emailAddress"]) . "', '" . mysql_real_escape_string(sha1($_POST["password"])) . "', "; mysql_query($sqlInsertMember); Header("Location: /kers/account/"); } break; } ?> <!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=utf-8"/> <title>Kapil Gohil</title> </head> <body> <div id="main-body"> <h2>Register</h2> <form name="input" action="" method="post"> <?php echo (!empty($errors['first_name']) ? $errors['first_name'] .'<br />' : ''); ?> <p>First Name:</p> <input class="input-field" type="text" name="firstName" /> <p>Last Name:</p> <input class="input-field" type="text" name="lastName" /> <p>Email Address:</p> <input class="input-field" type="text" name="emailAddress" /> <p>Password:</p> <input class="input-field" type="password" name="password" /> <input type="submit" name="submit" value="Register" /> </form> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043462 Share on other sites More sharing options...
kapz22 Posted April 17, 2010 Author Share Posted April 17, 2010 lol to the previous post by mrMarcus I only saw the mistake when u pointed it out. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043465 Share on other sites More sharing options...
mrMarcus Posted April 17, 2010 Share Posted April 17, 2010 couple things: what is this: if (!isset($_POST["submit"])){ $_POST["submit"] = ""; } and you are mishandling this: $errors['first_name'] = (empty($_POST['emailAddress']) ? 'Please enter a name.' : ''); as you are assigning $errors['first_name'] if $_POST['emailAddress'] is empty. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043466 Share on other sites More sharing options...
PFMaBiSmAd Posted April 17, 2010 Share Posted April 17, 2010 The code you posted is doing what the logic in it says to do. It echoes Please enter a name. when you don't put anything in the email field (which I'll assume is logically that way just because you are trying get anything to work at this point.) You do have a problem with your setting of the $errors elements. You are setting them to an empty string when there is no error. This however will cause if(!$errors){ to always skip the database query code. Don't set the $errors element at all if the validation test passes. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043468 Share on other sites More sharing options...
kapz22 Posted April 17, 2010 Author Share Posted April 17, 2010 yep, sorry, that was just me testing to get something echoed out. how should i be doing this in order to echo an error out then? Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043474 Share on other sites More sharing options...
mrMarcus Posted April 17, 2010 Share Posted April 17, 2010 if(!is_array($errors)) { //database query, and so on; Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043480 Share on other sites More sharing options...
kapz22 Posted April 17, 2010 Author Share Posted April 17, 2010 i am not fussed about the sql at this point, i want the errors to be echoed out onto the form. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043487 Share on other sites More sharing options...
mrMarcus Posted April 17, 2010 Share Posted April 17, 2010 i believe you are just over-complicating your code by using a switch(), unless you are planning on having multiple submit buttons? get rid of this: if (!isset($_POST["submit"])){ $_POST["submit"] = ""; } instead, do this: <?php include($_SERVER['DOCUMENT_ROOT'] . "kers/includes/session-set.php"); $errors = array(); if (isset($_POST['submit'])) { $errors['first_name'] = (empty($_POST['firstName']) ? 'Please enter a name.' : ''); if (!is_array($errors)) { $sqlInsertMember = " INSERT INTO member (firstName, lastName, emailAddress, password) VALUES ('" . mysql_real_escape_string($_POST["firstName"]) . "', '" . mysql_real_escape_string($_POST["lastName"]) . "', '" . mysql_real_escape_string($_POST["emailAddress"]) . "', '" . mysql_real_escape_string(sha1($_POST["password"])) . "', "; if ($res = mysql_query($sqlInsertMember)) { header('Location: /kers/account/'); exit(0); } } } ?> <!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=utf-8"/> <title>Kapil Gohil</title> </head> <body> <div id="main-body"> <h2>Register</h2> <form name="input" action="" method="post"> <?php echo (!empty($errors['first_name']) ? $errors['first_name'] .'<br />' : ''); ?> <p>First Name:</p> <input class="input-field" type="text" name="firstName" /> <p>Last Name:</p> <input class="input-field" type="text" name="lastName" /> <p>Email Address:</p> <input class="input-field" type="text" name="emailAddress" /> <p>Password:</p> <input class="input-field" type="password" name="password" /> <input type="submit" name="submit" value="Register" /> </form> </div> </body> </html> is a start; can obviously be improvements; Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043492 Share on other sites More sharing options...
PFMaBiSmAd Posted April 17, 2010 Share Posted April 17, 2010 An assignment statement will always create a variable if it does not already exist, even if NULL is used as the value. Using logic like $errors['first_name'] = (empty($_POST['firstName']) ? 'Please enter a name.' : ''); will always create the array entry. To keep it simple and avoid adding extra logic to test if the errors array is actually empty, you will need to use an actual if(){} statement - if(empty($_POST['first_name'])){ $errors['firstName'] = 'Please enter a name.'; } Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043496 Share on other sites More sharing options...
mrMarcus Posted April 17, 2010 Share Posted April 17, 2010 Using logic like $errors['first_name'] = (empty($_POST['firstName']) ? 'Please enter a name.' : ''); will always create the array entry. man, you totally right. it's been a loooong day. i was just trying to keep things short with the ternary, but my brain just wasn't making the connection. good call. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043500 Share on other sites More sharing options...
kapz22 Posted April 17, 2010 Author Share Posted April 17, 2010 thanks guys... really really apreciate the help. i have the code working now. Quote Link to comment https://forums.phpfreaks.com/topic/198810-show-user-errors-on-the-web-page-when-requiered-fields-are-empty/#findComment-1043504 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.