Wednesday, 17 August 2011

Important tips about webserver.

Command to calling a php file during Setting a cron job on Cpanel

lynx -source http://www.yourdomainname.com/crons/cron-jobs.php

Saturday, 13 August 2011

Ajax request form

function submit_register(form)
{
    var strSubmit       = '';
    var formElem;
    for (i = 0; i < form.elements.length; i++) {
        formElem = form.elements[i];
        switch (formElem.type) {
                case 'text':
                case 'select-one':
                case 'hidden':
                case 'password':
                case 'textarea': strSubmit += formElem.name + '=' + escape(formElem.value) + '&'; break;
        }
    }
    
    ajaxReq.open('POST',  http+'/cart/register-data-page.php?req='+Math.random(), true);
    ajaxReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajaxReq.onreadystatechange = function() {
            if (ajaxReq.readyState == 4) {
                  alert(ajaxReq.responseText);
            }
    }
    ajaxReq.send(strSubmit);
    return false;}

Friday, 12 August 2011

General Javascript functions

Clear the content of an div or span after a specific time:


function clrDiv(div2clr){
document.getElementById(div2clr).innerHTML = "";

}


Input: div or span id from which content has to be removed

Usage Example:
var resultDiv = 'mydivID';
var clr = setTimeout("clrDiv('"+resultDiv+"')", 2000);

 Insert Text at Cursor

function addText( input, insText ) {
        input.focus();
        if( input.createTextRange ) {
            document.selection.createRange().text += insText;
        } else if( input.setSelectionRange ) {
            var len = input.selectionEnd;
            input.value = input.value.substr( 0, len )
            + insText + input.value.substr( len );
            input.setSelectionRange(len+insText.length,len+insText.length);
        } else { input.value += insText; }
    }
 
 
PHP Equivalent in_array function in javascript: 

Array.prototype.in_array = function(p_val) {
 for(var i = 0, l = this.length; i < l; i++) {
  if(this[i] == p_val) {
   return true;
  }
 }
 return false;
}

Usage:

var weekdays ="sunday,monday,tuesday,wednesday,thursday,friday,saturday";
weekDayArr = weekdays.split(",");
 
if(weekDayArr.in_array('sunday'))
{
   alert("Sunday found");
}else{
 alert("sunday not found");
}
 
 
 
// Deafault input text swap



    function clearbox(box,defaulttxt) {





        if(defaulttxt.toLowerCase() == box.value.toLowerCase()) {

            box.value = "";

        }



        box.onblur = function() { unclearbox(box,defaulttxt);}



    }



    function unclearbox(box,defaulttxt) {

        if(box.value == "") {

        box.value = defaulttxt;

        }

    }
 

Select a drop down value using like search

<form name="myform" action="" method="POST">
<div>
    <select name="country">
        <option value="india_91">India</option>
        <option value="australia_23">Australia</option>
        <option value="china_56">China</option>
    </select>
</div>
<div>
    <input type="text" value="" onblur="setSelectedIndex(country, this.value)">
</div>
</form>
<script language="javascript">
    
function setSelectedIndex(s, v) {
    for ( var i = 0; i < s.options.length; i++ ) {
        var found = s.options[i].value.indexOf(v);
        if(found != "-1") {
           s.options[i].selected = true;
         return;
        }
    }
}

</script>


SMS counter with block and text counter:


function smsTextCounter(fieldID,resultID,maxlimit) {
  var smsBlock;
  var currentLength = document.getElementById(fieldID).value.length;
  smsBlock = Math.ceil(currentLength/maxlimit);
  document.getElementById(resultID).innerHTML = (maxlimit*smsBlock) - currentLength+"/"+smsBlock;
  if(currentLength==""){
      document.getElementById(resultID).innerHTML = maxlimit+"/"+1;
  }
}

Usage:
<tr class="form-row-1">
        <td width="40%" align="right" valign="middle" class="form-txt-no-border" <?php echo $tbl_row_hght;?>>Message:</td>
        <td width="60%" align="left" valign="middle">
        <textarea name="message" id="message" class="input" rows="10" cols="50" onkeyup= smsTextCounter('message','msgCounter','160') ></textarea></td>
      </tr>
      <tr>
      <td align="right" valign="middle" <?php echo $tbl_row_hght;?>></td>
      <td align="left" valign="middle" id="msgCounter" class="form-txt-no-border">160/1</td>
      </tr>

Inserting serial number in a table list:

function addSerial(tableID,serialStartFrom) {
    
    var rows = document.getElementById(tableID).rows, // get the rows in the table
    len = rows.length,    // get the quantity of rows
    cell = 1,             // index of the column (zero based index)
    count = serialStartFrom;            // keep score

    for(var i=0;i<len; i++){
       rows[i].cells[cell].innerHTML=count;
        count++;
       
    }    
}
setInterval("addSerial('voiceFiles',1)",500);


Giving alternate color to table listing:

function alternate(id){
  if(document.getElementsByTagName){
    var table = document.getElementById(id);
    var rows = table.getElementsByTagName("tr");
    var rowCounter=0;
    for(i = 0; i < rows.length; i++){
       if(rows[i].style.display !="none"){
      if(rowCounter % 2 == 0){
       rows[i].className = "form-row";
       }else{
       rows[i].className = "form-row-1";
            }
            rowCounter++;
          }
        }
         }
        }

usage:
document.onload=alternate("tableID");





Form validation in javascript

To check whether a radio button is checked or not:


function is_radio_checked(theForm,fieldname){
    var n=0;
    var rads =theForm.elements[fieldname];
    var valuee="";
    for(var i=0;i<rads.length;i++){
      if(rads[i].checked==true){
         valuee=rads[i].value;
         n=n+1;
      }
    }
    if(n==0){
      return false;
    }else{
      return selectedSingleValue;
    }
}

Input: Form name and name of radio field
Output: It will return the selected radio value and false if no radio is selected
Usage Example:

function checkform(theForm){
    if(is_radio_checked(theForm,'invoice_payment_statusID')==false){
        alert("Please select at least one option from status.");
        return false;
    }
}

=========== Importing value from pop up window to parent window===
function importAddress()
{


    var flag=0;
    val=new Array();
    var c=0;
    for (var i=0; i < document.form1.elements.length; i++)
    {

        if(document.form1.elements[i].name=='referID[]' && document.form1.elements[i].checked==true){
            flag=1;

            val[c]=document.form1.elements[i].value;
            c++;

        }

    }

    if(flag==0){
        window.close();
    }else{
        var selvalue =  val.join(",");
        window.opener.document.getElementById('recepient').value = selvalue;
        window.close();
    }

}

Monday, 8 August 2011

Javascript trim functions

Javascript Trim LTrim and RTrim Functions

Description

This set of Javascript functions trim or remove whitespace from the ends of strings. These functions can be stand-alone or attached as methods of the String object. They can left trim, right trim, or trim from both sides of the string. Rather than using a clumsy loop, they use simple, elegant regular expressions. The functions are granted to the public domain.

Javascript Trim Member Functions

Use the code below to make trim a method of all Strings. These are useful to place in a global Javascript file included by all your pages.

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+myString.trim()+"*");
alert("*"+myString.ltrim()+"*");
alert("*"+myString.rtrim()+"*");

Javascript Trim Stand-Alone Functions

If you prefer not to modify the string prototype, then you can use the stand-alone functions below.

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+trim(myString)+"*");
alert("*"+ltrim(myString)+"*");
alert("*"+rtrim(myString)+"*");

Example

Type in the box below and you will see the trimmed output dynamically. You must enter spaces before and after the string to see a difference. All whitespace is removed, including tabs and line feeds.
-----------------------------------------------------------------------------------

Compatibility

The functions above use regular expressions, which are compatible with Javascript 1.2+ or JScript 3.0+. All modern (version 4+) browsers will support this. If you require functions for older versions of Javascript back to version 1.0, try the functions below adapted from the Javascript FAQ 4.16. These strip the following, standard whitespace characters: space, tab, line feed, carriage return, and form feed. The IsWhitespace function checks if a character is whitespace.

function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}