perlcgi

Use `onClick` attribute in Perl CGI to call perl subroutine


I want to call a perl subroutine in Perl CGI. I know I need the onClick event but not sure how to use it correctly to call "TestSub". Can anyone help?

<input type="button" value="submit" onClick="process('')">


sub TestSub {
    ###Do some code###
}

Solution

  • You can't, at least not directly.

    onclick is a client-side JavaScript function. CGI code runs on the server.

    To call a Perl sub you need to:

    1. make an HTTP request to a URL
    2. have your server trigger a CGI program for that URL
    3. have that program call the sub (e.g. by examining values in the query string to decide to call it).

    The simple way to make that HTTP request would be to use a link or to submit a form. If you want to use JavaScript then you can assign a value to location or use the XMLHttpRequest object.