• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

W3 Validator, DOM, byteson and PHP ...

I've uploaded a new byteson example page, based on a single and simple request to a PHP page that uses W3 Validator service, calling a SOAP result, parse them to convert it into an associative array and finally send it with JSON to byteson.

Here is the PHP code

<?php
// byteson filter
if(isset($_POST[yteson])) {

 // http://www.phpclasses.org/browse/package/3512.html
 require FastJSON.class.php;

 // http://www.devpro.it/code/143.html
 require W3validator.function.php;

 // remove magic quotes if th it is present
        // magic_quotes is a problem for JSON strings
 if(get_magic_quotes_gpc())
  $_POST[yteson] = stripslashes($_POST[yteson]);

 // get the output
 $output = FastJSON::encode(W3validator(FastJSON::decode($_POST[yteson])));

 // force new content to download
 header(Content-Type: text/plain; charset=utf-8);
 header(Content-Length: .strlen($output));
 header(Expires: Mon, 26 Jul 1997 05:00:00 GMT);
 header(Last-Modified: .gmdate(D, d M Y H:i:s). GMT);
 header(Cache-Control: no-store, no-cache, must-revalidate);
 header(Cache-Control: post-check=0, pre-check=0, false);
 header(Pragma: no-cache);

 // exit writing JSON object for byteson
 exit($output);
}
?>


This code is above the xHTML output and You can view them by yourself, viewing the example page.

This php page uses two other files, my FastJSON class and my last function on devpro, called W3validator (what a fantasy ...).

The JavaScript code is this one

// new browsers filter
if(document.getElementsByTagName)
onload = function(){ // (C) Andrea Giammarchi

 // assing tabindex and accesskey
 function accesstab(input){
  input.accesskey = input.tabindex = ++accesskey;
  ++tabindex;
 };

 // assign generic link properties
 function addLinkProperties(a){
  tab(a);
  a.href = "#";
  a.onclick = function(){
   var ul = this.parentNode.getElementsByTagName("ul").item(0);
   ul.style.display = !!ul.style.display ? "" : "none";
   return false;
  };
  return a;
 };

 // append one or more elements into another
 // then return the "parent" element
 function append(parent, elements){
  if(elements.constructor !== Array)
   elements = [elements];
  for(var i = 0; i < elements.length; i++)
   parent.appendChild(elements[i]);
  return parent;
 };

 // create a list if there are errors or warnings
 function createList(message, obj){
  for(var a, span, i = 0, key = "", list = [], tmp = []; i < obj.length; i++) {
   for(key in obj[i]) {
    span = node("span");
    span.innerHTML = obj[i][key];
    tmp.push(append(
     node("li"), [
      append(node("strong"), text(key)),
      span
     ]
    ));
   };
   a = addLinkProperties(append(node("a"), text(message.concat(i + 1))));
   list.push(append(node("li"),[a, append(node("ul"), tmp)]));
   tmp = [];
  };
  return append(node("ul"), list);
 };

 // perform the request using byteson
 function doRequest(oldtab){

  // just call the server side page
  // sending input string and adding just
  // one listener ( because I am sure, this application will works "perfectly" :P )
  byteson.request("w3validator.php", inputuri.value, {

   // onload event
   load:function(obj, xhr){
    var a, key = "";

    // remove the loading text
    resultlist.removeChild(resultlist.firstChild);

    // create a list with every key value ...
    for(key in obj) {

     // but not with Arrays (errors or warnings)
     if(obj[key].constructor !== Array) {
      append(
       resultlist,
       append(
        node("li"), [
         append(node("strong"), text(key)),
         append(node("span"), text(obj[key]))
        ]
       )
      );
     };
    };

    // create last list with each error or each warning
    for(key in obj) {
     if(obj[key].constructor === Array && obj[key].length > 0) {
      a = addLinkProperties(append(node("a"), text(key)));
      append(
       resultlist,
       append(
        node("li"), [
         a,
         createList(key.replace(/list/, #), obj[key])
        ]
       )
      );
      a.onclick();
     };
    };

    // enable the form
    inputuri.disabled = senduri.disabled = false;

    // set old tabindex for next request
    // (correct tab navigation ready)
    tabindex = oldtab;
   }
  });
 };

 // create and return an element of specified type
 function node(type){return document.createElement(type)};

 // add tabindex property using global scpe tabindex integer value
 function tab(node){node.tabindex = ++tabindex};

 // create and return a text node with specified content
 function text(value){return document.createTextNode(value)};

 // internal global scope variables
 var accesskey = 0,     // accesskey (WCAG ready ?)
  tabindex = 0,     // tabindex (tab navigation ready)
  output = document.getElementById("request"), // div container
  form = node("form"),    // used form
  inputlabel = node("label"),   // label for input text
  inputuri = node("input"),   // input text
  sendlabel = node("label"),   // label for send button
  senduri = node("input"),   // send button
  resultlist = node("ul");   // result unordered list

 // assign accesskey, tabindex and other parameters for each input (in this case text and btn)
 accesstab(inputuri);
 inputuri.type = "text";
 inputuri.id = inputlabel["for"] = "userinput";
 // keyboard event listener (RETURN to perform the request)
 inputuri.onkeyup = function(evt){
  if(!evt)
   evt = window.event;
  if(!evt.wich)
   evt.wich = evt.keyCode;
  if(evt.wich === 13 && this.value.replace(/^s*|s*$/, ).length > 4)
   senduri.onclick();
 };
 
 accesstab(senduri);
 senduri.type = "button";
 senduri.id = sendlabel["for"] = "sendinput";
 senduri.value = "verify";
 // request function
 senduri.onclick = function(){
  var newresult = node("ul");

  // disable the form
  inputuri.disabled = senduri.disabled = true;

  // blur this button
  senduri.blur();

  // remove precedent unordered list element and replace
  // replace them with a simple "loading response" message
  // inside a new unordered list element
  resultlist.parentNode.replaceChild(newresult, resultlist);

  // assign new ul element to global scope var (to change them on load)
  resultlist = newresult;
  append(resultlist, append(node("li"), text("loading response")));

  // perform the request with byteson
  doRequest(tabindex);
  return false;
 };
 
 // form should be disabled, by default
 // (the form doesn degrade in this example)
 form.onsubmit = function(){return false};

 // create the output only for the first time
 output.replaceChild(
  append(
   form,         // the form
    append(
     node("fieldset"), [     // top fieldset
      append(
       node("legend"),    // and its legend
       text("W3 Validator")   // with its content
      ),
      append(
       inputlabel,    // label for text input
       text("write a valid url address") // and its content
      ),
      inputuri,     // input text
      append(
       sendlabel,    // label for button
       text("verify W3 validator result") // and its content
      ),
      senduri      // send button
     ]
    )
 ), output.firstChild);

 // create result fieldset to show informations
 // append this fieldset after the precedent output
 append(
  output,
  append(
   node("fieldset"), [       // bottom fieldset
   append(
    node("legend"),       // and its legend
    text("W3 Response")      // with its content
   ),
   resultlist        // and its unordered list
   ]         // to show the result
  )
 );
};      // that is all



The last file is the external stylesheet used in this page.

This basic example can explain better than every post, how W3validator PHP function works and how much is simple to implement byteson inside other JS code.
source: webreflection

 View Full Story.
Posted at 10:06:22 am | Permalink | Posted in PHP  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Be the first ... |Add your comment.

Your Comment ...

  Name (required)

  Email (required, hidden)

  Website


Top Stuff

e-messenger

MessengerFX

eBuddy

ILoveIM

AIM Express

Top 20 Ruby CMS


Our Partners

Facebook Applications

Ajax Projects

Web 2.0 Sites

Webloglines

Human Development Handbook

Software Development Company

Ajaxlines

Stock Exchange Chat


About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (114)
  • Ajax (10)
  • Ajax Games (9)
  • Articles (94)
  • Bookmarking (35)
  • Calendar (19)
  • Chat (40)
  • ColdFusion (3)
  • CSS (48)
  • Email (23)
  • Facebook (41)
  • Flash (17)
  • Google (30)
  • Html (16)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (40)
  • Javascript (190)
  • jQuery (3)
  • JSON (24)
  • Perl (2)
  • PHP (97)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (1)
  • Ruby (15)
  • Storage (4)
  • Toolkits (94)
  • Tutorials (203)
  • UI (12)
  • Utilities (173)
  • Web2.0 (18)
  • XmlHttpRequest (22)
  • YUI (4)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange