Sliding Down Calender

javascriptFile2 CssFile2 cssFile1 javascriptFile1

This is a calender which will display when we click on the text box.The calender will slide down when the visitor click on the text box…. 😀

All the javascript and CSS files are attached at the top of this post.Just copy and save with the names given in the top of those files.save these files in to the same folder where calender.php file is included.



 

 

 

 

 

 

 

 

 

 

 

 

 

 

The following is the php code….
<html><title>Calender</title>

<head>

<LINK href=”ui.datepicker.css” rel=”stylesheet” type=”text/css”>
<LINK href=”ui.theme.css” rel=”stylesheet” type=”text/css”>
<script src=”jquery-1.3.2.js” type=”text/javascript”></script>
<script src=”ui.datepicker.js” type=”text/javascript”></script>
<script>

function getStartDate(){
$(‘#txtStartDate’).datepicker({showButtonPanel: false});
}

</script>
</head>
<body>
Start Date : <input type=”text” id=”txtStartDate” onFocus=”getStartDate()”>

</body>
</html>

Advertisements
Published in: on November 18, 2009 at 12:12 pm  Leave a Comment  

PHP Pagination

pgination

If there are lot of rows are available to display in a web page,we can use pagination to display rows as parts.

The following is an example of php pagination.

This is the database table was used to get data.
database

This is the output after executing the code…..

<!–insert HTML–>
<?php echo '
<HTML>

<head>
<style type="text/css">

div.pagination {
padding: 3px;
margin: 3px;
}

div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;

text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;

color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #000099;

font-weight: bold;
background-color: #000099;
color: #FFF;
}
div.pagination span.disabled {
padding: 3px 7px 3px 7px;
margin: 2px;
border: 1px solid #EEE;

color: #DDD;
}

</style>
</head>
<body>
<b><p align="center">Pagination in PHP</p></b><br>
<table align="center" border="1">
<th>SESSION ID </th>
<th>DATE CREATED</th>'

; ?>

<?php
//connect to the database
$db=mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('LMS');

//give the table name here
$tbl_name="sessions";

// How many adjacent pages should be shown on each side?
$adjacents = 3;

/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

//I will load data in the same page..Therefore I have given the same name
$targetpage = "pagination.php";
//how many items to show per page.. I will show 5 rows
$limit = 5;

$page = "";
$page = $_GET['page'];
if($page)
$start = ($page – 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0

/* Get data. */
$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>'.$row['SESSION_ID'].'</td>';
echo '<td>'.$row['DATE_CREATED'].'</td>';
echo '</tr>';
}

/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page – 1; //previous page is page – 1
$next = $page 1; //next page is page 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage – 1; //last page minus 1

/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
else
$pagination.= "<span class=\"disabled\">« previous</span>";

//pages
if ($lastpage < 7 ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter )
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 ($adjacents * 2))
{
for ($counter = 1; $counter < 4 ($adjacents * 2); $counter )
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "…";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage – ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "…";
for ($counter = $page – $adjacents; $counter <= $page $adjacents; $counter )
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "…";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "…";
for ($counter = $lastpage – (2 ($adjacents * 2)); $counter <= $lastpage; $counter )
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}

//next button
if ($page < $counter – 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
else
$pagination.= "<span class=\"disabled\">next »</span>";
$pagination.= "</div>\n";
}
?>

<?php echo '</table><br>'; ?>

<?php echo $pagination. '</body></HTML>'; ?>

Published in: on November 11, 2009 at 10:46 am  Leave a Comment  

Image Upload


This is a simple way to upload pictures using PHP.While uploading the images can be resized.

<?php
error_reporting(0);

$change="";

//set the maximum size of the images can be uploaded
define ("MAX_SIZE","400");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
return "";
}
$l = strlen($str) – $i;
$ext = substr($str,$i 1,$l);
return $ext;
}

$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];

if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
//set the file types which are acceptable
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")&& ($extension != "bmp")&& ($extension != "TIFF"))
{
$change='Unknown Image Extension';
$errors=1;
}
else
{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
$change='You have exceeded the size limit!';
$errors=1;
}
else
{

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;
list($width,$height)=getimagesize($uploadedfile);

//set the file size according to your choice
$newwidth=100;
$newheight= 80;
$tmp=imagecreatetruecolor($newwidth,$newheight);

//the original image
$originalWidth=$width;
$newheight1=$height;
$tmp1=imagecreatetruecolor($originalWidth,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$originalWidth,$originalHeight1,$width,$height);

//set the name of the file
$filename = "small_". $_FILES['file']['name'];
$filename1 = $_FILES['file']['name'];

imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
}

//If no errors registred, print the success
if(isset($_POST['Submit']) && !$errors)
{
$change='Image Uploaded Successfully!';
}

?>

HTML Code

<html>
<head>
<title>Picture Upload</title>
</head>
<body>

<table border="0" cellspacing="3" cellpadding="15" align="center">
<tbody>
<form method="post" action="" enctype="multipart/form-data" name="form">
<tr>
<td><?php echo $change; ?> </td>
</tr>
<tr>
<td><img src="<?php echo $filename; ?>" /> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>
<tr>
<td> <input size="25" name="file" type="file" /></td>
</tr>

<tr>
<td>Image maximum size&nbsp;&nbsp;<b>400 </b>&nbsp;&nbsp;kb</td>
</tr>
<tr>
<td><input type="submit" value="Upload" name="Submit"/></td>
</tr>
</tbody>
</form>
</table>

</body>
</html>

Published in: on November 6, 2009 at 5:39 am  Leave a Comment  

Prevent SQL Injection Attack in PHP

MYsql injection takes place when a visitor to website enters values into a form.Tha is a form input by a visitor (Fill a form).

A Login form can be taken as an example.Someone can enter a query inside our query and can change the code.Therefore when we do programming like form inputs , we should be aware of this mysql injection attacks.And we should use some tricks to protect our sites against sql injection.

PHP has a way of avoiding sql injection attacks.That is mysql_real_escape_string()
We can use variable assined values with this rather using values directly…

Normally values can be assigned directly as follows..
$sql = “SELECT COUNT(*) FROM USER WHERE USERNAME = $this->userName”;

We can use the following query to avoid this issue…
$sql = sprintf(“SELECT COUNT(*) FROM USER WHERE USERNAME = ‘%s'”, mysql_escape_string($this->userName));

Published in: on October 27, 2009 at 8:14 am  Leave a Comment  

Dreamweaver Vs NetBeans for PHP – Part 1


I started web developing with dreamweaver. It was easy for me to deal with code while shifting to the design.’Split’ mode helps to view both code and the design at the same time.Therefore it is very easy to do web developing with dreamweaver when we deal with both design and the programming.


Today I satrted web developing with Netbeans.Wow.Netbeans is very easy to use with software developing specially with OOP.When we dont have to deal with the design very much, we can use NetBeans.If we are doing OOP(Object Oriented programming), the objects will be displayed in a panel.Therefore the objects can be identified very easily than dreamwaver.


We can select the compiler as we want as shown in the above screenshot.If we need the browser we can set it.Otherwise we can just use a commandline complier for debugging.


Another advantage of NetBeans is , while we are coding we can see the sintax errors (eg: missing Braces) as shown in the figure below.

But my conclusion is that dreamweaver and NetBeans has different types of advantages.The things that can be done by dreamweaver canot be done in with NetBeans and vise versa.Therefore both are very useful tools for web developing.

I hope to tell about the difference between those two further while having experience with netbeans… 🙂

Published in: on October 15, 2009 at 11:52 am  Leave a Comment  

include() Vs require() in PHP

These two functions in php means the same functionality.But require() will result a fatal error but include() will give a warning.That means the error handling is different.Therefore when an error occurs in include(), it will display the error and the programme will not terminate (Because the error is a warning).But in require() function the program will terminate because it will give a fatal error…

Published in: on October 15, 2009 at 10:31 am  Leave a Comment  

include() Vs include_once() in PHP

include_once() is safer than include()…

As an example, lets assume:

we use include() to use a file and that file has a function.Again I include another file and that file also has the same function.Then there will be a conflict of functions because the same function has been already added.But if we use include_once(), it will not be a problem,because it will avoid of verwitting.Therefore include_once() is a safe method to avoid functions from redefining and overwriting.Since we can use include() many more times , it will cause conflicts due to duplicating functions and variable value reassigning.

Lets discuss a very simple example to understand the difference between include() and include_once()

hello.php file will print “Hello”. we will use this in display.php file as follows…

hello.php

<?php
echo “Hello”;
?>

display.php

<?php
include(‘hello.php’); //this will print ‘Hello’
include(‘hello.php’); //This will again print ‘Hello’
include(‘hello.php’); //This will print ‘Hello’ for the third time also

include_once(‘hello.php’); //This will not diaply ‘Hello’ again
include_once(‘hello.php’); //This will not diaply ‘Hello’ again

?>

Published in: on October 15, 2009 at 9:18 am  Leave a Comment  

PHP mail()

In the php manual , it is simply mentioned as
<?php
$to = ‘bob@example.com’;
$subject = ‘Wakeup bob!’;
$message = ‘yo, whassup?’;
$headers = “From: server@example.com\r\n” .
‘X-Mailer: PHP/’ . phpversion() . “\r\n” .
“MIME-Version: 1.0\r\n” .
“Content-Type: text/html; charset=utf-8\r\n” .
“Content-Transfer-Encoding: 8bit\r\n\r\n”;
?>

// Send
mail($to, $subject, $message, $headers);

But sometiems this will not work due to the server’s php intial file’s settings.So what we can do is to overcome this problem,
we sould set the php ini settings as follows.Then it will work properly.

Following is an example which describes the php mail function with php ini settings.

This is the Form

This is the HTML code for the above form

<html>
<title>Comments</title>
<body>
<form method="POST" action="feedback.php">
<table>
<tr>
<td width="87">Name:</td>
<td width="242"><input name="name" size="25" type="text"></td>
</tr>
<tr>
<td>Email :</td>
<td><input name="email" size="25" type="text"></td>
</tr>
<tr>
<td valign="top"> <div align="left">Comments </div></td>
<td> <textarea rows="15" cols="25" name="comments"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"> <input name="submit" value="Send" type="submit"><input name="Clear" value="Clear" type="reset"> </td>
</tr>
</table>
</form>
</body>
</html>


This is the PHP code

<?php

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$comments = $_REQUEST['comments'];

$headers .= "Content-type: text/html\r\n";
$subject = "Feedback From a Viewer";
//$mail = "someone@somewhere.com";
$additional_parameters = "-r $mail";

$message = "Name: $name<br><br> Email Address: $email<br><br> Comments: $comments ";

//
ini_set("SMTP","mail.servername.com");
ini_set("smtp_port","25");
ini_set("sendmail_from","$email");
ini_set("display_errors",0);

if(mail("mails@hndesl.com","$subject","$message","$headers",$additional_parameters))
{
echo "Thank You For Your Feedback";
}
else
{
echo "Thank You For Your Feedback";
}

?>

Published in: on October 13, 2009 at 7:31 am  Leave a Comment  

Multiple Picture Upload

This is the Interface at the beginning

This is the interface after uploading

This code can be used to upload five pictures at the same time in to the server folder named “images.”When the pictures are uploaded , they will be resized and saved in to the server’s folder.And the uploaded pictures will be diaplyed in the browser.

(Note: Create a folder called “images” in the same folder where this file will be inserted.That is the folder where pictures are going to be uploaded.)

PHP Code

<?php
error_reporting(0);

$change="";

//set the maximum size of the images can be uploaded
define ("MAX_SIZE","400");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
return "";
}
$l = strlen($str) – $i;
$ext = substr($str,$i 1,$l);
return $ext;
}

$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
global $files;
$files = array('file','file2','file3','file4','file5');

for($i=0; $i<5; $i )
{
$file = $files[$i];

$image =$_FILES["$file"]["name"];
$uploadedfile = $_FILES["$file"]["tmp_name"];

if ($image)
{
$filename = stripslashes($_FILES["$file"]["name"]);
$extension = getExtension($filename);
$extension = strtolower($extension);

if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
$change='<div class="msgdiv">Unknown Image extension </div> ';
$errors=1;
}
else
{
$size=filesize($_FILES["$file"]["tmp_name"]);
if ($size > MAX_SIZE*1024)
{
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
else
{

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES["$file"]["tmp_name"];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES["$file"]["tmp_name"];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;
list($width,$height)=getimagesize($uploadedfile);

$newwidth=100;
$newheight= 80;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth1=$width;
$newheight1=$height;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

$filename = "images/small_". $_FILES["$file"]["name"];
$filename1 = "images/". $_FILES["$file"]["name"];
echo "<img src=' $filename ' />&nbsp;&nbsp;&nbsp;";
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
}
}

//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
$change=' Images Uploaded Successfully!';
}

?>

HTML code

<html>
<head><title>Multiple Picture Upload</title>
</head><body>
<table border="0" cellspacing="3" cellpadding="15" align="center">
<tbody>
<form method="post" action="" enctype="multipart/form-data" name="form">
<tr>
<td><?php echo $change; ?> </td>
</tr>
<tr>
<td> <input size="25" name="file" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file2" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file3" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file4" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file5" type="file" /></td>
</tr>
<tr>
<td>Image maximum size&nbsp;&nbsp;<b>400 </b>&nbsp;&nbsp;kb</td>
</tr>
<tr>
<td><input type="submit" value="Upload" name="Submit"/></td>
</tr>
</tbody>
</form>
</table></body>
</html>

Published in: on October 13, 2009 at 5:47 am  Leave a Comment  

Image Upload

Interface After Uploading Images

PHP Code

<?php
error_reporting(0);

$change="";

//set the maximum size of the images can be uploaded
define ("MAX_SIZE","400");
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i)
{
return "";
}
$l = strlen($str) – $i;
$ext = substr($str,$i 1,$l);
return $ext;
}

$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];

if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
//set the file types which are acceptable
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")&& ($extension != "bmp")&& ($extension != "TIFF"))
{
$change='Unknown Image Extension';
$errors=1;
}
else
{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
$change='You have exceeded the size limit!';
$errors=1;
}
else
{

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;
list($width,$height)=getimagesize($uploadedfile);

//set the file size according to your choice
$newwidth=100;
$newheight= 80;
$tmp=imagecreatetruecolor($newwidth,$newheight);

//the original image
$originalWidth=$width;
$newheight1=$height;
$tmp1=imagecreatetruecolor($originalWidth,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$originalWidth,$originalHeight1,$width,$height);

//set the name of the file
$filename = "small_". $_FILES['file']['name'];
$filename1 = $_FILES['file']['name'];

imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
}
}
}

//If no errors registred, print the success
if(isset($_POST['Submit']) && !$errors)
{
$change='Image Uploaded Successfully!';
}

?>

HTML Code

<html>
<head>
<title>Picture Upload</title>
</head>
<body>

<table border="0" cellspacing="3" cellpadding="15" align="center">
<tbody>
<form method="post" action="" enctype="multipart/form-data" name="form">
<tr>
<td><?php echo $change; ?> </td>
</tr>
<tr>
<td><img src="<?php echo $filename; ?>" /> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;</td>
</tr>
<tr>
<td> <input size="25" name="file" type="file" /></td>
</tr>

<tr>
<td>Image maximum size&nbsp;&nbsp;<b>400 </b>&nbsp;&nbsp;kb</td>
</tr>
<tr>
<td><input type="submit" value="Upload" name="Submit"/></td>
</tr>
</tbody>
</form>
</table>

</body>
</html>

Published in: on October 12, 2009 at 12:33 pm  Leave a Comment