Archive | Code RSS for this section

Parsing prototype’s responseXML in JavaScript

Here’s a quick code snippet to parse the responseXML using JavaScript.

I am printing the xml file using php, feel free to use your server side scripting language.

First make a quick Ajax.Request call to send the data to server file.

function AddItem() {

 
var input = ‘myinput=’+$F(‘myinput’);
var list = ‘ListID=’+$F(‘ListID’);
var user = ‘userID=’+$F(‘userID’);

var pars = input+’&’+user+’&’+list;

new Ajax.Request(

‘GetItem.php’,
 { 
  asynchronous:true,
  parameters:pars,
  onComplete: ShowData 
  
 }
);
$(‘myform’).reset();
$(‘myinput’).activate();
return false;

}

Now that we have sent the data to our server file, in the above case its GetItem.php

Print the xml file in the php side.

Collect the reponse from the server in the form of the responseXML.

Below is the code to parse the responseXML and picking it up in javascript DOM.

function ShowData(originalRequest) {
 
var xmlDoc = originalRequest.responseXML.documentElement;

var value = xmlDoc.getElementsByTagName(“ItemValue”)[0].childNodes[0].nodeValue;
var value1 = xmlDoc.getElementsByTagName(“ItemID”)[0].childNodes[0].nodeValue;

divID = ‘DIV’+value1;

var div = document.createElement(‘div’);
div.className =’ItemRow’;
div.id = divID;

var val = ‘”‘+value+'”‘;

var i = document.createElement(‘input’);
i.type=’checkbox’;
i.id=value1;
i.value=value;
i[“onclick”] = new Function(“MarkDone(this.id)”);
var t = document.createTextNode(value);

div.appendChild(i);
div.appendChild(t);

$(‘ItemTree’).appendChild(div);

new Effect.Highlight($(div));
}

This has worked perfectly for me, let me know if you run into issues with this code.

Cheerz

Advertisements

Learn how to play with Flash, Mysql & PHP

Its one of those cool tutorials I have ever come across. It helps us in understaning how to integrate and play with Flash, Mysql and PHP.

I am sure it would help you too.

http://www.creativecow.net/articles/brimelow_lee/php_mysql/php_mysql_flash.html

Cheerz
SHri

If you are reading article, you might also be interested in the PHP and Scriptaculous Book I have authored.

The book gives you insights about effects, drag-n-drop, slideshows, applications, auto-completion, in-place editing and more. Complete code snippets and explanations.

CHECK out and BUY the PHP and Script.aculo.us book at Packt official site

PHP and Script.aculo.us Book

PHP and Script.aculo.us Book

http://www.packtpub.com/php-and-script-aculo-us-web-2-0-application-interface/book

Advanced Word Count for PHP!!!!!

If you have tried counting words in a string in PHP, you sure must have used “explode” function.

Its a pretty simple function and can be used in N-ways. A general simple function can be written as


$String = "This is my blog";
$words = explode(" ",String);
$num = count($words);

Now, this should give you the number of words (4) and following with the count of number of words{This, is, my, blog}.

BUT, the real problem is if your string is “Hello     World”, the number of words here are 7 and count is also 7. How come? THats coz, of the fact the function explode takes delimiter as a space and will count the spaces, commas, etc also as words.

I have found a better way of doing the same. So here I am sharing with you.

Instead use this function, in which we check wheather the word is having any integer, charcter etc in it or not. If its a space we can royally ignore it 🙂

function wordCount($string){
     $words = “”;
     $string = eregi_replace(” +”, ” “, $string);
     $array = explode(” “, $string);
     for($i=0;$i < count($array);$i++)    {          if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i]))              $words[$i] = $array[$i];      }      return $words;  } [/sourcecode] In this way, we can get the words as the user must have typed omitting the spaces and junk characters. This function will return you a array of words in the string, and further we can even make it only Words++ to count how many words are there. So this function can actually work for 2 purposes. 1. Count the number of Words in a string 2. Get the words in array Hope you find this useful. CHeerz Shri If you are reading article, you might also be interested in the PHP and Scriptaculous Book I have authored.

The book gives you insights about effects, drag-n-drop, slideshows, applications, auto-completion, in-place editing and more. Complete code snippets and explanations.

CHECK out and BUY the PHP and Script.aculo.us book  at Packt official site
mybook

http://www.packtpub.com/php-and-script-aculo-us-web-2-0-application-interface/book

Why should bloggers have all the fun????

And, when I use the word bloggers here, it refers to people who are using http://blogger.com

After the launch of Google indic translator, now bloggers can easily type in Hindi but what about WordPress users? <wink>I perosnally love wordpress platform</wink>

So, now Im trying to create a plugin by integrating my project OpenKey with TinyMCE{ the editor which WP is using}

Yes, we already have one such thing, thats from Indic Joomla group. But that doesnt fill the gap. So Im trying to that 🙂

For now, you take a look at how it would look when we are using it.

Hindi Text

The picture you are looking at is TinyMCE editor being hacked for indic purpose.

What do u think about it? Would it be useful for u? Drop in ur comments

Cheerz

Shri

30 Day Project : Starts Today!!!

“Leave complex problems to BIG people, try to solve simple-small issues”

Yes, thats exactly what I am starting today. NO big deal. Just simple applications.

To start with today I got hooked with a small problem. So I got this idea.

I wont reveal much of what the app will be, what it shud be, or how I wud be wasting my time 🙂

The idea is to get something UP & Running,Quickly, very Quickly and then can keep on improving it!!!!

So write to u in detail on 15th September!!!

Keep guessing!!!!
Cheerz
Sridhar

<Hint>No Rocket Science, it has something to do with IndicComputing!!!!! </Hint>