How to call a function in template.php through ajax from a page? I have a button in drupal page template. When user clicks on that it will write the text in the text box to a text file.

I have written a function for that in template.php file. I want to pass the value in text box to the function using ajax. But how can I do it? What url should i give in javascript ajax function?
I would recommend using the Ajax Framework in Drupal 7 core: http://api.drupal.org/api/drupal/includes%21ajax.inc/group/ajax/7. 

You will need to create a callback through hook_menu which will return the html you want to load.

See the examples module for how to do this. The two functions show adding a link and using the 'use-ajax' class and creating a link using a render array:
http://drupalcode.org/project/examples.git/blob/refs/heads/7.x-1.x:/ajax_example/ajax_example_misc.inc

They both link to 'ajax_link_callback' which calls the other function ajax_link_response().

'ajax_link_callback' is defined in hook menu like so:

 180   // A menu callback is required when using ajax outside of the Form API.
 181   $items['ajax_link_callback'] = array(
 182     'page callback' =&gt; 'ajax_link_response',
 183     'access callback' =&gt; 'user_access',
 184     'access arguments' =&gt; array('access content'),
 185     'type' =&gt; MENU_CALLBACK,
 186     'file' =&gt; 'ajax_example_misc.inc',
 187   );