Dec
20
Posted on 20-12-2006
Filed Under (AJAX ) by jay

This is the Uber-Web 2.0 function of the Year, In Place Editing. This function is mostly associated with Ruby and other 'fluffy' web apps but once you figure out how easy it is to add to an app you'll be amazed.

In this short tutorial I will show you how to add InPlaceEditing to an Unordered List.

This tutorial is built using the Script.aculo.us Library and PHP5.

Most of the examples I found are for Ruby but I did find one other that helped me. Even though this topic has been covered I feel it is my duty to help spread the knowledge.

Demo

Step 1: The SQL
Start by creating a table to hold a list of data in. For this tutorial I have a simple MySQL Data Table that has 4 columns: ID, Name, Description and Updated.

SQL:
  1. CREATE TABLE `tblInLineEdit` (
  2.   `id` int(11) NOT NULL AUTO_INCREMENT,
  3.   `name` varchar(20) NOT NULL DEFAULT '',
  4.   `desc` varchar(250) DEFAULT NULL,
  5.   `updated` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  6.   PRIMARY KEY  (`id`),
  7.   KEY `name` (`name`)
  8. ) TYPE=InnoDB AUTO_INCREMENT=5 ;

Step 2: The Main Page
Create a file that will display the list.

Add the script.aculo.us library:

CODE:
  1. <!--scriptacilous lib-->
  2. <script type="text/javascript" src="scriptaculous/prototype.js"></script>
  3. <script type="text/javascript" src="scriptaculous/effects.js"></script> 
  4. <script type="text/javascript" src="scriptaculous/controls.js"></script>
  5. <script type="text/javascript" src="scriptaculous/builder.js"></script>
  6. <script type="text/javascript" src="scriptaculous/dragdrop.js"></script>
  7. <script type="text/javascript" src="scriptaculous/slider.js"></script>
  8. <script type="text/javascript" src="scriptaculous/scriptaculous.js"></script>

Get Some data from the database:

PHP:
  1. <?php
  2.  
  3. //open a connection
  4. $mysql = new PDO('mysql:host=[SERVER];dbname=[DATABASE]','[USERNAME]','[PASSWORD]');
  5.  
  6. //select some data
  7. $query =$mysql->prepare("SELECT * FROM tblInLineEdit");
  8.            
  9. $query->execute();
  10. $i=0;
  11.    
  12. //bind it to an Associate Array
  13. foreach($query->fetchAll(PDO::FETCH_ASSOC) as $r ){
  14.     $results[$i]=array("id"=>$r["id"],"name"=>$r["name"]);
  15.     $i++;
  16. }     
  17. $mysql = null
  18.  
  19. ?>

Create FORM and and display the list of items:

CODE:
  1. <form action="InPlaceEdit.php" method="GET">
  2. <?php
  3.     if($results != null){
  4.         echo "<ul class='links'>\n";
  5.         foreach($results as $item){   
  6.             echo "<li id='item_".$item['id']."'>".stripslashes($item['name'])."</li>\n";
  7.         ?>
  8.             <script type='text/javascript'>
  9.                 new Ajax.InPlaceEditor('item_<?php echo $item['id'];?>', 'ajaxInPlaceEditFunctions.php',{callback: function(form, value) { return 'name=' + escape(value)+'&id=<?php echo $item['id'];?>'}});
  10.             </script>
  11.         <?php                  
  12.         }
  13.         echo "</ul>";      
  14.     }
  15. ?>
  16. </form>

In the foreach we are creating a list of items:

CODE:
  1. echo "<li id='item_".$item['id']."'>".stripslashes($item['name'])."</li>\n";

.

Then we create a JavaScript block for each list item:
Go here a complete explanation of how Ajax.InPlaceEditor.

CODE:
  1. <script type='text/javascript'>
  2. new Ajax.InPlaceEditor('item_<?php echo $item['id'];?>', 'ajaxFunction.php',{callback: function(form, value) { return 'name=' + escape(value)+'&id=<?php echo $item['id'];?>'}});
  3. </script>

Step 3: The Data Updater
Now create a new file that will perform the data transaction. This file is referenced in the Ajax.InPlaceEditor as ajaxFunction.php.

PHP:
  1. <?php
  2. //chack to see if a name and id are in the FROM POST
  3. if($_POST['name'] != '' && $_POST['id']){
  4.    
  5.     //assign the POST data to variables
  6.     $itemname = $_POST['name'];
  7.     $itemid = $_POST['id'];
  8.    //open a DB connection
  9.     $mysql = new PDO('mysql:host=[SERVER];dbname=[DATABASE]','[USER]','[PASSWORD]');                   
  10.    //prepare the SQL UPDATE 
  11.     $query = $mysql->prepare("UPDATE tblInLineEdit SET ".
  12.         " name = ?,".   
  13.         " updated = ? ".       
  14.         " WHERE id = ?");   
  15.    //execute the UPDATE with the POST Data 
  16.     $query->execute(array($itemname,getdate(),$itemid));
  17.    //kill the connection
  18.     $mysql = null;
  19.  
  20.    //echo the name
  21.     echo $itemname
  22. }
  23. ?>

And that's it.

Lastly
This is just a quick overview of a very powerful function. This example only deals with existing data not does not get into INSERTING data but if you clever you'll see that not much needs to be tweaked to add a new item to the list.

Demo

    Read More   

Comments

Cool on 20 February, 2007 at 10:06 am #

Thanks this helped alot!



Post a Comment
Name:
Email:
Website:
Comments: