Reaper0167 Posted December 21, 2008 Share Posted December 21, 2008 i'm have three text fields for name, email and password. if one or more is not filled in, i'm looking to have an error message appear to the right of the text field. (ex: Name is required, or Email is required) and so on. What is the simplest way to do achieve this. I am new to PHP and I cannot find a simple solution anywhere. here is what i have so far for adding the completed fields to the database. my form name on my index page is named "register" <?php /* server and database information */ $host="--------"; $username="-----------"; $password="-----------"; $db_name="----------"; $tbl_name="members"; /* connects to server and database */ mysql_connect("$host","$username","$password")or die("Could not connect."); mysql_select_db("$db_name")or die("Could not find database"); /* define variables from form */ $username=$_POST["username"]; $password=md5($_POST["password"]); $email=$_POST["email"]; /* inserting data into your database */ $sql="INSERT INTO $tbl_name(username, password, email)VALUES('$username','$password','$email')"; $result=mysql_query($sql); /* success or error text display */ if($result){ echo "Thank you for registering."; } else { echo "ERROR"; } /* closing your connection */ mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/137873-need-help-with-some-simple-validation/ Share on other sites More sharing options...
ngreenwood6 Posted December 21, 2008 Share Posted December 21, 2008 First of all when you are posting you should use the code tags for code it is much easier to read. You code here is flawed a little bit. Here is a better method: <?php /* server and database information */ $host="--------"; $username="-----------"; $password="-----------"; $db_name="----------"; $tbl_name="members"; /* connects to server and database */ mysql_connect("$host","$username","$password")or die("Could not connect."); mysql_select_db("$db_name")or die("Could not find database"); /* define variables from form */ $username=$_POST["username"]; $password=$_POST["password"]); $encrypted_password=md5($password); $email=$_POST["email"]; /* inserting data into your database */ $sql="INSERT INTO $tbl_name(username, password, email)VALUES('$username','$encrypted_password','$email')"; if(empty($username)) { echo "Please enter a username!"; } elseif(empty($password)) { echo "Please enter a password!"; } elseif(empty($email)) { echo "Please enter an email address!"; } else { $result = mysql_query($sql); //redirect them to another page header("location:user_registered.php"); } /* closing your connection */ mysql_close(); ?> Basically the code that you had would register a user no matter what. When you post the $result from this is will run the query. So no matter what the user was getting entered. In this case it checks the variables first and then if everything is ok it will enter there data in the database. Also as far as putting the errors on the same page you will have to post the page to itself which would be $_SERVER['PHP_SELF']. But if you wanted something that did it without refreshing you would have to look into some javascript/ajax. edit: oh yeah forgot to add this but all of your variables should be filtered by using the mysql_real escape_string like this: $username = mysql_real_escape_string($_POST['username']); Link to comment https://forums.phpfreaks.com/topic/137873-need-help-with-some-simple-validation/#findComment-720568 Share on other sites More sharing options...
Reaper0167 Posted December 21, 2008 Author Share Posted December 21, 2008 thanks for your time bud. if anyone else has any other suggestions, feel free. thanks again! Link to comment https://forums.phpfreaks.com/topic/137873-need-help-with-some-simple-validation/#findComment-720570 Share on other sites More sharing options...
ngreenwood6 Posted December 21, 2008 Share Posted December 21, 2008 no problem. if you have any questions or need any more help feel free to ask. Link to comment https://forums.phpfreaks.com/topic/137873-need-help-with-some-simple-validation/#findComment-720571 Share on other sites More sharing options...
chronister Posted December 21, 2008 Share Posted December 21, 2008 Typically when dealing with errors, I create an error array with the fieldname as the array key and the message I want to display as the array value. e.g. <?php $error['firstName'] = 'Please enter your First Name'; $error['lastName'] = 'Please enter your Last Name'; ?> Then I create a function to check and see if that particular error key is set. <?php function checkError($fieldName) { if(isset($error[$fieldName])) // if an error array is set with this fields name as key { echo $error[$fieldName]; // echo that error array item, which echo's the message you set. } } ?> Next to the field or wherever you wish to have it display, you call that function and pass the fieldname to it. here is a more complete example in action. form.php <li <?php checkError('firstName'); ?>> <label>First Name</label> <input type="text" name="firstName" id="firstName" <?php errorValue('firstName'); ?>/><?php errorIcon('firstName'); ?> </li> include_file.php <?php /******************************************************************************** errorValue() fills in the fields with the entered values when an error occurs To be used with input fields that require value="" $field - The name of the field being checked *******************************************************************************/ function errorValue($field) { global $error; if(isset($error) && $_POST[$field] != '') { echo 'value="'.$_POST[$field].'"'; } } /******************************************************************************** checkError() adds class="appError" to the <li> tag to highlight the field that is in error state. @param $field - The name of the field being checked *******************************************************************************/ function checkError($field) { global $error; if(isset($error[$field])) { echo 'class="appError"'; } } /************************************************************* errorIcon() prints an icon next to the fields that have error values.... it is only used on required fields **********************************************************/ function errorIcon($fieldName) { global $error; if(isset($error[$fieldName])) { echo ' <img class="errorIcon" src="/images/errorIcon.gif" align="top" alt="'.$error[$fieldName].'" title="'.$error[$fieldName].'" /> '/*<span class="errorMsg">'.$error[$fieldName].'</span>'*/; } } ?> As you can see in the attached image, it highlights the field, it will re-fill a value if one was entered, it displays an icon next to the field, and it will print a list of error messages at the top of the page. Notice that that the alt value and the title value for the error icon is the actual message. If hovered over it displays the message for that field. I am creating a form class to make this whole thing very easy. I will be posting it on here in the near future to get input on it. Hope this helps. Nate [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/137873-need-help-with-some-simple-validation/#findComment-720598 Share on other sites More sharing options...
Reaper0167 Posted December 21, 2008 Author Share Posted December 21, 2008 i was trying to keep this as simple as possible, but I will take a look at doing it that way tomorrow, thanks for the tip. I just started with PHP so I have sticky notes all over the place until I get the hang of things. thanks again. Link to comment https://forums.phpfreaks.com/topic/137873-need-help-with-some-simple-validation/#findComment-720604 Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 loook it easy all you need to no if the field is empty or not use empty() or use $name=" "; both are null create a error message. Link to comment https://forums.phpfreaks.com/topic/137873-need-help-with-some-simple-validation/#findComment-720624 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.