Blogs

How to get the checkbox label value?

When we work with checkbox, sometimes we have to use the checkbox labels. How to get it?

We know that, to get the checkbox value, we use the checkboxname.value;

We can get the label like below

checkboxname.nextSibling.nodeValue

Example
javascript to get the label:

function getChkboxLabel() {

var chkLength =document.testForm.testchk.length;
var selected;
for(i=0;i if(document.testForm.testchk[i].checked) {
alert(document.testForm.testchk[i].nextSibling.nodeValue);
}
}
}

Html to display the checkbox


<form name=testForm method="post">
<input type=checkbox name=testchk onclick="return getChkboxLabel()" value="1″>RADIO</input>
<input type=checkbox name=testchk onclick="return getChkboxLabel()" value="2″>TV</input>
</form>

PHP tips and tricks

In PHP, we can achieve the task by using different syntax. for example spilt/explode for split the string and other example is to search/match something by preg or ereg.
So in this article, i am just writing about few tips.

1. print_r : To print the content of the object or array in human readable form.
for example
$testArr = array('r'=>'Red','g'=>'Green', 'b'=>'Blue','other'=>array('w'=>'White','o'=>'Orange','p'=>'Pink'));
print_r($testArr);

you will get

Array(
[r] => Red
[g] => Green
[b] => Blue
[other] => Array
(
[w] => White
[o] => Orange
[p] => Pink
)
)

2. Swaping values in array without using intermediate
By using array and list function of PHP, you can swap values without using intermediate variable. For example

$bubble = array(10, 3);
list($small, $big) = $bubble;

3. @ operator - If we prepend this @ operator in front of function or expression any error thrown will be ignored

4. split/preg_split and explode

Split used to split the string using regular Expression, where as Explode split the string using string.
So question, when to use what? comes in mind. To split the string using regular expression then preg_split is faster than split.
If there is no regular expression used to split then your choices is explode which is faster than split.

5. Join and Implode
The join and implode is same. So can use anything to join the array.

6. preg and ereg

preg is perl compatible regexp library, which is faster than the ereg. Where as ereg is POSIX compatible regexp library and resource intensive.

Main thing is ereg going to be removed from pHP6. So avoid using ereg.

Example:
preg_match('/^a/i', 'abcd'); /i is for case sensitive search

if you want search/replace integer patterns first convert into hexadecimal and specify with escape
$number = 10;
$hexaCode = dechex($number);
preg_match("/\x$hexCode/", 'Ajdfjd^\tsdff');

7. Redirect to different page

header() provide way to redirect to specified internal/ external site location.

header("Location: http://php.net");

8. Handling header already sent error
Normally this error occurs when you try to setcookie or header after print something to the browser. Always the header or setCookie should be called before printing anything to the browser.
Always the header then body has to send to browser in the order.

for example
print "Sending before calling header";
setcookie("username","sasikala");
or
print "printing before redirecting");
header("Location: http://php.net");

9. get_browser()
If you want to send data based on browser type, you can use this function. There are some situation where you want to generate content based on browser type. For example application has to generate xhtml for mobile, html for other browser. Just get output of this function, parse it and based on type send output to browser.

You can get the browser name and version using $_SERVER['HTTP_USER_AGENT'] also.

I hope this would help you. You can get more details from
http://php.net

Symbolic Links and Hard Links

Recently for a project, i need to create a symbolic link in window box.

I know the linux soft and hard link creation. But it is first time for windows. So want to share about creation of links in windows and linux.

How to create symbolic link in windows 

when i google it i got something called junction.exe. so how to use it?

  1. download it from http://technet.microsoft.com/en-us/sysinternals/bb896768.aspx
  2. It is an exe file. Place it in folder c:\ or where you want it.
  3. Go to dos prompt type junction. It will show like below.
    C:\Program Files>junction

    Junction v1.05 - Windows junction creator and reparse point viewer
    Copyright (C) 2000-2007 Mark Russinovich
    Systems Internals - http://www.sysinternals.com

    The first usage is for displaying reparse point information, and the
    second usage is for creating or deleting a NTFS junction point:

    usage: junction [-s] [-q]
    -q Don't print error messages (quiet)

    -s Recurse subdirectories

    usage: junction [-d] []
    -d Delete the specified junction
    example: junction d:\link c:\winnt

  4. for example you want to create link called myprojectdir in c:\ pointing to actual folder c:\projects\projectname then  junction c:\myprojectdir "c:\projects\projectname"

    it will show like

    Junction v1.05 - Windows junction creator and reparse point viewer
    Copyright (C) 2000-2007 Mark Russinovich
    Systems Internals - http://www.sysinternals.com

    Created: c:\myprojectdir
    Targetted at: c:\projects\projectname

  5. To delete the link type : junction -d c:\myprojectdir 

Create Soft and Hard link in Linux

In linux, you can create soft link using ln -s command

for example, you want to create link called myproject in /tmp directory pointing to target directory /projects/project1


ln -s /projects/project1/myfiles.txt /tmp/myproject/myfiles.txt

to create hard link

ln /projects/project1/myfiles.txt /tmp/myproject/myfiles.txt

Hide and show the element

When you work in application, based on action you want to hide/show the particular or all elements in the page.

This can be achieved using the div tag and the style.

By setting the document.getElementById(id).style.display='none'; you can hide the element.

To show the element
document.getElementById(id).style.display='block';

for a sample

function showNextDiv()
{
document.getElementById("showFirst").style.display="none";
document.getElementById("ShowSecond").style.display="block";
}

Welcome to the Hide and show Element detail Page

I hope it is useful to you

you can toggle this based on click as like below

function toggleHideShow()
{
if(document.getElementById("ShowSecond").style.display=="none") {
document.getElementById("showFirst").style.display="none";
document.getElementById("ShowSecond").style.display="block";
}else {
document.getElementById("showFirst").style.display="block";
document.getElementById("ShowSecond").style.display="none";

}
}

Syndicate content