Sunday, December 1, 2013

Value of CKeditor using Javascript


To get the value of the CKeditor content using Javascript or jQuery. We need to use the below syntax.

CKEDITOR.instances['article_long_desc'].getData().length => It provides the length of the content in the editor

CKEDITOR.instances['article_long_desc'].getData() => It retrieves the value of the Textarea


Where "article_long_desc" is the textarea name


Monday, November 18, 2013

Random values from an Array using php

Below is the function to pick random values from an existing array

function array_pick_random($arr, $number = 1) {
    shuffle($arr);
   
    $r = array();
    for ($i = 0; $i < $number; $i++) {
        $r[] = $arr[$i];
    }
    return $number == 1 ? $r[0] : $r;
}

In the above function '$arr' is the Array which we want to get the random values.

'$number' is the number of array elements need to return.

Function returns an array if '$number' contains more than 1.

Sunday, October 20, 2013

ajax with cross domain


We may worked with jQuery ajax in many cases but when we try to use jQuery AJAX to get the data from other domain, we need to use "crossDomain" & set it to true.

Let us consider the basic jQuery AJAX within the domain.

$.ajax({
            url: "show_users.php",
            data: {data1:"val1", data2="val2"},
            type:'POST',
            success: function(res)
            {
                $('#element_name').html(res);
            },
            error: function (){alert('something went wrong');}
});

Above is the AJAX request within the domain & just we are passing the data to the page "show_users.php" with two values. As it is simple AJAX request, it works fine.

But when we work with cross domains (i.e. AJAX request to other domain from our domain) we need to rewrite the jQuery AJAX as below.

$.ajax({
            url: "http://domain.com/show_users.php",
            data: {data1:"val1", data2="val2"},
            type:'POST',
            crossDomain: true,
            success: function(res)
            {
                $('#element_name').html(res);
            },
            error: function (){alert('something went wrong');}
});

We need to use "crossDomain" & set it to TRUE & in the domain.com show_users.php page we need to set an header as below.

header('Access-Control-Allow-Origin: *');

We can set the "Access-Control-Allow-Origin" with only one IP address or '*' if it is not limited to single IP address

Sunday, October 6, 2013

Verify whether HTML object has the event using javascript or jQuery

We would be using multiple events or plugins to get our requirement done using jQuery.

While adding them we may not know whether the plugin is activated or not that gives error to the browser if it does not initiate and stops the next line of scripts in jQuery which we should not be doing. 

To get rid of this, we can use jQuery hasOwnProperty which tells whether the HTML object has the event which we are checking for.

Below is the sample code:

Consider we have included the CKEDITOR in the script and it applies for the textareas.


<html>
<head>
<script type="text/javascript" src="ckeditor.js">
<title>Test Object Event Existence</title>
</head>
<body>
<ul>
     <li>First name: <input type="text" name="fname" /> </li>
     <li>Last name: <input type="text" name="lname" /> </li>
     <li>Address: <textarea name="address" id="address" ></textarea></li>
</ul>
</body>
</html>
For the above script, to check whether the textareas has CKEDITOR or not we can check as below

$('#address').hasOwnProperty('CKEDITOR') 

If it exists it returns true else returns false.


Monday, July 29, 2013

Decode html entities using javascript

Below is the code to decode the html entities using javascript rather than the Server side scripting

function decodeEntities(input) {
    var y = document.createElement('textarea');
    y.innerHTML = input;
    return y.value;
}

Above function just receives the html values like below

var str = "<p>Hello World<br />This is the test message";

When we pass the above HTML string to the function it returns as below,

calling the JAVASCRIPT function 

decodeEntities(str)

Output is: "&lt;p&gt;Hello World&lt;br/&gt;This is the test message";

Thursday, July 18, 2013

Disable right click in a page using javascript


When we want to restrict the right click of the mouse in some of the pages, need to add the below code in the <head> </head> tag within the JAVASCRIPT.

<script type="text/javascript">
var message="Sorry, Right Click has been disabled";
        function clickIE() {if (document.all) {window.console.log(message);return false;}}
       
       function clickNS(e) {
if (document.layers||(document.getElementById&&!document.all)) {
       if (e.which==2||e.which==3) {window.console.log(message);return false;}
}
}
        if (document.layers)
        {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
        else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
        document.oncontextmenu=new Function("return false")

</script>


Monday, July 15, 2013

how to select all options in multi select dropdown using jquery


When we use multi select drop down, we may consider the feature for selecting all and deselect all option for the respective Multi Select box.

Using jQuery, we can easily do the select all and deselect all option for the dropdown.

Below is the code to select all using jQuery

HTML
<select name="education" id="education" multiple="multiple">
       <option value="1">Under Graduate</option>
       <option value="2">Graduate</option>
       <option value="3">Post Graduate</option>
       <option value="4">Pursuing Graduation</option>  
</select>

$('#education').find('option').attr('selected','selected');

Where 'education' is the id for the multi select & we are adding the attribute selected to get selected.

To deselect all using jQuery.

$('#education').find('option').removeAttr('selected','selected');

Wednesday, January 30, 2013

Basic UNIX commands



Below are the basic unix commands which may help you while using PUTTY to access the server. Move, copy, view the pages from folder to other destination. All other basic stuff syntax are given below


If there are images in this attachment, they will not be displayed.  Download the original attachment
Unix Basic Commands

Copy:

cp sorce_file target_file  :copies only if the files are in current direc

cp -r sor_DIR target_dir (to copy directories)

Move/Rename;

mv file1 file2

mv -i f1 f2 (asks for permision to move or not)

Remove:

rm filename

rm -r file (to remove directory structure)

rm -i file ( asks for permision to move or not)

view:

cat filename ( to view the complete file content)

To extract or unzip files:

unzip file ( extracts file to current dir)

To uncompress

uncompress filename

To compress

compress filename

To change directory

cd path

to list files in current dir

ls

ls -l (long list of files : gives acess permision,creation date..etc)

    -a(list all files satarting with a " .")

to make a dir

mkdir name or md name

to remove

rmdir dir_name

to clear the screen

clear (clears the windoe or terminal)

help commands

man <comd name> (displays the mannual page)

whatis <cammnd> (gives description)

date (to dispaly the date)

to count the words

wc filename (gives lines,words,characters

wc -l file (only lines)

wc -c file(only characters)

wc -w file(onbly words)

df ( displays the amount of free space)

grep (search a file for matching pattern)

grep [optioln]<regularExpression filename

eg: grep [a-z]*.c filename

Wednesday, January 16, 2013

Upload huge files in PHP


When we try to upload huge files using PHP, we face many issues to upload. One of them might be the php_value upload_max_filesize which is by default 2M in php.ini

Still we may have other issues which we need to do fix them. Below are the steps we need to add in htaccess to fix the large file uploads.


php_value upload_max_filesize 10M
php_value post_max_size 64M
php_value max_execution_time 300


Thursday, January 3, 2013

javascript validation for file upload

Below is the article for adding javascript validation (or) jquery validation for a file upload that makes our life easier whether the file upload is a valid type or not.


if($('#upload_file').val() != '')
{
        var _validFileExtensions = [".jpg", ".jpeg", ".bmp", ".gif", ".png"]; // Can change the extensions if we need for doc type or something else
        if($('#upload_file').val() != '')
        {
            var sFileName = $('#upload_file').val();
            var blnValid = false;
            for (var j = 0; j < _validFileExtensions.length; j++)
            {
                var sCurExtension = _validFileExtensions[j];
                if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                    blnValid = true;
                    break;
                }
            }
            if(!blnValid)
            {
               alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
                $('#upload_file').focus();
                return false;
            }
        }
 }