Friday, February 03, 2006

Mochikit on PHP

Hi there, hehehe, frankly enough huh ?
yes, I manage to run mochikit on my office project, built on top of php.
Here's the tips :
1. on your html file put a javascript like this one :


// define the DOCManager Object classes
DOCManager = function()
{ // define the property
this.target_div = getElement("response");
this.parameter = "&company_id="+ getElement("company_id").value +
"&grin_type="+ getElement("grin_type").value +
"&grin_no="+ getElement("grin_no").value +
"&ref_type="+ getElement("ref_type").value +
"&ref_no="+ getElement("ref_no").value +
"&order_no="+ getElement("order_no").value;
// bind the methods
bindMethods(this);
};

// define the DOCManager methods
DOCManager.prototype =
{
"loadDOC" : function()
{ alert( " will gonna define the JSON Doc" );
var doc = loadJSONDoc("{FRONTCOMPONENTURL}ajax/ajax.bedjo.php?task=search"+
this.parameter
);
alert( " define DOC, and we will proccess the doc" );
doc.addCallbacks(this.processDOC, this.errorDOC);
alert( "DOC process complete" );
},

"processDOC" : function(data)
{ alert("come to processDOC baby");
var rows = data.rows;
var cols = data.columns;
var newTable = TABLE({"id":"repeat_table", "name":"repeat_table",
"width":"100%", "border":"0", "cellpadding":"1",
"cellspacing":"0"},
THEAD(null, map(this.colDisplay, cols)
),
TFOOT(null, TR(null,
TD({"colspan":"3", "class":"sectiontableheader"}, " "),
TD({"colspan":"5", "class":"sectiontableheader"}, " ")
)
),
TBODY(null, map(this.rowDisplay, rows)
)
);
swapDOM('response', newTable);
},

"errorDOC" : function(data)
{
alert( data.message );
},

"rowDisplay" : function (data)
{
return TR({"class":"sectiontableentry2"}, map(partial(TD, null), data));
},

"colDisplay" : function (data)
{
return TR(null, map(partial(TD, {"class":"sectiontableheader"}), data));
}

};

function search_grin()
{ doc_manager = new DOCManager();
doc_manager.loadDOC();
};


Please remove the // sign in front of the tag, since It forbiden in here
and you have to made an on-click action on your submit-button, I'll bet you know what I meant.

2. Create the ajax file , I named it ajax.bedjo.php, and heres the sample code with a JSON and XML return doc :

$task = $_REQUEST["task"];


switch ( $task )
{
case 'create':
header("Content-type: text/xml");
echo "";
echo "";
echo "";
echo $action;
echo "";
echo "kembang jepun kembang krampung, aku mlembung kakean diambung ";
echo "";
break;
case 'find':
echo '[{';
echo '"id" : "8370",';
echo '"name" : "KANG BEDJO EMANG EMOY"';
echo '},';
echo '{';
echo '"id" : "8360",';
echo '"name" : "TESTING SODARA SODARA SEKALIAN"';
echo '}]';
break;
case 'list':
echo '{';
echo ' "columns": [[ "domain_name", "create_date", "expiry_date", "organization_name"]],';
echo ' "rows": [';
echo ' ["json.org", "2000-05-08", "2006-05-08", "Douglas Crockford"],';
echo ' ["mochibot.com", "2005-02-10", "2007-02-10", "Jameson Hsu"],';
echo ' ["pythonmac.org", "2003-09-24", "2006-09-24", "Bob Ippolito"],';
echo ' ["undefined.org", "2000-01-10", "2006-01-10", "Robert J Ippolito"],';
echo ' ["python.org", "1995-03-27", "2007-03-28", "Python Software Foundation"]';
echo ' ]';
echo '}';
break;
}

Ok thats all, now u can use it. Anyway this is a simple tutorial on using mochikit with php.

Ok, cu next

No comments: