Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

Friday, May 9, 2014

Multiple File upload with Ajax

Sry Guyz for the late post. I was out of station thats why and my gf my computer was not with me but now I Am Back. In this post we i will show how to create a awsm Multiple File upload with percentage and results with ajax. So get Started.

Live Demo Watch Video Tutorial Download Script

html

markup for this is very simple we are just using a <form> with <input type="file"> and Submit button and we have give the multiple attribute to the file so that the user can select multiple file. We have created 3 empty div for putting result via javascript
<form action="upload.php" method="post" enctype="multipart/form-data">
 <fieldset>
  <legend>File upload</legend>
  <input type="file" name="file[]" id="file" multiple required/>
  <input type="submit" name="upload" id="upload"/>

  <div id="per"></div>
  <div id="sucess"></div>
  <div id="error"></div>
 </fieldset>
</form>

php

In this php script we are getting the file from the html file and checking it if the file is valid we r uploading the file and stroing it in a array else we are storing the error message in a array and in last we are encoding the success and error array into json format
<?php
header('Content-Type: application/json');

//function to get the extension of the file
function getExt($name){
 $array = explode(".", $name);
 $ext = strtolower(end($array));
 return $ext;
}

//create global array
$allowed = array('gif', 'png', 'jpg');
$success = array();
$error = array();

if(isset($_FILES['file']) && !empty($_FILES['file'])){
  foreach ($_FILES['file']['name'] as $key => $name) {
   $tmp = $_FILES['file']['tmp_name'][$key];
   $ext = getExt($name);
   $size = $_FILES['file']['size'][$key];

   // check the extension is valid or not
   if(in_array($ext, $allowed) == true){
    $filename = md5($name) . time() .'.'.$ext;
    //check the size of the file
    if($size <= 2097152){
     if(move_uploaded_file($tmp, 'uploads/'.$filename) === true){
      $success[] = array('name' => $name, 'location' => 'uploads/'.$filename);
     }else{
      $error[] = array('name' => $name, 'msg' => 'File is not uploaded');
     }
    }else{
     $error[] = array('name' => $name, 'msg' => 'File size more than 2MB');
    }
   }else{
    $error[] = array('name' => $name, 'msg' => 'File Type not allowed');
   }
  }
  
  //encode the result in json format
  $json = json_encode(array(
   'success' => $success,
   'error' => $error
  ));

  echo $json;
  exit();
}
?>

javascript

we are sending a XMLHttpRequest to the server and displaying the response given by the upload.php page back to the user in empty div which we have created earlier 
<script type="text/javascript">
 var form = document.querySelector('#upload'),
   fileToUpload = document.querySelector('#file'),
  button = document.querySelector('#submit'),
  per = document.querySelector('#percentage'),
  sucessDiv = document.querySelector('#success'),
  errorDiv = document.querySelector('#failed'),
  sucessMarkup = '',
  errorMarkup = '';

 form.addEventListener('submit', function(e){
  e.preventDefault();

  var files = fileToUpload.files;

  var formdata = new FormData();

  //loop throught all the files

  for (var i = 0; i < files.length; i++) {
   var file = files[i];

   formdata.append('file[]', file);
  }

  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'upload.php');

  xhr.addEventListener('readystatechange', function(){
   if(xhr.readyState === 4){
    if(xhr.status === 200){
     var uploaded = JSON.parse(xhr.response);
     //put the json response to the div
     //success files 
     for(var i = 0; i < uploaded.success.length; i++){
      var name = uploaded.success[i].name;
      var location = uploaded.success[i].location;

      sucessMarkup += "<a href='view.php?id="+location+"' target='_blank'>"+name+"</a>";
     }

     //error file

     for (var i = 0; i < uploaded.error.length; i++) {
      var name = uploaded.error[i].name;
      var msg = uploaded.error[i].msg;

      var errorH1 = document.createElement('h2');
      errorH1.innerHTML = 'Unfortunately '+uploaded.error.length+' files are not uploaded';
      errorDiv.appendChild(errorH1);
      errorMarkup += '<p>'+name+' - '+msg+'</p>';
     };
     
     //put the results in the div
     sucessDiv.innerHTML = sucessMarkup;
     errorDiv.innerHTML = errorMarkup;
    }
   }else{
    console.log('error');
   }
  }, false);
 
  //code for showing the upload percenatge
  xhr.upload.addEventListener('progress', function(e){
   if(e.lengthComputable === true){
    var per = Math.round((e.loaded / e.total) * 100);
    percentage.innerHTML = per + '% uploaded';
   }
  })

  
  xhr.send(formdata);

 }, false);
</script>

Wednesday, April 23, 2014

Create a custom confirm box plugin with jQuery

Hey guyz sorry for the late post i was busy with girls hahaha but now i am back to my best girlfriend my computer. In this post i am going to show u how to create and use a confirm box plugin with jQuery so get started.

View demo Download Get the Latest version from GitHub

Markup of confirm box

markup is simple we have a <section> with class of .confirm-container inside we have 3 <div> of header, body, and footer
<section class="confirm-container">
			<div class="confirm-h">
				<span><!--confirm box headline--><span>
					<button class="close">x</button>
				</div>
			<div class="confirm-b">
				<p><!--confirm box message--></p>
			</div>
			<div class="confirm-f">
				<button id="cancel-btn" class="btn btn-grey">Cancel</button>
				<button class="btn btn-blue" id="function-btn">Ok</button>
			</div>
</section>

css

body{
	margin: 0;
	padding: 0;
}
#container{
	height: 700px;
width: 600px;
border: 1px solid rgba(0, 0, 0, 0.5);
margin: 0 auto;
padding: 10px;

}
h2{
	font-family: arial;
}
.overlay{
	display: none;
	position: fixed;
	left: 0;
	top: 0;
	height: 100%;
	width: 100%;
	background: #555;
	opacity: 0.5;
	z-index: 10;
}
.confirm-container{
	display: none;
position: fixed;
background: #fafbfb;
height: 200px;
width: 500px;
border-radius: 10px;
box-shadow: 2px 2px 15px #555;
z-index: 15;
}
.confirm-h{
	padding: 5px;
height: 30px;
border-bottom: 1px solid #ccc;
}
.confirm-h > span{
	font-family: arial;
font-weight: 400;
font-size: 20px;
}
.confirm-h .close{
float: right;
border: 0px;
background: transparent;
font-size: 15px;
font-weight: bold;
margin: 5px;
opacity: 0.6;
outline: none;
}
.confirm-h .close:hover{
	cursor: pointer;
	opacity: 1;
}
.confirm-b{
	padding: 5px;
font-family: arial;
height: 110px;
border-bottom: 1px solid #ccc;
}
.confirm-b > p{
	margin: 0;
	padding: 0;
}
.confirm-f{
	float: right;
padding: 3px;
}
/*style for buttons*/
.btn{
	display: inline-block;
font-weight: 400;
 text-align: center;
background: #fafbfb;
border:0;
padding: 3px 6px;
line-height: 1.5;
font-size: 14px;I
border-radius: 5px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
text-decoration: none;
color: black;
cursor: pointer;
font-family: verdana;
outline: none;
}
.btn-grey{
	background: #fafbfb;
	border: 1px solid #ccc;
	box-shadow: 1px 1px 2px #ccc;
	-webkit-box-shadow: 1px 1px 2px #ccc;
	-moz-box-shadow: 1px 1px 2px #ccc;
}
.btn-grey:hover{
	box-shadow: inset 1px 1px 2px #ccc;
	-webkit-box-shadow: inset 1px 1px 2px #ccc;
	-moz-box-shadow: inset 1px 1px 2px #ccc;
	cursor: pointer;
}

.btn-blue{
	background: #4c66a4;
	border: 1px solid #445C94;
	color: #fff;
	box-shadow: 1px 1px 2px #7085B6;
	-webkit-box-shadow: 1px 1px 2px #7085B6;
	-moz-box-shadow: 1px 1px 2px #7085B6;
}
.btn-blue:hover{
box-shadow: inset 1px 1px 2px #7085B6;
	-webkit-box-shadow: inset 1px 1px 2px #7085B6;
	-moz-box-shadow: inset 1px 1px 2px #7085B6;
}

Create a jQuery plugin

I will show u how to create a jQuery plugin but for making it easy i will brake this process in 4 or 5 parts.

1. Define a jQuery plugin

//define a plugin function
(function($){
	//give a name to the plugin
	$.fn.confirm = function(userObject,callbackFunction){

	}
}) (jQuery);

2. mergin the user object to the default object

Mergin of objects in important if u want some information for the user like message or headline etc for this we will use $.extend(object1,object2); method
defaultObject = {
			'headline': 'Confirm Box',
			'message': 'Are you sure'
		};
		//merging the content of the userObject in to default object
		var option = $.extend(defaultObject, userObject);

3. Inserting the the confirm box markup dynamiclay to the DOM (page)

var markup = '<div class="confirm-h">
<button class="close">x</button></div>
<div class="confirm-b">
</div>
<div class="confirm-f">
<button class="btn btn-grey" id="cancel-btn">Cancel</button><button class="btn btn-blue" id="function-btn">Ok</button></div>
',
			$this = $(this);

		//create a overlay div
		var createOverlay = '<div class="overlay">
</div>
';
		$(createOverlay).appendTo('body');
		//add confirm box class to the div
		$this.addClass('confirm-container');

		//append markup to the confirm box
		$(markup).appendTo($this);

4. making a reference variable

//list header footer and body in variables
		var headerTitle = $('.confirm-h span',$this),
			closeBtn = $('.confirm-h button.close', $this),
			body = $('.confirm-b p',$this),
			cancelBtn = $('.confirm-f #cancel-btn', $this),
			okBtn = $('.confirm-f #function-btn', $this),
			windowW = $(document).width(),
			windowH = $(document).height(),
			boxW = $this.width(),
			boxH = $this.height(),
			overlay = $('.overlay');

4. Final touch UP

in this part we will create a closeBox() function and give work to every element of the confirm box an finaly show the box
//close box function
		function closeBox(){
			$this.slideUp(500);
			overlay.fadeOut();
			$this.removeClass("confirm-container");
			$this.html('');
		}
		headerTitle.text(option.headline);
		body.text(option.message);
		okBtn.click(callbackFunction);
		cancelBtn.click(closeBox(););
		closeBtn.click(closeBox(););
		//center the box
		$this.css('left', (windowW/2) - (boxW/2)).css('top', '150px');
		//show the box and overlay
		overlay.fadeIn();
		$this.slideDown(500);

Putting it all together

html

<!DOCTYPE html>
<html>
<head>
<title>Custom conform box</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
	<div id="container">	
		<h2>Custom Confirm box by hunklessons.blogspot.com</h2>
		<button id="launchConfirm">Launch confirm box</button>
		<div id="hello"></div>
	</div>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="alert.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	//lauch confirm box
	$('#launchConfirm').click(function(){
		$('#hello').confirm({
			'headline': 'Alert box by hunklessons',
			'message': 'Are you husain saify'
		},function(){
			alert('This is a callback function');
		});
	});
});
</script>
</body>
</head>
</html>

Css

We have dicused the css in the above part

jQuery

//plugin for custom confirm
(function($){
	//confirm version 1.0
	$.fn.confirm = function(userObject,callbackFunction){

		defaultObject = {
			'headline': 'Confirm Box',
			'message': 'Are you sure'
		};
		//merging the content of the userObject in to default object
		var option = $.extend(defaultObject, userObject);

		var markup = '<div class="confirm-h"><span></span><button class="close">x</button></div><div class="confirm-b"><p></p></div><div class="confirm-f"><button id="cancel-btn" class="btn btn-grey">Cancel</button><button class="btn btn-blue" id="function-btn">Ok</button></div>',
			$this = $(this);

		//create a overlay div
		var createOverlay = '<div class="overlay"></div>';
		$(createOverlay).appendTo('body');
		//add confirm box class to the div
		$this.addClass('confirm-container');

		//append markup to the confirm box
		$(markup).appendTo($this);

		//list header footer and body in variables
		var headerTitle = $('.confirm-h span',$this),
			closeBtn = $('.confirm-h button.close', $this),
			body = $('.confirm-b p',$this),
			cancelBtn = $('.confirm-f #cancel-btn', $this),
			okBtn = $('.confirm-f #function-btn', $this),
			windowW = $(document).width(),
			windowH = $(document).height(),
			boxW = $this.width(),
			boxH = $this.height(),
			overlay = $('.overlay');

		//close box function
		function closeBox(){
			$this.slideUp(500);
			overlay.fadeOut();
			$this.removeClass("confirm-container");
			$this.html('');
		}
		headerTitle.text(option.headline);
		body.text(option.message);
		okBtn.click(callbackFunction);
		cancelBtn.click(closeBox(););
		closeBtn.click(closeBox(););
		//center the box
		$this.css('left', (windowW/2) - (boxW/2)).css('top', '150px');
		//show the box and overlay
		overlay.fadeIn();
		$this.slideDown(500);

	}
}) (jQuery);

Wednesday, April 2, 2014

Simple DropDown Menu with Keyboard operation shortcut

Hi Guyz in this tutorial i am going to teach u how to create a DropDown menu with Keyboard operation shortcut. U can press enter to operate the dropDown menu . Its very easy and basic it have just few lines of code and a beatuful interface
Live View Download Watch Video Tutorial

html

html has nothing special special
<div class="Dropdown">
 <span>My Account <img src="./arrow.png"></span>
  <div class="submenu">
   <ul>
      <li>Profile</li>
      <li>Settings</li>
      <li>Find Friends</li>
      <li>Feedback</li>
      <li>Signout</li>
   </ul>
  </div>
</div>

Css

body{
 background-color: #e4e8ec;
 font-family: arial;
}
.Dropdown{
 background: #FFF;
 padding: 5px 10px;
 border-radius: 10px;
 width: 120px;
 box-shadow: 2px 2px 5px #ccc;
 cursor: pointer;
}
.Dropdown span img{
 margin-bottom: 5px;
 margin-left: 20px;
}
.submenu ul{
 display: none;
 margin: 0;
 padding: 0;
 list-style: none;
 margin-top: 5px;
 background: #fff;
}
.submenu ul li{
 padding: 5px;
}
.submenu ul li:hover{
 background:#155FB0;
 color: #FFFFFF;
 cursor: pointer;
}

javaScript

In this file when class Dropdown is clicked we toggle the dropbox and when the enter button is clicked we also toggle dropbox on and off
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
 $(document).ready(function() {
  //when dropdowm button is clicked
  $('.Dropdown').click(function(){
   $('.submenu ul').toggle();
  });
  //when a key press in the document
  $(document).keyup(function(e) {
   var key = e.keyCode;
   if(key == '13'){
    $('.submenu ul').toggle();
   }
  });
 });
</script>

Saturday, March 29, 2014

hunkScrollTop.js a jQuery plugin by Husain Saify

hey guyz in this post i am going to give you a  hunkScrollTop.js jQuery plugin created by Husain Saify (hunk). Its a light weight plugin with make a dynamic ScrollTop button for your website.


View Demo Download

html

to use the hunkScrollTop.js you just need to add the jQuery & hunkScrollTop.js in the head
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="hunkScrollTop.js"></script>

js

To use hunkScrollTop.js you just need to call the hunkScrollTop with html and give the speed in parameters $('html').hunkScrollTop(1000);
<script type="text/javascript">
  $(document).ready(function(){
    $('body').hunkScrollTop(1000);
  });
</script>

Thursday, March 20, 2014

Create a Facebook app For free with Dropbox

Hii guyz in this post i am going to teach you how to create a facebook app for free with dropbox. As we all know facebook demand a Secure canvas url SSL https://. Hosting and SSL cost money i find a solution of  that problem which is for free.

Watch at youtube & Subscribe hunklesson


Thursday, March 6, 2014

Facebook like Profile Tooltip with jQuery, Ajax, Php & Mysql

In this post i am going to tell u guz how to create a facebook like profile tooltip with jQuery php & mysql. Probably this will be my last post till 26 march 2014 because currently my paper are going on and i get very less time for my girlfriend (computer) so we will meet after 26 march 2014 byeeeeeee
facebook like profile tooltip with jQuery and php

Live Demo Download

Database

first we are creating a database with general info id, fullname, headline, avatar, cover
CREATE TABLE IF NOT EXISTS `fb_profile_tooltip` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fullname` varchar(255) NOT NULL,
  `headline` varchar(255) NOT NULL,
  `avatar` text NOT NULL,
  `cover` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

html

Html is having nothing special we just have html5 data-id (anchor tag) with store the user id and that id will be fetched by jQuery and send to php to create a dynamic profile tooltip & a div with a class p-tooltip
 <div class="p-tooltip"></div>
 <table cellpadding="20">
  <td><a href="#" class="profile" data-id="1">user1</a></td>
  <tr>
  <td><a href="#" class="profile" data-id="2">user2</a></td>
  <tr>
  <td><a href="#" class="profile" data-id="3">user3</a></td>
  <tr>
  <td><a href="#" class="profile" data-id="4">user4</a></td>
 </table>

css

body{
font-family: verdana;
}
a{
text-decoration: none;
}
.p-tooltip{
background: #fafbfb;
border: 1px solid #BFBFBF;
width: 320px;
margin: 0 auto;
border-radius: 5px;
position: absolute;
display: none;
padding: 0 0 10px 0;
}
.p-tooltip-cover img{
width: 100%;
height: 120px;
}
.p-tooltip-avatar{
text-align: left;
margin-top: -45px;
margin-left: 10px;
}
.p-tooltip-avatar img{
height: 75px;
width: 75px;
padding: 4px;
background: #fff;
border: 1px solid #ccc;
cursor: pointer;
}
.p-tooltip-info{
text-align: left;
}
.p-tooltip-info .p-username{
float: none;
margin-top: -70px;
margin-left: 100px;
font-size: 14px;
font-weight: bold;
color: white;
}
.p-tooltip-info .p-headline{
float: none;
margin-top: 6px;
margin-left: 100px;
font-size: 12px;
color: black;
}
.p-tooltip-button{
float: right;
margin-top: 5px;
}
.p-tooltip-button button{
cursor: pointer;
}

jQuery
we have created two function 1> showProfileTooltip(); 2> hideProfileTooltip(); when user mouseover a link we use the showProfileTooltip(); this function get the data from a get_profile.php and feed the data in the p-tooltip div and when the mouseleave we use hideProfileTooltip();
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){
  function showProfileTooltip(e, id){
   var top = e.clientY + 20;
   var left = e.clientX - 10;

   $('.p-tooltip').css({
    'top':top,
    'left':left
   }).show();
   //send id & get info from get_profile.php
   $.ajax({
    url: 'get_profile.php?id='+id,
    beforeSend: function(){
     $('.p-tooltip').html('Loading..');
    },
    success: function(html){
     $('.p-tooltip').html(html);
    }
   });
  }

  function hideProfileTooltip(){
   $('.p-tooltip').hide();
  }

  $('.profile').mouseover(function(e){
   var id = $(this).attr('data-id');
   showProfileTooltip(e, id);
  });

  $('.p-tooltip').mouseleave(function(){
   hideProfileTooltip();
  });
 });
 </script>

get_profile.php

in this page we are getting the user id and generating the content based on its uniquer id fetched from the html5 data-id anchor tag
<?php
 //include '../connect.php';
 ///connect to mysql 
 mysql_connect('localhost','root', '');
 mysql_select_db('hunklessons');
 //get the user id
 if(isset($_GET['id']) && !empty($_GET['id'])){
  $id = mysql_real_escape_string($_GET['id']);
  //get info of user based on user id
  $q = mysql_query("SELECT * FROM fb_profile_tooltip WHERE id='$id'");
  if(mysql_num_rows($q) == 1){
   $row = mysql_fetch_assoc($q);
   //output the html
   ?>
  <div class="p-tooltip-cover">
   <img src="<?php echo $row['cover'];?>">
  </div>
  <div class="p-tooltip-avatar">
   <img src="<?php echo $row['avatar'];?>" />
  </div>
  <div class="p-tooltip-info">
   <a target="_blank" href="http://hunklessons.blogspot.in/" style="text-decoration:none;">
    <div class="p-username"><?php echo $row['fullname'] ?></div>
    <div class="p-headline"><?php echo $row['headline'] ?></div>
   </a>
  </div>
  <div class="p-tooltip-button">
   <a target="_blank" href="http://hunklessons.blogspot.in/">
    <button>Add as friend</button><button>message</button>
   </a>
  </div>
   <?php
  }else{
   echo "Error";
  }
 }
?>

Wednesday, February 26, 2014

jQuery Tooltip plugin

Today i have created my first jQuery plugin and named it hunkTooltip . This is a simple & lightweight plugin you do not need to add some more in html markup just u have to select the element with jQuery.
Tooltip plugin by husain saify

Download Script View Demo 

html

To use this plugin you do not need any thing new in the html just the same markup like before just one thing is important title attribute the plugin fetch the text from the title and display it

Javascript

to use the hunkTooltip plugin you just need to select the element call the call $(element).hunkTooltip(); this function will attach the hunkTooltip plugin to that element

 <script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="jquery.hunkTooltip.min.js"></script>
 <script type="text/javascript">
 $(document).ready(function(){
  $('a').hunkTooltip(); //call the hunkTooltip plugin
 });
 </script>

Like Us on Facebook / Add Hunk As Friend


Monday, February 17, 2014

Css3 Progress Bar

Now a days progress bar is a very important in any social media website and progress bar is very easy to create with just a few lines of css and some beautifull css3 transition
CSS3 Progress Bar

Download Script View Demo 

html

html is very simple it just have a div with class .bar and a span inside the div
<div class="bar">
 <span style="width:0%;"></span>
</div>

css

//style of div
.bar{
 width: 100%;
 background: #eee;
 padding: 3px;
 border-radius: 3px;
 box-shadow: inset 0px 1px 3px rgba(0,0,0,0.2);
}
//style of span
.bar span{
 height: 20px;
 background: cornflowerblue;
 display: block;
 border-radius: 3px;
 -webkit-transition: width 0.8s ease;
 -moz-transition: width 0.8s ease;
 transition: width 0.8s ease;
}

Like Us on Facebook / Add Hunk As Friend