Cannot execute sh file using button click in php file in apache

I the problem that i facing is cannot use button click to execute the sh file that store in the same location. the program file is a php file and running in apache2. the code that i run is show below

<button onclick="sh()" name="sh">SH</button>

<script type='text/javascript'>
function sh()
{
<?php exec('/home/user/test.sh'); ?>
}
</script>

javascript is client-side. php is server-side. The php code runs once, when the page is loaded.

To trigger server-side code to happen when you click, that click must trigger a page to load. (or a form to be posted, etc.)

Then got any suggestion for doing this? actually i need to use button click to trigger a sh page to be execute.

You can use the javascript to make a hidden form post, or cause a page retrieval to silently happen via an xmlhttprequest object, etc, etc.

is there another method to do it? as it look complicated and i am new to this field. thanks

Make it a link to the same page, so that people can click it. No javascript need be involved at all. Of course, on click, the page will be reloaded.