you are mixing things up here.
if you want php to include error messages (javascript or not) regarding previously entered form data, you have to submit the page first (otherwise no php code gets executed). therefore, a normal click on "submit" is sufficient, you don't have to use onClick.
in my opinion, I would construct the script as a form page that reloads itself on submit (<form action=$PHP_SELF>). In the beginning of the script, if the submit button (e.g. $ok) is set, the script jumps into the validating/processing section. collect errors in a string.
then do the processing (i.e., mail the user data) only if the error string is empty. when this is done, you can header() to another page on your site.
if there were errors, the form is re-displayed and should contain the previously entered data so the user can correct it. display the error string, too.