Page 1 of 1

Displaying Errors In a Form

Posted: Sun May 08, 2005 12:05 am
by anthony88guy
I want to display errors by using a function I made (located on function.php):

Code: Select all

function error($errors){
	if($errors){
		return "<table width=\"250\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  				<tr bgcolor=\"#990000\"> 
    			<td><div align=\"center\"><font color=\"#CCCCCC\"><strong>Please Fix the Following 
       			Errors</strong></font></div></td>
  				</tr>
  				<tr bgcolor=\"#990000\"> 
    			<td bgcolor=\"#333333\"><ul>"
		foreach($errors[] as $fix => $value){
			return implode("<li></li>",$fix);
		}
  		return "</ul></td></tr></table>";
	}else{
		$error = 0;
		return $error;
	}
}
Here is the form error checking:

Code: Select all

include_once('function.php');
			$errors = array();
			
			if($atopcommander == ""){
   				$atopcommander = 'none';
			}
			
			$addcheck = mysql_query("SELECT * FROM `farms` WHERE username = '$ausername'")  or die(mysql_error());

			if(mysql_num_rows($addcheck) !== 0){
				$errors[] = "Farm Already In Database";
			}
			
			if($ausername = "" && $daefenceaction == "" && $aarmysize == "" && $astatid == ""){
				$errors[] = "Please Fill In All Fields";
			}
				
			if((!is_numeric($adefenceaction)) && (!is_numeric($aarmysize))){	
				$errors[] = "Please Recheck Defence Action and Armysize.";
			}			
						
			if((strlen($astatid) <= 6) && (!is_numeric($astatid))){
				$errors[] = "Please Check Stat ID.";
			}			
			
			error($errors);		
			
			if($error == 0){
				$timeadd = time();
				mysql_query("INSERT INTO farms (id, username, topcommander, defenceaction, armysize, statid, comments, author, time) VALUES(NULL, '$ausername', '$atopcommander', '$adefenceaction', '$aarmysize', '$astatid', '$acomments', '$author', '$timeadd')") or die(mysql_error());
				mysql_query("UPDATE users SET farmcount = farmcount + 1 WHERE user = '$username' LIMIT 1") or die(mysql_error());
				print "Farm Was Added To Database";
			}
I'm getting errors about the foreach. I need a push in the right direction, many thanks.

Posted: Sun May 08, 2005 1:09 am
by hongco
print_r ($errors);

see what you got.

Posted: Sun May 08, 2005 5:34 am
by Chris Corbyn

Code: Select all

foreach($errors[] as $fix => $value){

//change to
foreach($errors as $fix => $value){

Posted: Sun May 08, 2005 10:16 am
by anthony88guy

Code: Select all

$ausername = $_POST&#1111;'username'];
			$atopcommander = $_POST&#1111;'topcommander'];
			$adefenceaction = trim(str_replace(&quote;,&quote;,&quote;&quote;,$_POST&#1111;'defenceaction']));
			$aarmysize = trim(str_replace(&quote;,&quote;,&quote;&quote;,$_POST&#1111;'armysize']));	
			$astatid = trim($_POST&#1111;'statid']);
			$acomments = $_POST&#1111;'comments'];
			$author = $_POST&#1111;'author'];
			
			include_once('function.php');
			$errors = array();
			
			if($atopcommander == &quote;&quote;){
   				$atopcommander = 'none';
			}
			
			$addcheck = mysql_query(&quote;SELECT * FROM `farms` WHERE username = '$ausername'&quote;)  or die(mysql_error());

			if(mysql_num_rows($addcheck) !== 0){
				$errors&#1111;] = &quote;Farm Already In Database&quote;;
			}
			
			if($ausername = &quote;&quote; &amp;&amp; $daefenceaction == &quote;&quote; &amp;&amp; $aarmysize == &quote;&quote; &amp;&amp; $astatid == &quote;&quote;){
				$errors&#1111;] = &quote;Please Fill In All Fields&quote;;
			}
				
			if((!is_numeric($adefenceaction)) &amp;&amp; (!is_numeric($aarmysize))){	
				$errors&#1111;] = &quote;Please Recheck Defence Action and Armysize.&quote;;
			}			
						
			if((strlen($astatid) &lt;= 6) &amp;&amp; (!is_numeric($astatid))){
				$errors&#1111;] = &quote;Please Check Stat ID.&quote;;
			}			
			
			if($errors){
				print &quote;&lt;table width=\&quote;250\&quote; border=\&quote;0\&quote; cellspacing=\&quote;0\&quote; cellpadding=\&quote;0\&quote;&gt;
  						&lt;tr bgcolor=\&quote;#990000\&quote;&gt; 
    					&lt;td&gt;&lt;div align=\&quote;center\&quote;&gt;&lt;font color=\&quote;#CCCCCC\&quote;&gt;&lt;strong&gt;Please Fix the Following 
       					Errors&lt;/strong&gt;&lt;/font&gt;&lt;/div&gt;&lt;/td&gt;
  						&lt;/tr&gt;
  						&lt;tr bgcolor=\&quote;#990000\&quote;&gt; 
    					&lt;td bgcolor=\&quote;#333333\&quote;&gt;&lt;ul&gt;&lt;li&gt;&quote;;
				foreach($errors as $fix =&gt; $value){
					$fixerrors = implode(&quote;&lt;/li&gt;&lt;li&gt;&quote;,$value);
					print $fixerrors;
				}
  				print &quote;&lt;/ul&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quote;;
				
				print_r($errors);
			}else{
				$timeadd = time();
				//mysql_query(&quote;INSERT INTO farms (id, username, topcommander, defenceaction, armysize, statid, comments, author, time) VALUES(NULL, '$ausername', '$atopcommander', '$adefenceaction', '$aarmysize', '$astatid', '$acomments', '$author', '$timeadd')&quote;) or die(mysql_error());
				//mysql_query(&quote;UPDATE users SET farmcount = farmcount + 1 WHERE user = '$username' LIMIT 1&quote;) or die(mysql_error());
				print &quote;Farm Was Added To Database&quote;;
			}
Ok, I'm getting the following error:

Warning: implode(): Bad arguments. in /home/nokiddin/public_html/ffinder/add2.php on line 137

Posted: Sun May 08, 2005 10:22 am
by John Cartwright
You need to pass an array to implode. What you are doing is passing an array element.

Change

Code: Select all

foreach($errors as $fix => $value){
     $fixerrors = implode("</li><li>",$value);                    
     print $fixerrors;                
}
to just

Code: Select all

print implode('</li><li>',$errors);

Posted: Sun May 08, 2005 10:54 am
by anthony88guy
Ahhh, that would explain it.

So now, When an error returns, it returns fine. But when the user enters/fixes the data, it doesnt check for error any more. I tried unset($errors). Any ideas?