Thursday, June 28, 2012

Popup Form with HTML, CSS, Java Script


Popup Form with HTML, CSS, Java Script
=============================


#lightbox{
    visibility:hidden;
    position:absolute;
    background:blue;
    border:2px solid #3c3c3c;
    color:white;
    z-index:200;
    width: 400px;
    height:300px;
    padding:40px;
}


.dimmer{
    background: #000;
    position: absolute;
    opacity: .5;
    top: 0;
    z-index:50;
}




Java Script
===========


var opener = document.getElementById("opener");


opener.onclick = function(){
    
    var lightbox = document.getElementById("lightbox"),
        dimmer = document.createElement("div");
    
    dimmer.style.width =  window.innerWidth + 'px';
    dimmer.style.height = window.innerHeight + 'px';
    dimmer.className = 'dimmer';
    
    dimmer.onclick = function(){
        document.body.removeChild(this);   
        lightbox.style.visibility = 'hidden';
    }
        
    document.body.appendChild(dimmer);
    
    lightbox.style.visibility = 'visible';
    lightbox.style.top = window.innerHeight/6 - 20 + 'px';
    lightbox.style.left = window.innerWidth/6 - 20 + 'px';
    return false;
}​




HTML 
====
<div id="lightbox">
 <form id='contactus' action='' method='post' accept-charset='UTF-8'>


    <div class='container'>
        <label for='name' >Your Full Name*: </label><br/>
        <input type='text' name='name' id='name' value='' maxlength="50" /><br/>
        
    </div>
    <div class='container'>
    <label for='email' >Email Address*:</label><br/>
        <input type='text' name='email' id='email' value='' maxlength="50" /><br/>
        
    </div>
    <div class='container'>
        <label for='message' >Message:</label><br/>
        
        <textarea rows="10" cols="50" name='message' id='message'></textarea>
    </div>
     <div class='container'>
        <input type='submit' name='Submit' value='Submit' />
    </div>
    </form>
   
</div>
<a href="#" id="opener">Click me</a>​




User for testing for HTML/CSS/JS
http://jsfiddle.net/rxGmk/43/

Monday, June 11, 2012

PHP - Automatic Copyright Year


PHP - Automatic Copyright Year


Current year only
&copy; <?php echo date("Y") ?>


With start year
&copy; 2008-<?php echo date("Y") ?>


Start date with error protection
<?php function auto_copyright($year = 'auto'){ ?>
   <?php if(intval($year) == 'auto'){ $year = date('Y'); } ?>
   <?php if(intval($year) == date('Y')){ echo intval($year); } ?>
   <?php if(intval($year) < date('Y')){ echo intval($year) . ' - ' . date('Y'); } ?>
   <?php if(intval($year) > date('Y')){ echo date('Y'); } ?>
<?php } ?>




Usage:
<?php auto_copyright(); // 2011?>


<?php auto_copyright('2010');  // 2010 - 2011 ?>

PHP - RSS Generator


PHP - RSS Generator
You'll need a MySQL database with a a table called `rss_feed`. In that table there are 3 colums:item title (which is the name a person will see for an item), the item link (which is the location of the page with the item on it), and description (which tells what the feed is about). Put the file in a folder called feed and you're feed will be available at yoursite.com/feed
Remember to change the feed title, link and image for your specific feed.

<?php

   // Connect to database... (you'll need to create this yourself)
   require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/connection.php';

   // Run query...
   $getFeed = mysql_query("SELECT *
                           FROM `rss_feed`
                           ORDER BY `time` DESC
                           ")or die(mysql_error());

   // Output XML (RSS)
    echo '<?xml version="1.0" encoding="ISO-8859-1" ?>
          <rss version="2.0">
                <channel>
                        <title>Your RSS Title</title>
                        <link>http://the_location_of_your_feed/feed</link>
                        <description>Description of your Feed</description>
                        <language>English</language>
                        <image>
                                <title>website Logo</title>
                                <url></url>
                                <link>Link to image</link>
                                <width>width</width>
                                <height>height</height>
                        </image>';
while($rssFeed = mysql_fetch_array($getFeed)) {
        echo '<item>',
             '<title>', $rssFeed['item_title']</title>',
             '<link>', $rssFeed['link'], '</link>',
             '<description><![CDATA[ ,$rssFeed['description'],']]></description>
  </item>';

  }
echo '</channel>
        </rss>';

?>

PHP - Quick Alphabetic Navigation


PHP - Quick Alphabetic Navigation


<?php
foreach (range('a', 'z') as $char) {
 print '<a href="#' . $char . '">' . $char . '</a> | ';
}
?>

PHP - Send Email


PHP - Send Email


HTML Form with Inputs


<form action="" method="post">
  <label for="name">Name:</label>
  <input type="text" name="name" id="name" />


  <label for="Email">Email:</label>
  <input type="text" name="email" id="email" />


  <label for="Message">Message:</label><br />
  <textarea name="message" rows="20" cols="20" id="message"></textarea>


  <input type="submit" name="submit" value="Submit" />
</form>






Process with PHP (sendemail.php)


<?php
       // from the form
       $name = trim(strip_tags($_POST['name']));
       $email = trim(strip_tags($_POST['email']));
       $message = htmlentities($_POST['message']);


       // set here
       $subject = "Contact form submitted!";
       $to = 'your@email.com';


       $body = <<<HTML
$message
HTML;


       $headers = "From: $email\r\n";
       $headers .= "Content-type: text/html\r\n";


       // send the email
       mail($to, $subject, $body, $headers);


       // redirect afterwords, if needed
       header('Location: thanks.html');
?>


Login Function


These functions will log in a user based on a username and password being matched in a MySQL database.


login($username, $password);




// function to escape data and strip tags
function safestrip($string){
       $string = strip_tags($string);
       $string = mysql_real_escape_string($string);
       return $string;
}


//function to show any messages
function messages() {
   $message = '';
   if($_SESSION['success'] != '') {
       $message = '<span class="success" id="message">'.$_SESSION['success'].'</span>';
       $_SESSION['success'] = '';
   }
   if($_SESSION['error'] != '') {
       $message = '<span class="error" id="message">'.$_SESSION['error'].'</span>';
       $_SESSION['error'] = '';
   }
   return $message;
}


// log user in function
function login($username, $password){


 //call safestrip function
 $user = safestrip($username);
 $pass = safestrip($password);


 //convert password to md5
 $pass = md5($pass);


  // check if the user id and password combination exist in database
  $sql = mysql_query("SELECT * FROM table WHERE username = '$user' AND password = '$pass'")or die(mysql_error());


  //if match is equal to 1 there is a match
  if (mysql_num_rows($sql) == 1) {


                          //set session
                          $_SESSION['authorized'] = true;


                          // reload the page
                         $_SESSION['success'] = 'Login Successful';
                         header('Location: ./index.php');
                         exit;


   } else {
               // login failed save error to a session
               $_SESSION['error'] = 'Sorry, wrong username or password';
  }
}




All pages involved would have the messages function somewhere so proper use feedback is given:


messages();

PHP - Email Address Validation


PHP - Email Address Validation


Simple validation


$email = 'mail@example.com';
$validation = filter_var($email, FILTER_VALIDATE_EMAIL);


if ( $validation ) $output = 'proper email address';
else $output = 'wrong email address';


echo $output;


Advanced validation


<?php
$email="test@geemail.com";
if (isValidEmail($email))
{
       echo "Hooray! Adress is correct.";
}
else
{
       echo "Sorry! No way.";
}


//Check-Function
function isValidEmail($email)
{
       //Perform a basic syntax-Check
       //If this check fails, there's no need to continue
       if(!filter_var($email, FILTER_VALIDATE_EMAIL))
       {
               return false;
       }


       //extract host
       list($user, $host) = explode("@", $email);
       //check, if host is accessible
       if (!checkdnsrr($host, "MX") && !checkdnsrr($host, "A"))
       {
               return false;
       }


       return true;
}
?>

PHP - Get Current File Name


PHP - Get Current File Name


<body id="body_<?php echo $pageName; ?>">


<?php
    $pageName = basename($_SERVER[PHP_SELF]);
?>