Archive for category HTML
Upload image using hidden iFrame (Simple Photo Manager)
Posted by admin in HTML, JavaScript, PHP on May 1st, 2009
You can view the working demo here.
You can download the source code here.
In this example we are using a hidden IFrame for uploading file without refreshing page.
Include IFRAME in your page.
<div id="iframe_container"> <iframe src="pm-upload.php" frameborder="0" style="height:75px;"></iframe> </div>
The pm-upload.php has a FROM with file input field. When user select a file for upload we have called a upload() function on onChange event of file element.
<form name="iform" action="" method="post" enctype="multipart/form-data"> <input id="file" type="file" name="image" onChange="window.parent.upload(this);" /><br> <span style="font-size:11px; color:#666666;">only gif, png, jpg files.</span> <input type="hidden" value="" name="div_id" /> </form>
If file successfully upload and satisfy all validation conditions then we call setUploadedImage() function to setup the information of recently uploaded file on parent window. we can access the parent window javascript functions like this:
window.parent.setUploadedImage();
If there is some validation error then we call uploadError() function of parent window.
Here is complete javascript code for this tutorial:
//Call at the time of upload function upload(fileObj){ var par = window.document; var frm = fileObj.form; var div_id = parseInt(Math.random() * 100000); // hide old iframe var iframes = par.getElementsByTagName('iframe'); var iframe = iframes[iframes.length - 1]; iframe.className = 'hidden'; // create new iframe var new_iframe = par.createElement('iframe'); new_iframe.src = 'pm-upload.php'; new_iframe.frameBorder = '0'; new_iframe.style.height = '75px'; par.getElementById('iframe_container').appendChild(new_iframe); // add image progress var images = par.getElementById('images_container'); var new_div = par.createElement('div'); new_div.id = div_id; var new_img = par.createElement('img'); new_img.src = 'images/indicator2.gif'; new_img.style.marginLeft = '33px'; new_img.style.marginTop = '50px'; new_div.appendChild(new_img); images.appendChild(new_div); var errorDiv = par.getElementById('error'); errorDiv.innerHTML = ""; errorDiv.style.display = 'none'; // send frm.div_id.value = div_id; setTimeout(frm.submit(),5000); } //Call when upload completed function setUploadedImage(imgSrc, fileTempName, divId) { var par = window.document; var images = par.getElementById('images_container'); var imgdiv = par.getElementById(divId); var image = imgdiv.getElementsByTagName('img')[0]; imgdiv.removeChild(image); var image_new = par.createElement('img'); image_new.src = imgSrc; image_new.className = 'pic'; var image_label = par.createElement('input'); image_label.type = "text"; image_label.maxLength = "40"; image_label.size = "12"; image_label.value = "Label"; image_label.name = "title[]"; var image_hidden = par.createElement('input'); image_hidden.type = "hidden"; image_hidden.value = "1"; image_hidden.name = "delFlag[]"; var image_name = par.createElement('input'); image_name.type = "hidden"; image_name.value = fileTempName + ",new"; image_name.name = "imageName[]"; var image_del_link = par.createElement('a'); image_del_link.href = "javascript:void(0)"; image_del_link.appendChild(par.createTextNode("Delete")); var br = par.createElement('br'); imgdiv.appendChild(image_new); imgdiv.appendChild(image_label); imgdiv.appendChild(image_hidden); imgdiv.appendChild(image_name); imgdiv.appendChild(br); imgdiv.appendChild(image_del_link); image_label.onfocus = function() { eval(labelOnFocus(image_label)); } image_label.onblur = function() { eval(labelOnBlur(image_label)); } image_del_link.onclick = function() { eval(deleteLinkOnClick(image_del_link, '')); } } // call when error occurred at the time of upload function uploadError(divId, oName) { var par = window.document; var images = par.getElementById('images_container'); var imgdiv = par.getElementById(divId); images.removeChild(imgdiv); var errorDiv = par.getElementById('error'); errorDiv.innerHTML = oName + " has invalid file type."; errorDiv.style.display = ''; } function labelOnFocus(image_label) { if (image_label.value == "Label") { image_label.value = ""; } } function labelOnBlur(image_label) { if (image_label.value == "") { image_label.value = "Label"; } } function deleteLinkOnClick(delLink, delFlag) { var par = window.document; var imgDiv = delLink.parentNode; var image_hidden = delFlag == '' ? imgDiv.childNodes[2] : par.getElementById(delFlag); if (image_hidden.value == '1') { image_hidden.value = '0'; delLink.removeChild(delLink.childNodes[0]); delLink.appendChild(par.createTextNode("Restore")); delLink.style.color = '#FF0000'; } else { image_hidden.value = '1'; delLink.removeChild(delLink.childNodes[0]); delLink.appendChild(par.createTextNode("Delete")); delLink.style.color = '#64B004'; } }
You can check the live demo of this tutorial and you can also download the source code of the sample demo. The demo example is very simple and only for learning purpose. You can easily extent the sample code as per your requirement.
How know recipient read the email or newsletter?
Sometimes we send the newsletters or emails to lot of peoples and we want to know how many of them read it. Normally the websites like Gmail, Yahoo, Hotmail don’t provide any facility for this purpose. But we can know that by sending HTML emails.
HTML Emails – HTML emails are emails which can contain HTML tags in email body. Like <table>, <tr>, <td>, etc.
The main thing here is we need to send some information to our server when user open the email. So for this, we can place an image in email body and when user open this email the browser send request to our webserver for this image. We can also achieve this by adding javascript code or hidden iframe but normally email clients don’t support javascript code and iframes in email body and they strips out this unwanted code form email body. So, the best way to achieve this is by using images.
<?php /*send_newsletter.php*/ $newsletterid = 25; // array of recipients $recipients = array('test1@yahoo.com', 'test2@yahoo.com', 'test3@yahoo.com', 'test4@gmail.com', 'test5@gmail.com', 'test6@hotmail.com', 'test7@hotmail.com', 'test8@aol.com', 'test9@aol.com', 'test10@yahoo.com'); // subject $subject = 'Newsletter for April'; // message $message = " <html> <head> <title>Newsletter for April</title> </head> <body> <p>This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. This is a Demo Newsletter. </p> <img src='http://www.akchauhan.com/newsletter_read.php?newsletterid={$newsletterid}' height='1' width='1' /><br/><br/> Thanks,<br/> www.akchauhan.com </body> </html>"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "rn"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn"; $headers .= 'From: AKChauhan Blog <anil@akchauhan.com>' . "rn"; foreach($recipients as $to) { // Mail it mail($to, $subject, $message, $headers); } ?>
I have used a variable $newsletterid to track newsletter. We can also send some extra information in query string as per our requirement.
Like
http://www.akchauhan.com/newsletter_read.php?newsletterid=$newsletterid&userid=$userid
Note: Here I have used mail() function only to make demonstration simple. mail() function is not suitable for sending large number of email in loop. Better we use PEAR::Mail or PEAR::Mail_Queue packages for sending large number of email.
<?php /*newsletter_read.php*/ $newsletterid = $_GET['newsletterid']; /*Write some code to update your read counter in your database. Or, do whatever you want to do when user open your newsletter.*/ mysql_query('UPDATE info SET newsletter_count = newsletter_count + 1 WHERE newsletterid = ' . $newsletterid); // We'll be outputting a JPEG header('Content-Type: image/jpeg'); $im = @imagecreatefromjpeg('images/one_pixel_small_white_image.jpg'); imagejpeg($img); imagedestroy($img); ?>
In above code, first we updated our newsletter read counter in database. We can do other things also like send an email to admin etc. as per requirement. Then we set the Content-Type to image/jpeg as we are going to send a jpeg image. So, we need to tell the browser about the content type of our response. Then, we created a 1 pixel white image usng GD library and outputted it to browser using imagejpeg() function.
Create CSS Stylesheet and JavaScript for Print
Posted by admin in CSS, HTML, JavaScript on April 13th, 2009

CSS Stylesheet for Print
When we want to print out HTML page we need a lighter version of our HTML page. Basically, we don’t want to print the background images, background colors, advertisements and other things which are not relevant in printing. For this purpose, we need to specify a CSS stylesheet which browser use when we print our HTML page. We can also change the width and layout of our HTML page. We can specify our CSS stylesheet for print like this.
<head> /*External style sheets*/ <link rel="stylesheet" href="css/print.css" type="text/css" media="print" /> </head>
<head> /*Embedded style sheets*/ <style type="text/css" media="print"> .noprint { display:none; } </style> </head>
The important thing to note here is the media descriptor media=”print” which we are giving through media attribute. The browser knows using this media descriptor that this CSS stylesheet is defined to use at the time of print. We can define CSS stylesheet for SCREEN like this.
<head> /*External style sheets*/ <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" /> </head>
<head> /*Embedded style sheets*/ <style type="text/css" media="screen"> .header { width:800px; height:200px; } </style> </head>
In the following example, I don’t want to print the “Print this Coupon” link in coupon. So, I have just added the class “noprint” and set the display property none in this class. Now, I will apply this class to all the elements which I don’t want to print. You can easily create other classes for different purpose like to change the background color of element in print.
<!--coupon.html--> <html> <head> <title>CSS stylesheet for Print</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" /> <link rel="stylesheet" href="css/print.css" type="text/css" media="print" /> </head> <body> <div class="coupon-mini"> <table width="500" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="146" align="left" valign="top"><div class='default-coupon'><img src='/sites/default/files/coupon_116_116.'></div></td> <td width="354" align="left" valign="top"> <h3 style="font-family: 'palatino linotype',palatino; line-height:18px; padding:6px 0 0 0; color:#0e549c; "> Divan Restaurant and Hookah Lounge, Alabama </h3> <div class='coupons-text1'><span>McDonald 20% Off.</span><p>McDonald 20% Off.</p></div><br /> <div style="clear:both;"></div> <div class="detail-basic1"> 3125 Piedmont Rd NE, Atlanta, Alabama<br> <strong> Phone Number :</strong> (404) 365-0410<br/> <strong> Pincode:</strong> 30305 <div class="openhr1"><strong> Opening Hours:</strong> 6:30 PM to 01:00 AM</div> <div class="web1"> <div class="lft1"><a href="#" class="about-web-link">http://www.divanatlanta.com</a></div> <div class="rght1"><a href="#" class="about-mail-link">ajay0878@yahoo.com</a></div> </div> </div> </td> </tr> </table> </div> <div class="print noprint"><a href="javascript:window.print();" class="coupon-print-link">Print this Coupon</a></div> </body> </html>
We have also added the window.print() function on Print this Coupon link. This function print the current windows content. We can also print our page through File->Print… menu. But this function is useful when the toolbar and menubar are disabled.
