Jump to content

foreach errors


graham23s

Recommended Posts

Hi Guys,

 

i can't figure out the best way to do this, this piece of code:

 

} elseif ($_GET['action'] == 'add-url') {

// Deal with post data
if (isset($_POST['submit']))
{
  // post vars
  $var_url = $_POST['url'];
  $var_des = $_POST['desc'];
  
  // initialise array
  $errors = array();
  
  // empty fields 
  if (empty($var_url))
  {
   $errors[] = "You never entered a URL.";
  }
  
  // empty fields 
  if (empty($var_des))
  {
   $errors[] = "You never entered a Brief description of your site.";
  }
  
  // show any errors found
  if (!isset($errors))
  {
  
   // NO ERRORS
   print("THANK YOU!!!!!!!");
   
   } else {
  
   print("<div id=\"bullet_box\">");
   print("<ul class=\"bullets\">");

   foreach($errors as $error)
   {
    print("<li>$error");
   }
   print("</ul>");   
   print("</div>");

   } 
  
} // end isset

// print form add url
print("<form action=\"\" method=\"POST\" name=\"submit_page\">\n");
print("<table width=\"500\" border=\"0\" cellpadding=\"5\" cellspacing=\"2\">\n");
print("<tr>\n");
print("<td align=\"left\" colspan=\"2\"><h2>Submit URL</h2></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td align=\"left\">URL:</td><td align=\"left\"><input type=\"text\" name=\"url\" size=\"40\"></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td align=\"left\">Brief Description:</td><td align=\"left\"><input type=\"text\" name=\"desc\" size=\"40\"></td>\n");
print("</tr>\n");
print("<tr>\n");
print("<td align=\"left\"><input type=\"submit\" name=\"submit\" value=\"Add URL\"></td><td align=\"left\"><span class=\"star\">*</span> We will crawl your site in 2-3 days!</td>\n");
print("</tr>\n");
print("</table>\n");
print("</form>\n");

} else {

include("results.php");

}
?>

if there are errors, i print a bullet list of the errors with a css border round it, works great untill there are no errors, then it just prints the bullet list with nothing in it! it doesn't seem to execute the no errors part!

its probably really simple to

thanks guys

Graham

 

 

Link to comment
https://forums.phpfreaks.com/topic/111509-foreach-errors/
Share on other sites

Hi,

 

You're setting the variable $errors to be an array then checking to see if it is set.

 

Leave the majority of the setup the way you have it just change the logic of the if statment to be

 

if(count($errors) > 0) {
//display error code
}
else {
//display thanks no errors code
}

Link to comment
https://forums.phpfreaks.com/topic/111509-foreach-errors/#findComment-572270
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.