Jump to content

[SOLVED] Array Help


HoTDaWg

Recommended Posts

well basically this script takes care of errors in a registeration form. The problem is multiple errors can occurr under one field and i want to be able to have the fields with errors highlighted with red once an error is found.

bare in mind my code is not complete so, some of the functions in my registeration form may not work right now. I am currently more concerned about the error handlign. here is the code:

<?php
function form(){
$submit = $_GET['submit']; 
if(isset($submit)){
   	echo '<form name="hey" action="'.$_SERVER[php_SELF].'"><input type="text" maxlength="12" name="username" value=""><br><input type="text" name="password" value="" maxlength="50"><br><input type="text" maxlength="80" name="email" value=""><br><input type="submit" name="submit" value="submit"></form>';
   	}
}
//It Begins.
$arrErrors = array();
$conn = mysql_pconnect("localhost","mediacir","entersandman");
   		mysql_select_db("mediacir_community");
//Get it, get it now.
if(isset($_POST['submit'])){
$username = $_GET['username'];
$password = $_GET['password'];
$email = $_GET['email'];
$submit = $_GET['submit']; 
$ip = GetHostByName($REMOTE_ADDR);
$date = date('d m Y @ h:i:s A'); 
//Empty?! I THINK NOT!
	if(empty($username)){
	$arrErrors['username']= "The Username Field Was Left Blank";
	}
	if(empty($password)){
	$arrErrors['password']= "The Password Field Was Left Blank";
	}
	if(empty($email)){
	$arrErrors['email']= "The E-mail Field Was Left Blank";
	}
//Too Long?! I THINK NOT!
	if(strlen($username) > 12){
	$arrErrors['username']= "The Username Field exeeds the limit of <b>12</b> characters";
	}
	if(strlen($password) > 14){
	$arrErrors['password']= "The Password Field exeeds the limit of <b>14</b> characters";
	}		
	if(strlen($email) > 50){
	$arrErrors['email']=>'toolong'= "The E-mail Field exeeds the limit of <b>50</b> characters";
	}
//Invalid characters...grr...
        if (preg_match('|[^a-z\d]|i', $username)) {
	$arrErrors['username'] = "The Username Field contains invalid characters. <B>Alphanumeric (a-z, A-Z, 0-9)</b> are accepted only.";
        }
	if (preg_match('|[^a-z\d]|i', $password)) {
	$arrErrors['password'] = "The Password Field contains invalid characters. <B>Alphanumeric (a-z, A-Z, 0-9)</b> are accepted only.";
	}
//FINALLY!
	if(!count($arrErrors) > 0){
	//Secure the variables
   		$email=strip_tags($email);
   		$username=strip_tags($username);
   		$password=strip_tags($password);
   		$email=addslashes($email);
   		$username=addslashes($username);
   		$password=addslashes($password);
//Create the display name
   		$displayname =&$username;
   		$username=strtolower($username);
//final filter
   		$username = mysql_real_escape_string($username);
   		$password = mysql_real_escape_string($password);
   		$displayname = mysql_real_escape_string($displayname);
   		$email = mysql_real_escape_string($email);
   		$password = md5($password);
	echo "done";
	}else{
	$strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>';
// Get each error and add it to the error string
        // as a list item.
        	foreach ($arrErrors as $error) {
            	$strError .= "<li>$error</li>";
        	}
        	$strError .= '</ul></div>';
		echo "<style>
  label {
  width: 80px;
  text-align: right;
  float: left;
}

.formerror {
  border: 1px solid red;
  background-color : #FFCCCC;
  width: auto;
  padding: 5px 0;
}

.errortext {
  padding-left: 80px;
  font: bold smaller sans-serif;
}
</style>";
	echo $strError;
	}
}else{
echo form();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/42436-solved-array-help/#findComment-205869
Share on other sites

1. i just figured if there are multiple errors for one field in a form, that means multiple keys.

2. the email addition is for the email field in the form. i thought it would  make it easier to echo the  errors underneath the email field if they are already sorted out in the correct locations.

 

am i correct to do this?

Link to comment
https://forums.phpfreaks.com/topic/42436-solved-array-help/#findComment-205882
Share on other sites

1. i just figured if there are multiple errors for one field in a form, that means multiple keys.

2. the email addition is for the email field in the form. i thought it would  make it easier to echo the  errors underneath the email field if they are already sorted out in the correct locations.

 

am i correct to do all this? and how can i fix up my array so it will actually work?

 

right now im getting this:

Parse error: syntax error, unexpected T_DOUBLE_ARROW in ***************** on line 47

 

sorry to double post, but it wont let me modify for some reason:S

Link to comment
https://forums.phpfreaks.com/topic/42436-solved-array-help/#findComment-205897
Share on other sites

id say keep your errors as single entries instead of arrays. so if some one puts in an incorrect email address and its over 50 chars long, just make two entries - one as invalid_email the other as invalid_email_length

 

if you were to create the email error as an array

 

arry['email'] = array('length' => 'too long', 'parse' => 'not an email');

 

when you go to parse the errors, you have to account for this single exception - keep it simple

Link to comment
https://forums.phpfreaks.com/topic/42436-solved-array-help/#findComment-205902
Share on other sites

artacus, if i were to use:

<?php
$arrErrors['email'] = array('type'=>'toolong', 'description'=>"The email ...");
?>

 

when echoing the error how would i do it?

 

right now i do:

<?php
	$strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>';
// Get each error and add it to the error string
        // as a list item.
        	foreach ($arrErrors as $error) {
            	$strError .= "<li>$error</li>";
        	}
        	$strError .= '</ul></div>';
?>

 

something along the lines of that.

Link to comment
https://forums.phpfreaks.com/topic/42436-solved-array-help/#findComment-205906
Share on other sites

would this work?

<?php
	$strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>';
// Get each error and add it to the error string
        // as a list item.
        	foreach ($arrErrors as $type as $description) {
            	$strError .= "<li>$description</li>";
        	}
        	$strError .= '</ul></div>';
?>

Link to comment
https://forums.phpfreaks.com/topic/42436-solved-array-help/#findComment-205907
Share on other sites

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.