Query WordPress database in javascript

I do not think you can run a database query directly with javascript but I believe you can link to a php file which does the query and returns results.
I have a script which needs to have a value supplied to it dynamically:

jQuery(document).ready(function() {
jQuery('.myzebra-checkbox[value=*dynamic value here*]').attr('checked', true);

How can I
a) link to a php file
b) insert a returned value eg $foo
and do it in a secure, WordPress-friendly way?