Hi Sam,
On the template form,
i. add a sub form,
ii. take a button,
iii. Select Button, goto Field tab in Object window, set control type to Submit,
iv. Got to Submit tab set submit as XML Data (XML)
v. On button click event call ur application, you will get form data in xml format.
For passing data to template you can pass it in xml format.
Not having much idea abt PHP, but we implemented same with Java.
Hope this will help you.