Monday, May 31, 2010

JavaScript Select all Checkbox

How to select all checkbox in form


Usage : Simple JavaScript for select and deselect all checkbox in form.
Compatibility :




JavaScript Code :
Insert this JavaScript in the head section of the html code.

<script type="text/javascript">
function select_all(option){
var no_of_elements = form1.elements.length;
for(loop=0; loop < no_of_elements; loop++){
form1.elements[loop].checked = option;
}
}
</script>

Html Code :

<html>
<head>
<title>Select All Check Box </title>
</head>
<body>
<form name="form1" id="form1" action="#" method="post">
checkbox 1<input type="checkbox" name="checkbox_item" id="checkbox_item" value="checkbox 1">
checkbox 2<input type="checkbox" name="checkbox_item" id="checkbox_item" value="checkbox 2">
checkbox 3<input type="checkbox" name="checkbox_item" id="checkbox_item" value="checkbox 3">
</form>
<input name="select_all" type="button" value="Select All" onclick="select_all('true')">
<input name="clear_all" type="button" value="Clear All" onclick="select_all('')">
</body>
</html>

Tuesday, May 18, 2010

JavaScript Validate file extension

How to validate upload file extension

Usage : Simple JavaScript for validate upload file extension. If you use this validation, user can't upload wrong file type.

Compatibility :




JavaScript Code :
Insert this JavaScript in the head section of the html code.

<script type="text/javascript">
function check_file_type(){
var file_name = document.getElementById('upload_file').value;
var file_extension = file_name.substring(file_name.lastIndexOf('.') + 1);

if(file_extension == "gif" || file_extension == "jpg") {
alert("Ok. This is valid file");
return true;
} else {
alert("Invalid File. File Format supported: JPJ and GIF Only");
return false;
}
return true;
}
</script>

Html Code :

<html>
<head>
<title>Check File Extension </title>
</head>

<body>
<input name="upload_file" id="upload_file" type="file" onchange="check_file_type();" />
</body>

</html>

Demo :





JavaScript Email Validation

Smart JavaScript Email Validation

How to validate email address use JavaScript, What is correct Email format


Usage:
This validation is used to check whether the email address is in correct format. The correct email format is xxx@xxx.top-level-domain. xxx must be '-' or ' _' or '.' or alphanumeric characters only

compatibility:

JavaScript Code
:

Insert this JavaScript in the head section of your html code.


<script type="text/javascript">
function validation(control_name,id){
var address = document.getElementById(control_name).value;
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
switch (id){
case 1:
if(address == ''){
alert('Enter your Email Address');
document.getElementById(control_name).focus();
}else if(reg.test(address) == false) {
alert('Invalid Email');
} else {
alert('Valid Email');
}
break;

case 2:
if(address == ''){
document.getElementById('message_email').innerHTML = ('Enter your Email Address');
}else if(reg.test(address) == false) {
document.getElementById('message_email').innerHTML = ('Invalid Email');
} else {
document.getElementById('message_email').innerHTML = ('Valid Email');
}
break;
}
}
</script>


HTML Code
:


<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<table border="0" cellpadding="2" cellspacing="2">
<tr&gt
<td>Email Address</td>
<td><input name="email_alert" id="email_alert" type="text" /></td>
<td><input name="btn_validate" type="button" value="Validate" onclick="validation('email_alert',1)" /></td>
</tr>
<tr>
<td>Email Address </td>
<td><input name="email" id="email" type="text" onkeyup="validation(this.id,2)"/></td>
<td style="color:#FF0000; font-family:Arial, Helvetica, sans-serif; font-size:10px;" id="message_email"> </td>
</tr>
</table>
</body>
</html>



Demo
:

Email Address
Email Address




Monday, May 17, 2010

PHP Use htmlspecialchars() function

Allow to Display double quote in Text Field

Usage : In normally we cant display " " " in text field. Because after " value is break. But using htmlspecialchars() function we can allow to display double quote.

Text Field
<input type="text" name="textfield" value="<?=htmlspecialchars('"')?>">

PHP Validate Alphanumeric Characters

Validate alphanumeric characters use PHP


HTML Code:
Copy this code and save as form.php

<form action="validate.php" method="post">
<input name="name" id="name" type="text"/>
<input name="submit" type="submit" value="Validate">
</form>

PHP Code:
Copy this code and save as validate.php

<?
$name = $_REQUEST['name'];
if(!ereg('^[[:alnum:]]+$',$name)){
echo "Not a alphanumeric characters";
} else {
echo "Valid alphanumeric characters";
}
?>



PHP Validate Email Address

Validate email address use PHP


HTML Code:
Copy this code and save as form.php

<form action="validate.php" method="post">
<input name="email" id="email" type="text"/>
<input name="submit" type="submit" value="Validate">
</form>

PHP Code
Copy this code and save as validate.php

<?
$email = $_REQUEST['email'];
if(!ereg('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$',$email)){
echo "Not valid mail address";
} else {
echo "Valid mail address";
}
?>



PHP Convert Date Format

How to Convert Date Format in PHP

<?
$enterd_date = "2010-01-28";
echo "Entered Date = ".$enterd_date."
<br />";
echo "Converted Date = ".dateconvert($enterd_date,2);

function dateconvert($date,$func) {
if ($func == 1){
list($day, $month, $year/*if you use this function this order should match your entered date format*/) = split('[-.]', $date);
$date = "$year-$month-$day";
return $date;
}
if ($func == 2){
list($year, $month, $day) = split('[-.]', $date);
$date = "$day-$month-$year";
return $date;
}
}
?>

PHP Resize Images


How to Resize Upload Images


Step 1:
Copy this code and save as upload-form.php

<?
//function of move_uploaded_file() use to upload images
$new_file_name=time().$_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"],"image/" . $new_file_name);
$uploaddir='image/';
?>

<!--This is upload form-->
<form action="upload-form.php" method="post"
enctype="multipart/form-data">
<p><label for="file">Filename:</label>
<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Submit" /></p>
</form>
<?
$d_image=glob("image/*.*");//glob function use the read the image in image folder
for($row_count=0; $row_count < sizeof($d_image); $row_count++ ){
$name=$d_image[$row_count];
echo "<a href='$name'?rel='".thumbnail."'><img src= 'image-resize.php?src=".$name."&wmax=150&hmax=100&quality=90&bgcol=#FFFFFF'></a>";
}
?>

Step 2:
Copy this and save as image-resize.php

<?
header("Content-type: image/jpeg");
foreach($_REQUEST as $key=>$value){
$key=strtolower($key);
$$key=$value;
}
$source = imagecreatefromjpeg($src);
$orig_w=imagesx($source);
$orig_h=imagesy($source);

if ($orig_w>$wmax || $orig_h>$hmax)
{
$thumb_w=$wmax;
$thumb_h=$hmax;
if ($thumb_w/$orig_w*$orig_h>$thumb_h)
$thumb_w=round($thumb_h*$orig_w/$orig_h);
else
$thumb_h=round($thumb_w*$orig_h/$orig_w);
} else
{
$thumb_w=$orig_w;
$thumb_h=$orig_h;
}
if (!@$bgcol)
{
$thumb=imagecreatetruecolor($thumb_w,$thumb_h);
imagecopyresampled($thumb,$source,
0,0,0,0,$thumb_w,$thumb_h,$orig_w,$orig_h);
}
else
{
$thumb=imagecreatetruecolor($wmax,$hmax);
imagefilledrectangle($thumb,0,0,$wmax-1,$hmax-1,intval($bgcol,16));
imagecopyresampled($thumb,$source,
round(($wmax-$thumb_w)/2),round(($hmax-$thumb_h)/2),
0,0,$thumb_w,$thumb_h,$orig_w,$orig_h);
}
if (!@$quality) $quality=90;
imagejpeg($thumb,"",$quality);
imagedestroy($thumb);
?>


Tips : 1.Create folder call image for upload image.
2.Edit 'wmax' and 'hmax' in upload-form.php for change preview image size.



Post Multiple Check Box value

How to post multiple check box value in to PHP file


HTML Code :
Copy this code and save as multi-ckeck.php

<form action="view.php" method="post">
checkbox 1<input type="checkbox" name="subscrib_item[]" id="subscrib_item" value="checkbox 1">
checkbox 2<input type="checkbox" name="subscrib_item[]" id="subscrib_item" value="checkbox 2">
checkbox 3<input type="checkbox" name="subscrib_item[]" id="subscrib_item" value="checkbox 3">
<input name="submit" type="submit" value="Submit">
</form>

PHP Code :
Copy this code and save as view.php

<?
$value = $_REQUEST['subscrib_item'];
for($loop=0;$loop < sizeof($value); $loop++){
echo $value[$loop];
}
?>



Thursday, May 13, 2010

PHP File Handling

How to Handling file in PHP

Mean of the file handling is file open, file read, file write, file delete, file copy, set file mode etc.There are more file handling functions use in php. But functions use of file read, file write, file delete and file copy are most common.

File Open

The fopen() function is used to open files in PHP. There are two parameters are use in this function. The first parameter of this function contains the name of the file to be opened. We can give file in our server or external server in this parameter. The second parameter specifies in which mode the file should be opened. These are the file mode of we can use.

r - Read only
r+ - Read/Write
w - Write only
w+ - Read/Write
a - Append
a+ - Read/Append
x - Write only
x+ - Read/Write

Example :

$file = fopen("file.txt" ,"r");

File Read

The fread() function is used to read opened file in PHP. fread(file.length) is a syntax of this function. The "file" parameter specifies the open file to read from. The length parameter specifies the maximum number of bytes to read.

Example :

$file = fopen("file.txt" ,"r");
$contents = fread($file, 20);

Tips : If you want to read full file, use "filesize()" for length parameter

$file = fopen("file.txt" ,"r");
$contents = fread($file, filezize("file.txt"));
print $contents;

File Write

The fwrite() function is used to writes to an open file in PHP. fwrite(file,text,length) is a syntax of this function. The "file" parameter specifies the open file to write to, the "text" parameter specifies the string to write to the open file and the "length" parameter specifies the maximum number of bytes to write.

Example :
$file = fopen("file.txt" ,"r");
$contents = fread($file,filesize("file.txt"));
fwrite($file,"This is new file",20);

Tips : 1. If you want to write full length of text you can remove "length" parameter in function
           2. If yoy want to create new file use "a" mode in fopen() function. Ex fopen("file.txt" ,"a");



Tuesday, May 11, 2010

JavaScript Number Validation

How to Validate Numbers in JavaScript


Usage : Two simple JavaScript for validate numbers. The first JS use is_NaN() function for validation.
This function take both positive and negative numbers as number. But second function
take positive numbers only as number.

Compatibility :





JavaScript Code :
Insert this JavaScript in the head section of your html code.

<script type="text/javascript">
function check_is_number_1(){
if(isNaN(document.getElementById('textbox_1').value) == false){
alert("Ok. This is a number");
document.getElementById('textbox_1').focus();
return false;
} else {
alert("Error! This is not a number");
}
}

function check_is_number_2(){
var reg = /^[0-9]+$/;
if(reg.test(document.getElementById('textbox_2').value) == true){
alert("Ok. This is a number");
document.getElementById('textbox_2').focus();
return false;
} else {
alert("Error! This is not a number");
}
}

</script>

HTML Code :

<html>
<head>
<title>Validate Numbers </title>
</head>
<body>
Enter Number 1 : <input name="textbox_1" id="textbox_1" type="text" />
<input name="check" id="check" type="button" value="Check" onClick="check_is_number_1()"><br /><br />
Enter Number 2 : <input name="textbox_2" id="textbox_2" type="text" />
<input name="check_2" id="check_2" type="button" value="Check" onClick="check_is_number_2()">
</body>
</html>

Demo :

Enter Number 1 :



Enter Number 2 :

Monday, May 10, 2010

JavaScript Validate Multiple check box

How to validate Multiple check box in JavaScript

Usage :This validation can use to least one or more of the checkboxes is required to be checked

Compatibility :



JavaScript Code :
Insert this JavaScript in the head section of the html code.

<script language="javascript1.1">
function validate_form(the_form){
var counter=0;
for (i=0; i< document.form1.checkbox_item.length; i++) {
if ((document.form1.checkbox_item[i].checked)) {
counter+=1;
}
}
if(counter==0){
alert("Please select one or more checkbox");
return false;
}
return true;
}
</script>

Html Code :

<form name="form1" id="form1" action="#" method="post" onsubmit="JavaScript:return validate_form(this);">
checkbox 1<input type="checkbox" name="checkbox_item[]" id="checkbox_item" value="checkbox 1">
checkbox 2<input type="checkbox" name="checkbox_item[]" id="checkbox_item" value="checkbox 2">
checkbox 3<input type="checkbox" name="checkbox_item[]" id="checkbox_item" value="checkbox 3">
<input name="submit" type="submit" value="Submit">
</form>

Demo :