How to call perl or php script from HTML file?

Easiest way to do this is call php/perl script as javascript. Since javascript is client side stuff, php or perl script need to writeback output using javascript commands only. This is useful if you have lots of HTML files and would like to do some server side processing. For example Tips & Tricks section on our site has lots of html pages which uses code as follows:

<script language="JavaScript" type="text/javascript" src="">

Above code generates recent post posted in blog. Main advantage of this technique is simple, you can use it from HTML file and your javacode is also hidden (although other better ways exists to do this).

(A) An example of PERL, create file called as follows and put into your cgi-bin directory:

use strict;
use warnings;
use CGI;
my $q = CGI->new();
my $info = "Server: ". $ENV{'SERVER_NAME'} . "
Script name: ". $ENV{'SCRIPT_NAME'}; # need to send header otherwise sky will fall on you print $q->header(); # always get back to user after perl code print "document.write('Hello world
I am called from HTML page $ENV{'HTTP_REFERER'}
');"; print "document.write('$info');";

Add following line to your HTML code (replace with your real domain name):

<script language="JavaScript" type="text/javascript" 

(B) An example of PHP, create file called html.php as follows and put into your webserver directory:

 $ser = "Server: ". $_SERVER['SERVER_NAME'] .  "
Script name: ". $_SERVER['SCRIPT_NAME']; echo "document.write('Hello World
I am php code called from HTML file:' );"; echo "document.write('$ref
');"; echo "document.write('$ser');"; ?>

Add following line to your HTML code (replace with your real domain name):

<script language="JavaScript" type="text/javascript" 

PHP code is bit easier to use as compare to PERL.

Author: admin

I like chocolate, gadgets, open source software, photography, traveling and all shades of green colors. I love spending time with fun loving friends and family members. This is my own online journal.

30 thoughts on “How to call perl or php script from HTML file?”

  1. I came across you site because I’m trying to search for a solution to this issue-

    I had information for this book loaded in admin on our site and inadvertently deleted it. When someone goes to this link they get a blank page now:

    I reloaded the information and it is all in:

    Is there a way to redirect so that if someone selects the first they are taken to the second?

    Note Comment edited by admin to avoid bad domain name

  2. Yup they are closed May be I used old method to display code; if you have a problem with code then reply back :)

  3. “A Viewer” is right. The script tags in the HTML snippets are not properly closed.

  4. any suggestion…..This is my code

    script language=”JavaScript” type=”text/javascript” src=”listalbum.php” /script

  5. It doesn’t work, even with corrected tags.
    I wish it could, because I have been looking for it.
    The only method known to me is Ajax, but thought some shorter way could be found. Is there anyone who run this code ?

  6. Despite what the author says, his code is displayed wrong on this page, and my corrections might be displayed wrong too. I will try to make it work. You likely cannot call your perl by the duffix ‘.pl’ as he did (it did not work for me.) I will try some escapes…

    This code works: file.html

    embedded perl

    This is new content

    This is right content


    array one
    array two

    file.cgi (or a cgi named link to

    use CGI;
    my $q = CGI->new();
    print $q->header();
    my $filename = “file.txt”;
    open(FIL,”<$filename”) or warn “($!)”;
    @contents=(); # slurp
    close FIL;
    chomp @contents;
    foreach (@contents)
    print “document.write( ‘$_’ )\n”;

    I needed it to do the entire contents of a fail.

    This works, I hope the escapes work.
    You’re smart enough to figure it out.

  7. I believe this will show properly:

    <title>embedded php</title>
    <!–– begin outer table ––>
    <table border="1"><tr>
    <!–– temp ––>
    <td>This is new content
    <!–– begin left nav ––>
    <script language="JavaScript" type="text/javascript"
    <!–– end left nav ––>
    <!–– begin right nav ––>
    <td>This is right content
    <!–– end right nav ––>
    <!–– end outer table ––>
    use CGI;
    my $q = CGI–>new();
    print $q–>header();
    my $filename = "file.txt";
    open(FIL,"<$filename") or warn "($!)";
    @contents=(<FIL>); # slurp
    close FIL;
    chomp @contents;
    foreach (@contents)
    print "document.write( '$_' )\n";

    <table padding="5" border="1">

  8. i am trying to handle an onclick event and then call a perl script
    i am trying to do something like this-

    how does one handle this?

  9. Look at this way, you can use php to do whatever type of content you want to dump into browser, here you go
    tags in PHP file, only if it is a PHP file and u r not shuffling html. The code isnt bullet proof but explains it. Now save this code in a name dumpMyFile.php and if you plan to display image use or for JavaScript src=”dumpMyFile.php” :-)
    I will try to put some examples on my blog too!
    Nice site. :-)

  10. i have written a html form and from that i am calling perl script. how can i test whether its working fine or not.

    i am using windows xp and i ahve run as localhost……

  11. OOPS!
    I dont see the code I submitted in the comments, your WP upgrade spolied everything :(

  12. If your webserver runs php you can use php’s ‘include(filename)’;
    The file can be located outside of Webspace. This way you’re php script can use echo or print without writing document.writeln.

  13. I have this issue too but obviously the main contributor above (who is more insterested in showing what they think they know rather than being helpful) did not actually explain how to to call a PERL script inside an HTML code. All this person did was bleep out what muddled up stuff they know or have heard or have used. Half education is poisonous!!!!

  14. After quite a bit of struggle, and by using wireshark to analyze the output of a working example of calling a perl script through JavaScript, I was able to successfully make this work. Here’s what worked for me:

    The JavaScript:


    The Perl code:

    print "Content-type: text/html\n\n";
    print "document.write(\" Sample text...\\n\");\n";


    The secret of my success was providing the browser with the “Content-type” declaration in the Perl script.

  15. the error i am getting in the html file
    Message: Syntax error
    Line: 1
    Char: 1
    Code: 0
    URI: http://localhost/testhtml/test.php
    The test.php code is

    The test.html code is
    i have enclosed external javascript call within single quote so that it gets displayed here
    Can anyone help me in resolving this. I am new to php

  16. hey not able to find my cgi-bin folder in not able to execute any perl programs embedded within html or xml…the perl code is not getting executed..even there is no html me an i create html or cgi folder in ubuntu…

  17. I have an old email form that has some javascript to do some error checking before sending it to the perl in cgi/bin. I need to incorporate a captcha which uses PHP. How can I include in my javascript to validate the php, then if that code is correct to then continue to pass to the cgi-bin in perl? Does that make sense?

    Thank You!

  18. Hi.please help me:
    I used path of file in the php script and I want make a link to this file from php script but it can’t work.I used different function to take correct path of file like dirname() but until now it has problem.please show correct way and guide me how i can do this .thank you

  19. Hello, Neat post. There’s an issue together with your web site in internet explorer, would test this? IE still is the market chief and a huge component of people will leave out your fantastic writing because of this problem.

Leave a Reply

Your email address will not be published. Required fields are marked *