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>

Sunday, August 26, 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;
    }