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
if(isset($_POST['byteson'])) {
require 'FastJSON.class.php';
require 'W3validator.function.php';
if(get_magic_quotes_gpc())
$_POST['byteson'] = stripslashes($_POST['byteson']);
$output = FastJSON::encode(W3validator(FastJSON::decode($_POST['byteson'])));
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($output);
}
?>
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
if(document.getElementsByTagName)
onload = function(){
function accesstab(input){
input.accesskey = input.tabindex = ++accesskey;
++tabindex;
};
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;
};
function append(parent, elements){
if(elements.constructor !== Array)
elements = [elements];
for(var i = 0; i < class="jshproperties">length; i++)
parent.appendChild(elements[i]);
return parent;
};
function createList(message, obj){
for(var a, span, i = 0, key = "", list = [], tmp = []; i < class="jshproperties">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);
};
function doRequest(oldtab){
byteson.request("w3validator.php", inputuri.value, {
load:function(obj, xhr){
var a, key = "";
resultlist.removeChild(resultlist.firstChild);
for(key in obj) {
if(obj[key].constructor !== Array) {
append(
resultlist,
append(
node("li"), [
append(node("strong"), text(key)),
append(node("span"), text(obj[key]))
]
)
);
};
};
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();
};
};
inputuri.disabled = senduri.disabled = false;
tabindex = oldtab;
}
});
};
function node(type){return document.createElement(type)};
function tab(node){node.tabindex = ++tabindex};
function text(value){return document.createTextNode(value)};
var accesskey = 0, tabindex = 0, output = document.getElementById("request"), form = node("form"), inputlabel = node("label"), inputuri = node("input"), sendlabel = node("label"), senduri = node("input"), resultlist = node("ul");
accesstab(inputuri);
inputuri.type = "text";
inputuri.id = inputlabel["for"] = "userinput";
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";
senduri.onclick = function(){
var newresult = node("ul");
inputuri.disabled = senduri.disabled = true;
senduri.blur();
resultlist.parentNode.replaceChild(newresult, resultlist);
resultlist = newresult;
append(resultlist, append(node("li"), text("loading response")));
doRequest(tabindex);
return false;
};
form.onsubmit = function(){return false};
output.replaceChild(
append(
form, append(
node("fieldset"), [ append(
node("legend"), text("W3 Validator") ),
append(
inputlabel, text("write a valid url address") ),
inputuri, append(
sendlabel, text("verify W3 validator result") ),
senduri ]
)
), output.firstChild);
append(
output,
append(
node("fieldset"), [ append(
node("legend"), text("W3 Response") ),
resultlist ] )
);
};
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.
Have fun with W3 validation ;-)