Jump to content

need help with some simple validation


Reaper0167

Recommended Posts

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

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']);

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.