How to call perl or php script from HTML file?

by admin on August 3, 2005 · 30 comments

in Perl, php

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.

{ 30 comments… read them below or add one }

1 Lori James October 14, 2005 at 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

Reply

2 A Viewer November 10, 2005 at 4:17 am

Your tags aren’t closed properly …

Reply

3 Administrator November 10, 2005 at 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 :)

Reply

4 DynamicBits July 17, 2006 at 3:43 am

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

Reply

5 AMG RnD October 11, 2006 at 2:02 am

It doesn’t work for me. Any suggestions?

Reply

6 Jonny March 1, 2007 at 2:08 am

THX MAN THIS HLPED ME SO MUCH!!!

Reply

7 bhaskar February 29, 2008 at 2:58 pm

No worked for me….

Reply

8 bhaskar February 29, 2008 at 2:59 pm

any suggestion…..This is my code

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

Reply

9 pinhero May 8, 2008 at 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 ?

Reply

10 mccoytest August 14, 2008 at 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.

Reply

11 mccoytest August 14, 2008 at 6:57 pm

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

Reply

12 mccoytest August 14, 2008 at 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>

Reply

13 bprdkr October 16, 2008 at 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?

Reply

14 Kumar Chetan Sharma November 20, 2008 at 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. :-)

Reply

15 ali December 29, 2008 at 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……

Reply

16 ali December 29, 2008 at 12:34 pm

please tell me wat i ahve needed to run script

Reply

17 Kumar Chetan Sharma January 2, 2009 at 4:43 am

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

Reply

18 php in html June 17, 2009 at 5:58 am

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

Reply

19 Jeff Landers August 9, 2009 at 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.

Reply

20 Jeff Landers August 9, 2009 at 4:36 am

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

Reply

21 Jeff Landers August 10, 2009 at 5:16 am

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

Reply

22 Jeff Landers August 10, 2009 at 5:18 am

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

Reply

23 Jeff Landers August 10, 2009 at 11:49 pm

file name must end in .php

Reply

24 Victor August 11, 2009 at 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!!!!

Reply

25 King Beetle October 26, 2009 at 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.

Reply

26 pankaj June 27, 2010 at 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

Reply

27 kiran November 21, 2010 at 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…

Reply

28 Greg Tompkins January 23, 2011 at 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!

Reply

29 Hekmat October 20, 2011 at 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

Reply

30 Christin August 18, 2012 at 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.

Reply

Leave a Comment


9 − seven =

Previous post:

Next post: