That JSON site did nothing but confuse me when I first heard about JSON. What helped me was...
JSON is just a way to send data around. And by data, I mean arrays. PHP makes this super easy with the
json_encode function. All you do is feed that function an array and echo out the results. BAM!, now you got JSON.
I use JSON extensively for form validation. So, for example, my PHP would build an array consisting of the validation status and, if applicable, the error messages (along with their ID). Kinda like the following:
[validation] => 'failed'
[errors]
[0]
[id] => 'firstname'
[error] => 'First name not provided'
[1]
[id] => 'lastname'
[error] => 'No last name provided'
Then I would json_encode() this array and echo it out. When jQuery gets hold of this data it knows if the validation failed or succeeded. And if it failed, it knows what error messages to display and what form fields to highlight and apply an error class (make them red so the user knows what to fill in).
It is also worth noting that you should only return this JSON string if you are detecting an AJAX response. If the user has JavaScript disabled you don't want them staring at a JSON string. If an AJAX response was not detected, you should simply echo out the error messages in an unordered list (or something similar).
Now,
how to detect an AJAX response.
--------------------------------------------------
Victor Michnowicz
http://www.vmichnowicz.com