Sunday, December 9, 2012

Target IE6 and IE7 with only 1 extra character in your CSS


I got it just now while googling

#myelement   {
    color: #999; /* shows in all browsers */
    *color: #999; /* notice the * before the property - shows in IE7 and below */
    _color: #999; /* notice the _ before the property - shows in IE6 and below */
}

Saturday, November 10, 2012

JavaScript Form Validation : quick and easy!


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example for Validator</title>
<style type="text/css" xml:space="preserve">
BODY, P, TD { font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 10pt }
A { font-family: Arial, Verdana, Helvetica, sans-serif; }
B { font-family : Arial, Helvetica, sans-serif; font-size : 12px; font-weight : bold; }
.error_strings { font-family:Verdana; font-size:10px; color:#660000; }
</style>
<script language="JavaScript" src="gen_validatorv4.js" type="text/javascript" xml:space="preserve"></script>
</head>
<body>
<form action="" name="myform" id="myform" method="post">
  <table cellspacing="2" cellpadding="2" border="0">
    <tr>
      <td align="right"> First Name </td>
      <td><input type="text" name="FirstName" /></td>
    </tr>
    <tr>
      <td align="right"> Last Name </td>
      <td><input type="text" name="LastName" /></td>
    </tr>
    <tr>
      <td align="right"> EMail </td>
      <td><input type="text" name="Email" /></td>
    </tr>
    <tr>
      <td align="right"> Phone </td>
      <td><input type="text" name="Phone" /></td>
    </tr>
    <tr>
      <td align="right"> Address </td>
      <td><textarea cols="20" rows="5" name="Address"></textarea></td>
    </tr>
    <tr>
      <td align="right"> Country </td>
      <td><select name="Country">
          <option value="" selected="selected"> [choose yours] </option>
          <option value="008"> Albania </option>
          <option value="012"> Algeria </option>
          <option value="016"> American Samoa </option>
          <option value="020"> Andorra </option>
          <option value="024"> Angola </option>
          <option value="660"> Anguilla </option>
          <option value="010"> Antarctica </option>
          <option value="028"> Antigua And Barbuda </option>
          <option value="032"> Argentina </option>
          <option value="051"> Armenia </option>
          <option value="533"> Aruba </option>
        </select></td>
    </tr>
    <tr>
      <td align="right"></td>
      <td><div id="myform_errorloc" class="error_strings"> </div></td>
    </tr>
    <tr>
      <td align="right"></td>
      <td><input type="submit" value="Submit" /></td>
    </tr>
  </table>
</form>
<script language="JavaScript" type="text/javascript" xml:space="preserve">//<![CDATA[
//You should create the validator only after the definition of the HTML form
  var frmvalidator  = new Validator("myform");
 frmvalidator.EnableOnPageErrorDisplaySingleBox();
 frmvalidator.EnableMsgsTogether();

 frmvalidator.addValidation("FirstName","req","Please enter your First Name");
  frmvalidator.addValidation("FirstName","maxlen=20", "Max length for FirstName is 20");
  frmvalidator.addValidation("FirstName","alpha_s","Name can contain alphabetic chars only");
 
  frmvalidator.addValidation("LastName","req","Please enter your Last Name");
  frmvalidator.addValidation("LastName","maxlen=20","For LastName, Max length is 20");
 
  frmvalidator.addValidation("Email","maxlen=50");
  frmvalidator.addValidation("Email","req");
  frmvalidator.addValidation("Email","email");
 
  frmvalidator.addValidation("Phone","maxlen=50");
  frmvalidator.addValidation("Phone","numeric");
 
  frmvalidator.addValidation("Address","maxlen=50");
  frmvalidator.addValidation("Country","dontselect=0");
 
    function DoCustomValidation()
    {
        var frm = document.forms["myform"];
        if(frm.FirstName.value == 'Bob')
        {
            sfm_show_error_msg("Bob, you can't submit this form. Go away! ");
            return false;
        }
        else
        {
            return true;
        }
    }

  frmvalidator.setAddnlValidationFunction(DoCustomValidation);
//]]></script>
</body>
</html>

Tuesday, October 23, 2012

Class for change date into Arabic format


<?php
class dateTime {

function __construct() {
}
    /*
    function changetoArabic($date)
    */
    function changetoArabic($date) {
        $date_array = explode("/", $date);
        if(!$date_array['1']) {
$date_array = explode("-", $date);$superator = '-';
}
else $superator = '/';
        $d = $date_array['0'];
        $m = $date_array['1'];
        $y = $date_array['2'];
        if(is_numeric($d) && is_numeric($m) && is_numeric($y)) {
            $num['0']='٠';
            $num['1']='١';
            $num['2']='٢‎';
            $num['3']='٣‎';
            $num['4']='٤';
            $num['5']='٥';
            $num['6']='٦‎';
            $num['7']='٧';
            $num['8']='٨';
            $num['9']='٩';
            $num['/']='/';
            $num['-']='-';
$element = str_split($date);
$d_ar = '';
if(is_array($element)) {
foreach ($element as $k=>$v)    {
$d_ar .= $num[$v];
}
}else $d_ar .= $num['0'];
            return $d_ar;
        }
        else {
            return "Error:Wrong Format";
        }
    }
}
?>

Monday, October 1, 2012

PHP Mail sending code with HTML Body to multiple recipients


<?php
// multiple recipients
//$to  = 'satheesh@iv.bh' . ', '; // note the comma
//$to .= 'satheesh.tasks@gmail.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>'.$subject.'</title>
</head>
<body>
<p>Hi, <br>Please find the login credentials</p>
<p>Username : <br> Password : </p>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$to1 = 'Satheesh <satheesh.tasks@gmail.com>';
$to2 = 'Satheesh <satheesh@iv.bh>';
$headers .= 'To: '.$to1.', '.$to2.'' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
//$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
// Mail it
if(mail($to, $subject, $message, $headers))
echo "Mail sent";
else echo "Can't send";
?>

Monday, September 24, 2012

Creating XML file using DOMDocument() object in PHP



<?php
$employees = array();
$employees [] = array(
'name' => 'Albert',
'age' => '34',
'salary' => "$10000"
);
$employees [] = array(
'name' => 'Claud',
'age' => '20',
'salary' => "$2000"
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "employees" );
$doc->appendChild( $r );
foreach( $employees as $employee ) {
$b = $doc->createElement( "employee" );
$name = $doc->createElement( "name" );
$name->appendChild(
$doc->createTextNode( $employee['name'] )
);
$b->appendChild( $name );
$age = $doc->createElement( "age" );
$age->appendChild(
$doc->createTextNode( $employee['age'] )
);
$b->appendChild( $age );
$salary = $doc->createElement( "salary" );
$salary->appendChild(
$doc->createTextNode( $employee['salary'] )
);
$b->appendChild( $salary );
$r->appendChild( $b );
}
echo $doc->saveXML();
$doc->save("write.xml")
?>
Printing XML file

<?php
$doc = new DOMDocument();
$doc->load( 'write.xml' );
$employees = $doc->getElementsByTagName( "employee" );
foreach( $employees as $employee ) {
$names = $employee->getElementsByTagName( "name" );
$name = $names->item(0)->nodeValue;
$ages= $employee->getElementsByTagName( "age" );
$age= $ages->item(0)->nodeValue;
$salaries = $employee->getElementsByTagName( "salary" );
$salary = $salaries->item(0)->nodeValue;
echo "<b>$name - $age - $salary\n</b><br>";
}
?>

Thursday, September 20, 2012

Login form in fancy box: Close and Refresh the parent page after submit the login form


*****************************************************
HTML Page
*****************************************************
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Testing Page</title>
    <script type="text/javascript" src="scripts/jquery-1.6.2.min.js"></script>
// stored in https://docs.google.com/open?id=0B3t3ir6gJardd21lOGFlMmZ3QVE
    <link rel="stylesheet" href="fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
// stored in https://docs.google.com/open?id=0B3t3ir6gJardd21lOGFlMmZ3QVE
    <script type="text/javascript">
    $(document).ready(function() {
        $(".login-window").fancybox({
            'width'             : 300,
            'height'             : 180,
            'autoScale'       : false,
            'transitionIn'    : 'none',
            'transitionOut' : 'none',
            'type'               : 'iframe',
            'onClosed'        : function() {  
                            parent.location.reload(true);
                            ;}
        });
    });

</head>
<body>
</body>
<a href="fancy_login.php?" class ="login-window">Login</a>
</html>

*****************************************************
fancy_log.php
*****************************************************
<?php
if($_POST)  {
    $close_window = TRUE;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="scripts/jquery-1.6.2.min.js"></script>
// stored in https://docs.google.com/open?id=0B3t3ir6gJardd21lOGFlMmZ3QVE
<script type="text/javascript" src="fancybox/jquery.fancybox-1.3.4.pack.js"></script>// stored in https://docs.google.com/open?id=0B3t3ir6gJardd21lOGFlMmZ3QVE
<?php
if($close_window)  {
echo '<script type="text/javascript">
            $(function () {
                parent.$.fancybox.close();
            });
        </script>';
}
?>
<title> Login</title>
</head>

<body>
<form name="loginform" action="" method="POST">
    <table align="center">
        <tr>
            <th colspan="2">Login</th>
        </tr>
        <tr>
            <td>Username</td>
            <td><input type="text" name="username" />
            </td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="password" /></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="login" value="Login" /></td>
        </tr>
    </table>
</form>
</body>
</html>

Wednesday, September 12, 2012

PHP class for simple pagination


<?php
class Pagination    {
    function __coustruct()  {
        parent::__construct();
    }

    function showPagination($query, $page = '1', $per_page = '10')   {
        $total = 0;
        $rst = $this->selectData($query);
        $total = mysql_num_rows($rst);
        $page = ($page == 0 ? 1 : $page);
        $start = ($page - 1) * $per_page;
        $prev = $page - 1;
        $next = $page + 1;
        $lastpage = ceil($total/$per_page);
        $lpm1 = $lastpage - 1;
        $pagination = '';
        $linktitle = 'Goto ';
        $page_url = $_SERVER['PHP_SELF'];
        if($lastpage > 1)   {
            $first  = '<<';
            $prev   = '<';
            $next   = '>';
            $last   = '>>';
            $pagination .= '<ul class="pagination">';
                $link = ($page != '1')?$page_url.'?p=1': '#';
                $pagination.= '<li><a href="'.$link.'" title="'.$linktitle.'First Page">'.$first.'</a></li>';
                if($link != '#')$link = $page_url.'?p='.((int)$page-1);
                $pagination.= '<li><a href="'.$link.'" title="'.$linktitle.'Previous Page">'.$prev.'</a></li>';
                for ($counter = 1; $counter <= $lastpage; $counter++)   {
                    if ($counter == $page)
                        $pagination.= '<li><a class="current">'.$counter.'</a></li>';
                    else
                        $pagination.= '<li><a href="'.$page_url.'?p='.$counter.'">'.$counter.'</a></li>';
                }
                $linklast = ($page != $lastpage)?$page_url.'?p='.$lastpage: '#';
                $link = ($linklast != '#')? $page_url.'?p='.((int)$page+1):'#';
                $pagination.= '<li><a href="'.$link.'" title="'.$linktitle.'Next Page">'.$next.'</a></li>';
                $pagination.= '<li><a href="'.$linklast.'" title="'.$linktitle.'Last Page">'.$last.'</a></li>';
                $pagination .= '<li class="details">Page <span>'.$page.'</span> of <span>'.$lastpage.'</span></li>';
                $pagination .= '<div class="clear"></div></ul>';
         
        }
        return $pagination;
    }
    function selectData($query)   {
        mysql_connect('localhost', 'root', 'password') or mysql_error();
        mysql_select_db('database') or mysql_error();
        $result = mysql_query($query) ;
        return ($result and mysql_num_rows($result))? $result : false;
    }
}
?>

Monday, September 10, 2012

PHP Class: Mail sending with attachment


<?php
ini_set('error_reporting', E_ALL);
if($_POST['send_mail']) {
if($_POST['email']) {
function sendmsg($email_to, $email_subject, $msgtext, $email_from, $email_replyto, $file, $type){
require_once("htmlMimeMail.php");
$filename = basename($file);
$mail  = new htmlMimeMail();
$mail->setFrom("$email_from");
$mail->setReturnPath("$email_replyto");
$mail->setSubject("$email_subject");
$mail->setHTML("$msgtext");
$mail->addAttachment($mail->getFile("$file"), "$filename", 'application/pdf');
$result = $mail->send(array("$email_to"));
return $result ? 'Mail sent successfully!' :  'Sorry! Cannot send the mail. Try later!';
}
$fileatt = "test.pdf";
$subject = 'Sending Mail using HTMLMimeMail';
$to = $_POST['email'];
$email_from = 'satheesh@iv.bh';
$email_replyto = 'satheesh@iv.bh';
$type = 'application/pdf';
$ok = sendmsg($to, $subject, $msg,$email_from,$email_replyto, $fileatt, $type);
              echo $ok;
}
else echo 'Please enter Email';
}else echo 'Please enter Email';
?>
<form action="" method="POST">
<input type="text" name = "email"><br>
<input type="submit" value="Send Mail" name="send_mail">
</form>

Tuesday, September 4, 2012

Function for shorten long text without partial words


 function shortenText($text, $length = '')    {
// $normal_text = strip_tags(html_entity_decode($text, ENT_QUOTES));
$max = $length;
$normal_text = strip_tags($text, ENT_QUOTES);
$tot_len = strlen($normal_text);
if(!$length)$length = $tot_len;
if($length < $tot_len)   {
while(substr(strip_tags($text, ENT_QUOTES), $max, 1) != ' ' && $max > 0 )  {
$max--;
}
if($max == 0)$max = $length;
}
$output = substr($normal_text, 0, $max);
if($length < $tot_len)$output .= '<span dir="ltr">...<span>';
return $output;
}