Monday, April 4, 2011

Creating widget with Javascript and PHP

Hi,

For those who do not know, a widget can be considered as a small section in your web page, whose content is being rendered from a different website. To understand this, google adsense is a very good example of a widget. In Adsense, you are provided with a javascript. All you need to do is to copy and paste the code in any part of your website to generate the advertisement. Now how the advertisement is getting generated, is not your concern.

But you need to configure the google adsense according to your need, e.g. the dimension of the adsense. After you are done with configuring it you are given a javascript code, as I mentioned above. Which you just need to copy and paste in your website or blog.

Click here to download the entire source code of the example given below, with full database.

So the basic things that we need to program for creating a widget is as:
1. A configuration section where the user will configure the widget, which may include color, title etc. as per your needs
2. Most importantly the embeddable code, which the end user will copy and paste after their configuration is done.

How ever there is one more thing needed from a programmers perspective, that is the content of the widget. The content of the widget is completely dependent on your business logic and the configuration data that the end users have set.

Example:

Consider I have a website for doctors and their details. Now I decide to create widgets, so that the end users can embed them in their website or blog, which would show doctor details from my website. First I need to provide the end users with a user interface that will configure the widget as follows:



The above configuration section will be a part of my website. Whenever the user is changing some value, a auto generated HTML code is being generated in the textarea. Now the end user will just copy and paste the above code in their website or blog. For testing, you can simply copy and paste it in a simple ".html" file and get the following output:

The embedded script just contains a javascript file in your website, then a function,"init_widget()" is called with the parameters set by the end user while configuring the widget. Now the trick here is in the init_widget(). Along with the 2 <script> tags, there is also a div with id="wd_id". Now in init_widget() I am creating an <iframe> with a src in my server along with the various parameters as follows:

document.getElementById("wd_id").innerHTML='<div style="font-family:trebuchet ms;font-size:14px;">'+arguments[4]+'</div><iframe src="http://localhost/php/widgets/widget.php?country='+arguments[2]+'&g='+arguments[3]+'" width="'+arguments[0]+'" height="'+arguments[1]+'" style="border:1px solid #ccc" border="0"></iframe>';

Now in widget.php file I wrote my business logic to fetch doctor details with specific country and gender as specified by the end user. The code in widget.php is:


<table>
<?php
$countries=$_GET['country'];
$gender=$_GET['g'];
$query="select * from doctors where country='".$countries."' and gender='".$gender."'";
$doctors=mysql_query($query);
while($rec=mysql_fetch_array($doctors)){
?>
<tr>
<b><?php echo $rec['name']; ?>(<?php echo ucwords($rec['country']) ?>)</b><br/>
<i><?php echo $rec['description']; ?></i><br/>
<a href="<?php echo $rec['website']; ?>">Website</a><div class="divider"></div>
</tr>
<?php
}
?>

That's it, the widget is complete. Download widget.zip

16 comments:

  1. Very useful walkthrough.
    The code is simple and does the job I've been looking for. Many thanks

    ReplyDelete
  2. Great script! I had doubts on making widgets and took this as example, perfect understanding!

    ReplyDelete
  3. Awesome tuto. You had written such superb script in easiest way. Thanks for sharing awesome post with us. keep it up..

    ReplyDelete
  4. You made my day...Thanks a ton!!! Keep posting such short & neat technical stuff.

    ReplyDelete
  5. Thanks...
    its osum ....

    ReplyDelete
  6. Nice article sir... You are simply awesome... thanks a lot sir. :)

    ReplyDelete
  7. Very good, I have downloaded it and modified the code, perfect !

    ReplyDelete
  8. Although the example is OK please be aware your code is vulnerable to SQL-injection since there is no user-input check whatsoever!

    ReplyDelete
  9. Hi,

    Script is very good and simple. Is there any way to avoid iframe as it is affecting the website shape. I'm looking for something like chat widgets which reside on top of pages, like livezilla, google chat box etc.

    ReplyDelete
    Replies
    1. Yes, you can do it without iframe. The concept is. The embed script will include a js file, as it is doing right now. Create a new function which would make an ajax call on $(document)ready(). Make sure it is a json type call. After the response is received create the HTML of the widget using javascript and append it in the site.

      This is a more tedious task from js perspective.

      Delete
    2. Hi, Thanks for your response. Have you seen some codes any where with this kind of implementation? I have been googling for past couple of days and couldn't find it.

      Delete
    3. Not really. I did one ajax call to external website and made the call a json type. And it worked. But it was not for widget. So I feel with little brainstorming, widget also can be possible.

      Delete
  10. Do you think it is possible te make the same thing without iframe ?

    ReplyDelete
  11. Thank you very much! Example very complete.

    ReplyDelete
  12. Download link is not working. Can anyone upload the project again.

    ReplyDelete