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="http://rss.cyberciti.biz/rss2html.php?u=http://blogs.cyberciti.biz/feed/">
<script>

Above code generates recent post posted in blogs.cyberciti.biz/hm 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 html.pl as follows and put into your cgi-bin directory:

#!/usr/bin/perl
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 mydomain.com with your real domain name):

<script language="JavaScript" type="text/javascript"
src="http://www.mydomain.com/cgi-bin/html.pl"
<script>

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

<?php
 $ref = $_SERVER['HTTP_REFERER'];
 $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 mydomain.com with your real domain name):

<script language="JavaScript" type="text/javascript"
src="http://www.mydomain.com/html.php"
<script>

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

Meet the 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 comments… add one
  • Lori James Oct 14, 2005, 5:46 pm

    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:
    http://www.abcdomain.com/product_details.php?product_id=44

    I reloaded the information and it is all in:
    http://www.abcdomain.com/product_details.php?product_id=55

    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

  • A Viewer Nov 10, 2005, 4:17 am

    Your tags aren’t closed properly …

  • Administrator Nov 10, 2005, 6:31 am

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

  • DynamicBits Jul 17, 2006, 3:43 am

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

  • AMG RnD Oct 11, 2006, 2:02 am

    It doesn’t work for me. Any suggestions?

  • Jonny Mar 1, 2007, 2:08 am

    THX MAN THIS HLPED ME SO MUCH!!!

  • bhaskar Feb 29, 2008, 2:58 pm

    No worked for me….

  • bhaskar Feb 29, 2008, 2:59 pm

    any suggestion…..This is my code

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

  • pinhero May 8, 2008, 3:23 pm

    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 ?

  • mccoytest Aug 14, 2008, 6:09 pm

    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

    file.txt:

    array one
    array two

    file.cgi (or a cgi named link to file.pl:

    #!/usr/bin/perl
    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.

  • mccoytest Aug 14, 2008, 6:57 pm

    Moderator, is there anyway for you to fix this for proper display????????

  • mccoytest Aug 14, 2008, 8:22 pm

    I believe this will show properly:

    file.html:
    <html>
    <head>
    <title>embedded php</title>
    </head>
    <body>
    <!–– begin outer table ––>
    <table border="1"><tr>
    <!–– temp ––>
    <td>This is new content
    </td>
    <!–– begin left nav ––>
    <td>
    <script language="JavaScript" type="text/javascript"
    src="http://sbweb.sba.us.ray.com/~hacr2194/file.cgi"&gt;
    </script>
    </td>
    <!–– end left nav ––>
    <!–– begin right nav ––>
    <td>This is right content
    </td>
    <!–– end right nav ––>
    </tr></table>
    <!–– end outer table ––>
    </body>
    </html>

    file.pl:
    #!/usr/bin/perl
    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";
    }

    file.txt:
    <table padding="5" border="1">
    <tr><td>Deb</td><td>Mccoy</td></tr>
    <tr><td>Tim</td><td>Mccoy</td></tr>
    </table>

  • bprdkr Oct 16, 2008, 11:50 am

    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?

  • Kumar Chetan Sharma Nov 20, 2008, 8:03 am

    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. :-)

  • ali Dec 29, 2008, 12:34 pm

    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……

  • ali Dec 29, 2008, 12:34 pm

    please tell me wat i ahve needed to run script

  • Kumar Chetan Sharma Jan 2, 2009, 4:43 am

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

  • php in html Jun 17, 2009, 5:58 am

    it was good tutorial but can we call any php scipt with this method

  • Jeff Landers Aug 9, 2009, 4:26 am

    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.

  • Jeff Landers Aug 9, 2009, 4:36 am

    for example to call php file named ‘foo.php’ you would do:

  • Jeff Landers Aug 10, 2009, 5:16 am

    I meant that for file ‘foo.php’ to include this script it would be

  • Jeff Landers Aug 10, 2009, 5:18 am

    <?php
    include(“foo.php”);
    ?>

  • Jeff Landers Aug 10, 2009, 11:49 pm

    file name must end in .php

  • Victor Aug 11, 2009, 5:23 pm

    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!!!!

  • King Beetle Oct 26, 2009, 5:55 am

    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:
    ———————-

    #!/usr/bin/perl
    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.

  • pankaj Jun 27, 2010, 12:39 pm

    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
    “”
    HELLO TESTING
    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

  • kiran Nov 21, 2010, 1:58 pm

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

  • Greg Tompkins Jan 23, 2011, 3:34 pm

    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!

  • Hekmat Oct 20, 2011, 5:59 am

    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

  • Christin Aug 18, 2012, 7:43 pm

    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 Comment