Jump to content

change font color if success or not


damion

Recommended Posts

Is there a simple bit of code that I can use to show for example green font "Success" or red font "Fail" on a form?

I have several forms in my application that has file uploads, create user, create folder, and so on. I'm hoping there is a simple few lines I can add to these forms without being too intrusive. I'm wondering maybe the easiest would be if the data is stored in the database show green, if not show red?

 

 

 

Link to comment
Share on other sites

Without seeing how your code is set up I can't really offer a concrete solution, but I can offer some advice. For example, you can make two CSS classes (success and failure) which will change the text color to green or red respectively. Then its as simple as adding said CSS class on success or failure.

Link to comment
Share on other sites

Thanks mikesta707. Here is the code on the page that allows the admin to create folders. I hope this is enough and then I can hopefully understand what you did enough to apply it to my other forms.

 

<?php



session_start();
    require_once("includes/dbconnect.php"); //Load the settings
require_once("includes/functions.php"); //Load the functions
$msg="";

if($_SESSION["logged_in"]!=true){ 
header("Location: index.php");
} else {
//get access level
if(isset($_SESSION["accesslevel"])){
	$access = $_SESSION["accesslevel"];
	//determin admin or not.
	if(stristr($access,"abcdef")){ $level="admin"; }else{ $level="user"; }
} else { 
header("Location: index.php");
}


//paging settings
// how many rows to show per page
$rowsPerPage = 14;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}
$offset = ($pageNum - 1) * $rowsPerPage;
//CREATE PAGING LINKS
// how many rows we have in database
$query   = "SELECT COUNT(id) AS numrows FROM folders WHERE '1' ";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav  = '';
for($page = 1; $page <= $maxPage; $page++)
{
   if ($page == $pageNum){
	  $nav .= " $page "; // no need to create a link to current page
   }  else   {
	  $nav .= " <a href=\"$self?page=$page\">$page</a> ";
   }
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1){
   $page  = $pageNum - 1;
   $prev  = " <a href=\"$self?page=$page\">Prev</a> ";
   $first = " <a href=\"$self?page=1\">1st Page</a> ";
} else {
   $prev  = ' '; // we're on page one, don't print previous link
   $first = ' '; // nor the first page link
}

if ($pageNum < $maxPage){
   $page = $pageNum + 1;
   $next = " <a href=\"$self?page=$page\">Next</a> ";
   $last = " <a href=\"$self?page=$maxPage\">Last</a> ";
} else {
   $next = ' '; // we're on the last page, don't print next link
   $last = ' '; // nor the last page link
}

//show page only if admin access level
if($level=="admin"){
//request all neccessary variables for extensions action.
$name = (!empty($_REQUEST["name"]))?strip_tags(str_replace("'","`",$_REQUEST["name"])):'';


//"create new folder" action processing.
if(!empty($_REQUEST["add_folder"]) && $_REQUEST["add_folder"]=="yes" && !empty($name)){
	//check for existing user in DB.
	$name = strtolower(trim($name));		
	$sql="SELECT * FROM folders WHERE name='".$name."'";
	$result=mysql_query($sql) or die("error selecting folder from database for comparison");
	if(mysql_num_rows($result)>0){
	$msg = "Folder already exists in database. Try another one.";
	} else { 

		if(!empty($name)){		

		//also create mkdir folder in script base, and make it writable
		$thisdir = getcwd();
		@mkdir($thisdir ."/".$name , 0777);
		//chmod($script_dir.$name, 777);

		$sql="INSERT INTO folders (dateCreated,name) VALUES (NOW(),'".$name."')";
		$result=mysql_query($sql) or die("error occured when tryin to insert new folder.");
		$msg = "Folder was successfully added!";
		addLog($_SESSION["idUser"],"Added new folder $name");
		//}
		$name="";

		}
	}

}

//"delete selected folders" action processing.
if(!empty($_REQUEST["folders_delete"]) && $_REQUEST["folders_delete"]=="yes"){
	if(is_array($_POST['extToDel'])){
		if(join(",", $_POST['extToDel'])!='') {

			//update all users who currently has folder which we are going to delete.
			for($i=0;$i<count($_POST["extToDel"]);$i++){
				if($_POST["extToDel"][$i]!="uploads"){
				$query="UPDATE users SET upload_dir='uploads' WHERE upload_dir='".$rr["name"]."' ";
				$result=mysql_query($query);
				//remove folder from server if it is empty
				if (!is_dir($script_dir.$_POST["extToDel"][$i])) {
					$thisdir = getcwd();
					if(@rmdir($thisdir."/".$_POST["extToDel"][$i])){
						$msg .= "Directory '".$script_dir.$_POST["extToDel"][$i]."' was successfully deleted.<br/>";
						$sql="DELETE  FROM folders WHERE name='".$_POST['extToDel'][$i]."'";
						$result=mysql_query($sql) or die("error when tryin to delete folders");
						addLog($_SESSION["idUser"],"Deleted ".$script_dir.$_POST['extToDel'][$i]." folder.");
					} else { 
						$msg .= "Directory '".$script_dir.$_POST["extToDel"][$i]."' is not empty! Please delete files associated with this directory first.<br/>";
					}
				}

			}		


		}
	} 
}
}


//CREATE folders TABLE
  $folder_table="";
  $bgClass="even";
  	  $sql="SELECT * FROM folders ORDER BY name ASC LIMIT ".$offset.", ".$rowsPerPage;
  $result=mysql_query($sql) or die("error getting folders from db");
  if(mysql_num_rows($result)>0){
	  	while($rr=mysql_fetch_assoc($result)){
		$bgClass=($bgClass=="even"?"odd":"even");

  		$folder_table.="<tr class=\"".$bgClass."\">";

		if($rr["id"]!="1"){
		$folder_table.="<td><input name=\"extToDel[]\" type=\"checkbox\" value=\"".$rr["name"]."\" /></td>";
		} else { 
		$folder_table.="<td> </td>";
		}

		$folder_table.="<td height=\"24\">".$rr["name"]."</td>";		
		$folder_table.="<td>".date("d M Y, H:i",strtotime($rr["dateCreated"]))."</td>";
		$folder_table.="<td> </td>";		
  		$folder_table.="</tr>";
   		} 
 		$folder_table.="<tr>";
		$folder_table.="<td height=\"32\" colspan=\"7\"><input name=\"delete_users\" type=\"submit\" value=\"Delete Selected\"  /></td>";
  		$folder_table.="</tr>";
   } else { 
 	 	$folder_table.="<tr><td colspan=\"7\">0 folders found in database</td></tr>";
   }
   
?>

 

 

The form:

 

  
<div class="form_block">
  <h2>Add New Folder</h2>
  <strong><?php echo $msg; ?></strong>
  <form action="folders.php" enctype="multipart/form-data" method="post" name="ff1">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="152" height="25" align="right">Folder Name:</td>
    <td width="632" height="23"><input name="name" type="text" id="name" size="10"  /> </td>
  </tr>
  <tr>
    <td height="25" align="right"> </td>
    <td height="32">
      <input type="submit" name="create" id="create" value="Add Folder" />
      <input value="yes" name="add_folder" type="hidden" />
   </td>
  </tr>
</table>
</form>
</div>

Link to comment
Share on other sites

Hi Damion, with code we mean relevant code. The above is a complete script one needs to read through to find some clause which in your case needs to output succes or failure. Since I rather narrow it down to a more concrete example here is something you can do with the thing mentioned by Mikesta which is assigning a class rather than in line style after you tested the value.

 

So here goes:

the php:

$variable_to_test = 'monkeys';

//test the value
if ($variable_to_test == 'monkeys'){ // if equal to
    echo '<span class="valid">the value is indeed equal to monkeys</span>';
    
}else{// so in case it's not equal to 
    
    echo '<span class="invalid">the value is not equal to monkeys</span>';
}

 

Than in an external stylesheet you must create 2 classes valid and invalid

span.valid{
   color:green;
   font-weight:bold;
}
span.invalid{
  color:red;
  font-weight:bold;
}

 

By doing this you separate style and logic which will save you loads of time in the end. If you have questions let me know.

Link to comment
Share on other sites

Hi cssfreakie. Yes, I totally understood what Mikesta explained about the styling of the text. For me that's the easy part, and apologies if that was confused with what I really needed - the PHP code.

Although I appreciate your help, my situation does not test for a static value. So I need to somehow test what already exists in the database.

 

And sorry for all the superfluous code. I'm a little hesitant to assume that I know enough to trim down to the relevant code, but I will try here if it helps to get a solution.

 

	

<?php

//show page only if admin access level
if($level=="admin"){
//request all neccessary variables for extensions action.
$name = (!empty($_REQUEST["name"]))?strip_tags(str_replace("'","`",$_REQUEST["name"])):'';


//"create new folder" action processing.
if(!empty($_REQUEST["add_folder"]) && $_REQUEST["add_folder"]=="yes" && !empty($name)){
	//check for existing user in DB.
	$name = strtolower(trim($name));		
	$sql="SELECT * FROM folders WHERE name='".$name."'";
	$result=mysql_query($sql) or die("error selecting folder from database for comparison");
	if(mysql_num_rows($result)>0){
	$msg = "Folder already exists in database. Try another one.";
	} else { 

		if(!empty($name)){		

		//also create mkdir folder in script base, and make it writable
		$thisdir = getcwd();
		@mkdir($thisdir ."/".$name , 0777);
		//chmod($script_dir.$name, 777);

		$sql="INSERT INTO folders (dateCreated,name) VALUES (NOW(),'".$name."')";
		$result=mysql_query($sql) or die("error occured when tryin to insert new folder.");
		$msg = "Folder was successfully added!";
		addLog($_SESSION["idUser"],"Added new folder $name");
		//}
		$name="";

		}
	}
?>

 

I hope that helps, thanks.

Link to comment
Share on other sites

I understand, but it was an example, the logic stays the same.

 

May I assume that the value you want to output is $msg?

if that is the case this should do the trick.

 

//for the invalid message
$msg = '<span class="invalid">Folder already exists in database. Try another one.</span>';

//and for the valid message
$msg = '<span class="valid">Folder was successfully added!</span>';

 

Note i am using single quotes around the strings.

 

May I also assume you know how to add those classes in a stylesheet? if not let me know

Link to comment
Share on other sites

Wow, I totally blew that one. I did not see the two $msg variables in the code where I could add the style rules to each case. Now I see why you guys were pushing the CSS :)

Thank you for your help!

 

Time to go back to sleep..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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