How to create a current year to 1900 select box in JSP

hey guys,

i got one simple logic to create a current year to 1900 select box. lets go for coding.

<select>

<%    
Date dNow = new Date( );    
SimpleDateFormat = new SimpleDateFormat ("yyyy");    
String year = ft.format(dNow);    
int foo = Integer.parseInt(year);    
while(foo > 1989)
{
%>

<option value=”<%=foo%>”><%=foo%></option>

<%        
 foo--;            
}
 %>

</select>

Advertisements

List of file with Delete option in JSP

ya , here is improved version of previous post. you can view all files within a particular directory as well as delete any file.

specially this kind of requirements came when i was working in Advertisement Management Module. so there admin can view all advertisement images which he uploaded previously and he can also able to delete from directory. so he can able to manage disk size.

k lets go for coding.

oky so first i need to fetch all images from directory with delete button. i m putting filename in button id value so i can easily recognize that for which file user pressing delete button. and once i’will get filename then rest of coding flow is very simple.. i will pass that filename to delete.jsp file and there i will write logic to delete that file.

listfiles.jsp

JavaScript Function

script_img_dlt

and bellow screenshot is for listing of files from directory.

jsp_img_dlt

and now bellow jsp file show you delete function for that file

<%@ page import="java.io.*" %>
<% 
String id = request.getParameter("id"); 
File file = new File("C:\\Users\\LYMAN\\Documents\\NetBeansProjects\\WebApplication2\\web\\upload\\"+id+""); 
if(file.delete()) 
{ 	
out.println("file deleted successfully");
} 
else { 
	out.println("failed to perform delete operation"); 
} 
%>

hope you like this post.. 🙂

Image upload validation using php

hello guys,

sometime it is necessary to check whether the uploaded file is valid Image file or not ?  or sometime you need to allow user to upload only few image extension file.  let’s say you want to allow user to upload only (gif, jpeg, jpg, png) extension file.

so let’s go for coding.

HTML Coding

<body>
<form method="post" action="uploadimage.php" enctype="multipart/form-data">
<table>
<tr>
<td> Upload Image here : </td>
<td> <input type="file" name="file"> </td>
</tr>
<tr>
<td> <input type="submit" value="upload"> </td>
</tr>
</table>
</form>
</body>

PHP Coding (uploadimage.php)

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$arr = explode(".", $_FILES["file"]["name"]);
$extension = end($arr);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jpg"))
&& in_array($extension, $allowedExts))
{
   if ($_FILES["file"]["error"] > 0)
    {
        echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
      move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"] );
      echo "<script>alert('file uploaded successfully')</script>";
     }
 }
else
{
      echo "Invalid file";
}
?>

Please make sure that you have upload folder in your current directory.!

Hope you enjoy this small script!
🙂

Creating HTML elements dynamically using jquery

sometime during your project you need to create some html elements dynamically based on user’s choice.

for example you are providing upload facilities to user such that initially only one upload widget is available but if user want to upload more things, they will click on add button and based on that upload widgets should created and appear there.

so you need to use two methods of jquery.

  • live method
  • clone method

1) live method:

The live() method attaches one or more event handlers for selected elements, and specifies a function to run when the events occur.

Event handlers attached using the live() method will work for both current and FUTURE elements matching the selector (like a new element created by a script).

2) clone method:

The clone() method makes a copy of selected elements, including child nodes, text and attributes.

now let’s write code for creating dynamic elements.

 

<!DOCTYPE html>
<html>
<head>
<script src=”jquery.js”></script>
<script>
$(function()
{
$(‘.Add’).live(‘click’,function(e)
{
$(‘.Option:last’).after($(‘.Option:first’).clone());
});
});

</script>
</head>
<body>
<div class=’Option’><input type=’text’ name=’t1′/>
<span class=’Add’><img src=”add.png” height=”30″ width=”30″/></span>
</body>
</html>

dynamically_element

if you press add button you will get another same row of textbox and add button.!

hope you like it.!