Sunday, April 26, 2009

CODE: between JS and PHP with JSON

Found one thing that i unable to do until now

In the javascript if you use a formated json ( $H way ) you can't put it directly with document.write or in case you using Ajax prototype
the $(elementID).update(json) or $(elementID).innerHTML = json
will break your code, unless you use ajax mode send it to server first and let the php create it.

The solution :
you need to write it exactly like when you write using php and html there's no direct method to do this in prototype.
I guess that's mean don't write a html thing in your js if you want a clean code let the php handle it nicely.

In case you need this than try searching php equivalent to htmlentites or addslashes it usually work....

Friday, April 17, 2009

CODE: JSON from JS > PHP

JSON from JS to PHP was much more easier or maybe difficult if you don't know how to use AJAX again I recommend using prototype as it's simplify ajax method a lot. You can get the code in www.prototypejs.org

jsfile.js

to preapare a json variable that compatible with php we use prototype's $H method add a slashes in every quote and then format to json with "toJSON()" function ( You might think of using slashes when passing variable from php to js using addslashes method, trust me it's not working. But correct me if you found another solution ).

function send_json_to_php() {
var preparejson = $H({name:'Violetx', occupation:'characterx', age:30 });
var json = preparejson.toJSON();

//below is an ajax function -- prototype way
new Ajax.Request('php_accept_json.php',{
parameters: 'json='+json
});
}

php_accept_json.php

$json = $_POST['json'];
$decode = json_decode(stripslashes($json));
print_r($json);

*Don't forget you need to stripslashes first before do json_decode

//you will get below result
stdClass Object
(
[name] => Violetx
[occupation] => characterx
[age] => 30
)

which you can access each value like this $json->name, $json->age, etc.

Easy ? yep , this is only small thing but it will greatly help programmer. From CODE: JSON from PHP > JS article you remember we send a PHP json variable to javascript. you can pass that variable directly using ajax back to PHP and php still can read it using the same method in php_accecpt_json.php.

You can imagine the advantage yourself


----------END----------

CODE: JSON from PHP > JS

Ok for you love JSON this is how to pass json value to each language

PHPJSON

php > $test = array('name'=>'Violetx', 'occupation'=> 'characterx', 'age'=> 30 );
php > $json = htmlentities(json_encode($test));
php > you can pass the $json to js using DOM like this onclick="jsfunction(\''.$json.'\')"

Note:remember here if you use echo to wirte the third php line you need the backslash and quote ( \' ) between the value you want to give to js. the $json is a string, if you directly write with html you need quote ( ' ) only

We use htmlentities to make the json_encoded variable readable in html.
The code will pass nicely to your javascript function.

JSJSON

I use prototype js for manipulating js you can get the code in www.prototypejs.org

js > function jsfunction(json) {
js > var string = json.evalJSON();
js > alert(string.name +' '+ string.occupation+' '+string.age);
js > }

in your JS function you can directly evalJSON it ( if you use prototype ) and viola you can directly access it like above code ( string.name, etc) .

What's the advantage ?

Suppose you using DOM onclick to js and you need to give 3 or more value to javascript function, you end up something like this onclick="jsfunction(value,value,value,value)" .

Then in your javascript function you need to give each of them a variable name that sometime ( if you not strict in coding ) different than the variable name you set in the php ( suppose you get the four value from database and it's render automaticly for each row )

and later on maybe you need to put a mode value, a type value and so on and make your DOM onclick longer and longer and messy, and if you can manage it with array, I believe you will prefer do it with array.

by using above method you simplify :

From onclick = "jsfunction(value,value,value,value)"
To $array = array('var1'=>value,'var2'=>value,'var3'=>value,'var4'=>value);

From jsfunction(var1,var2,var3,var4)
To jsfunction(json)

From defining variable name twice
To defining variable name once and use for the rest of the code

Note: if you using prototype remember to enable mod_deflate for apache and strip all white space and comment from it, you can use the tool from the web.

Next > CODE: JSON from JS > PHP

Monday, March 16, 2009

The day when we share...

Share your love ..... that`s good ( universal of course )
Share your money ... donating that`s good also
Share your stuff .... be careful it might not return

Share your data .... it's time for collaboration
Share your idea ... same as above
Share your movie ... that's where torrent come in
Share your hobby ... find something similar
Share your apps ... open source on its way

Share your privacy .... hell no !!!!

Long live...

Long live ubuntu ( yeahh cant wait for jaunty and koala )
Long live nomachine ( it can access mac to not just windows and linux )
Long live google ( repair the docs and spreadsheedt please )
Long live zoho ( can you share doc with google ... guess not )
Long live openoffice ( cmon beat msoffice already )
Long live windows 7 ( dont steal x server layout please... )
Long live ......

Sunday, February 22, 2009

ubuntu vs window

nothing is perfect in this world yeah
especially man made but together they can overcome it haha

my windows day
i have to update all my av as fw and so many anti product
proud how strong my protection could be and later found there still a virus in the computer trojan spyware etc
getting slow day by day
find a lot of new software to play around
games you just need to put in the cd install and play

my ubuntu day
what a refreshing os speed ok
proud how clean my computer be without the anti stuff
don't care about it anymore
cool compiz effect
less software to play around especially it stressfull when installing something advance but terminal has it's own attractiveness
anyway there's a lot of web application now who care easier to install
for games i restart the computer first and boot to windows

Saturday, February 21, 2009

Kamma is man made ?

the final answer is no

kamma seed is created by panca kanda
the one who sow is panca kanda also

but not the mind
the mind is pure

when we no longer cling to panca kanda
the panca kanda still receive the kamma fruit

but no the mind