AJAX by examples: PHP Email with dynamic attachment

Introduction

One of the main advantages of AJAX is the ability to interact with the web server without loosing your current screen state.

Here is a real life example:

Let’s say you are working with your favorite web application. You have filled out bunch of form fields to generate a nice long report and you need to email this page as you see it to someone as an attachment –a Word document!

You can probably think of a number of ways to get this done, but here is an AJAX way – do it with a click of a button, staying exactly where you are and without launching external applications. Sounds interesting? Let’s get busy.

$2.89
- OR -

Overview

Interface Adjustments

The great thing about the described approach is that you can easily apply it to your existing application.
You can start with an existing page or create a new one and use either real or dummy data for it.

If you are working with an existing example you need to make a couple of adjustments to your application interface:

  1. Add the block of UI elements required to send an email – the Email button with the optional To and Subject fields.
  2. As you want to be able to email the current view as an attached document you would probably like to remove the form fields used for the search and email from the output (there is no point in having this content in the document). You need to enclose this block along with the Report search form fields in a separate block element (a div tag) with a set id attribute (so we can refer to it through the DOM).

    If you are starting from scratch (whether you are working with a real or dummy data) you have to account for this as well.

As a result your typical view would include the following:

1.       The form fields for searching and filtering.

2.       The forms fields for emailing (which I named To and Subject)
These can be either text fields – to let the user enter the parameters, or hidden fields if you want the parameters hard-coded.

3.       The button that will trigger the Email function.

4.       The content – the HTML table or datagrid (or any other type of data).

Alex July

Alex JulyAlex July is a Vancouver-based (Canada, British Columbia) Web Developer/ Graphic Artist who has an extensive experience in both creative realms.
He is also a host of Linecraft.com where he is showcasing his skills and sharing experience with the developers community. For the past 3 years Alex has been focusing on the development of Rich Internet Applications using Macromedia Flash technology.

When away from the computer Alex is practicing Martial Arts, playing guitar and enjoying time with his wonderful family.

See All Postings From Alex July >>

Reviews

Be the first to write a review

You must me logged in to write a review.