<?php  
    $leave="";
    if(isset($_POST['submit']))
    {
        function clean($str) {
    		$str = @trim($str);
    		if(get_magic_quotes_gpc()) {
    			$str = stripslashes($str);
    		}
    		return mysql_real_escape_string($str);
    	}
        $leave = clean($_POST['leave']);
        
        if(!$leave){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Enter Leave</span>
                </div>';
        }else 
        {
            if(!$select = mysql_query("SELECT * FROM school_leave_type WHERE name = '$leave'")){
                    echo'<div class="alert alert-danger absolue center text-center" role="alert">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">×</span>
                            </button>
                            <span class="text-danger">'.mysql_error().'</span>
                        </div>';
            }else{
                $countrow=  mysql_num_rows($select);
                if($countrow > 0){
                    echo'<div class="alert alert-danger absolue center text-center" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <span class="text-danger">Error: Leave Type already exists!</span>
                            </div>';
                }else
                {
                    //generate course code
                    if(!$insert = mysql_query("INSERT INTO school_leave_type (name,added,added_by)VALUES('$leave',NOW(),'$stafNo')")){
                        echo'<div class="alert alert-danger absolue center text-center" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <span class="text-danger">'.mysql_error().'</span>
                            </div>';
                    }else
                    {
                        $leave="";
                        $_SESSION['success']="true";
                        header('Location:add-leave.php');
                        echo "<script type='text/javascript'> document.location = 'add-leave.php'; </script>";
                        exit();
                    }
                }    
            }
        }
       
    }  
?>