How to call a perl script in html - html

How to call a perl script which is returning data "text" type inside a html file.Posting the perl script for reference.This script has to be called in the index.html so how to do that
#!/usr/bin/perl -w
use CGI;
my $cgi = CGI->new;
my $remote = $cgi->remote_host();
my $AB = "16.108";
my $CD = "16.214";
my $EF = "10.99";
my $GH = "10.243";
my $XY = "10.179";
my #remote_ip_values = split(/\./, $remote);
my $remote_ip = $remote_ip_values[0] .".". $remote_ip_values[1];
print "Content-type: text/html\n\n";
print "document.write(\"<style>\\n\");\n";
print "document.write(\"font color:red;\\n\");\n";
if ($remote_ip eq $AB)
{
print "document.write(\"SUCCESSFUL\");\n";
}
else
{
print "document.write(\"TEST\");\n";
}
exit;

I am editing my answer as I now understand what you are trying to do. I have set up a simple example which will load the content from "another website" - which for you will be your Perl CGI.
This HTML uses some simple JQuery. I am loading the following HTML page, http://chocksaway.com/tester.html and copying the contents into the "test" div:
<!DOCTYPE HTML>
<html>
<head>
<title>Loader</title>
</head>
<body>
<div id="test"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#test').load('http://chocksaway.com/tester.html');
});
</script>
</body>
</html>
http://chocksaway.com/tester.html has the following HTML:
<html>
<header></header>
<body>
this is a test so there
</body>
</html>
If you open you http://chocksaway.com/tester2.html, you will see:
this is a test so there
Replace "http://chocksaway.com/tester2.html" with the URL of your Perl CGI.

Related

html source code from Arduino

I am making a home appliance control device using arduino. I got this html code from the website. What is the code to get the value from the Arduino?
I think
$("#temperatureDisplay").load('temperature_display.php');
This part would be a receiving the value from the Arduino, but I am confusing. If I know what is code for receiving the value, how can I use if statement to change the background color with different value of output from Arduino?
<!DOCTYPE html>
<html>
<head>
<script src="jquery-2.0.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Weather Station</title>
</head>
<body>
<div class="data">
<div class="dataTitle">Temperature: </div>
<div id="temperatureDisplay">Waiting for data ...</div>
</div>
<script type="text/javascript">
setInterval(function()
{
$("#temperatureDisplay").load('temperature_display.php');
}, 1000);
</script>
</body>
</html>
This is php file and the data file is just number. This number is always changed with the Arduino.
<?php
$myFile = "temp_data.txt";
$fh = fopen($myFile, 'r');
$line = fgets($fh);
fclose($fh);
echo $line;
?>

perl script does not close body?

In an example script that prints HTML, it looks to me that the body tag is not closed. However I have never had experience with Perl before. Is this example incorrect? or is there something else that means body is closed?
print "Content-type: text/html\n\n";
print "<html>\n<head>\n<title>\nPerl CGI
Example\n</title>\n<body>\n<h1>Hello,
World!</h1>\nYour user agent is: <b>\n";
print $cgi_object->user_agent();
print "<b>.</html>\n";
Where there is a . on the last line it looks to me like it should be </body>
You aren't missing anything, that code simply doesn't generate an end tag for the body element, but that tag (unlike the missing Doctype) is optional in HTML anyway so the element will be closed by the browser when it parses the end tag for the html element.
It would be better written something more like this:
#!/usr/bin/env perl
use strict;
use warnings;
use CGI;
use Template;
my $cgi = CGI->new();
print $cgi->header(-charset => 'utf-8');
my $ua = $cgi->user_agent();
my $tt = Template->new();
$tt->process(\*DATA, { ua => $ua });
__END__
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Perl CGI Example</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>Your user agent is: <em>[% ua | html %]</em>.</p>
</body>
</html>
And better still if you ditched CGI and used PSGI/Plack.

Get input from HTML form in other document using perl and checking input

I have two seperate perl documents:
mailcheck.pl
loggingpage.pl
Mailcheck prints out a form in html where the user can put in her/his email and checks if the input is a valid email address. Then it calls upon the loggingpage.pl which saves the email in a logfile and gives out information to the user if the email address was successfully stored.
These are the two documents:
mailcheck.pl:
#!/usr/bin/perl
use strict;
use warnings;
print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>Mailcheck</title>
</head>
<body>
<form name="ec" action ="./loggingfiles.pl" method="get">
Email: <input type="text" name="email"> <br>
<input type="button" value="Pruefen" onclick="javascript:emailcheck();">
</form>
<script language="javascript" type="text/javascript">
function emailcheck()
{
var emailoutline = /^[a-z0-9._%+-]+\#[a-z0-9.-]+\.[a-z]{2,4}\$\/\;
var x = document.ec.email.value;
if (emailoutline.test(x))
{
open("./loggingpage.pl);
}
else
{
alert("Dies ist keine richtige eMailadresse!");
}
}
</script>
</body>
</html>
HTML
exit;
loggingpage.pl:
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:standart);
#reads the input of the form called "email"
$emailinput = param('email');
#saves the email in the logfile
open(my $ml, ">", "./../logs/maillog.txt") or die "Fehlermeldung";
print $ml scalar(localtime()), " Email: ", $emailinput;
close($ml);
#gives out information about the saving process
if (...) #saving the email in the logfile succeeded
{
print <<HTML;
<html>
<head></head>
<body>
<script language="javascript" type="text/javascript">
alert("Your input has been saved under the following email: " + $emailinput);
</script>
</body>
</html>
HTML
exit;
}
else #gives out warning if information was not stored right
{
print <<HTML;
<html>
<head></head>
<body>
<script language="javascript" type="text/javascript">
alert("Error while saving your input.");
</script>
</body>
</html>
HTML
exit;
}
(I know that the <<HTML (..) HTML blocks cannot have spaces in front of them - in my actual document I have edited the block the right way)
My questions are now:
a. How can I write the if-conditions when I want to see if the email got saved in the logfile?
b. I am not quite sure if the part $emailinput = param('email'); works since I was not able to try it, is it the right way to get the input of the form in mailcheck.pl or do I need to write the code differently?
For you information:
- mailcheck.pl works correctly.
- the saving of the email in the logfile also works fine (i tried it through defining a variable as an email and saving that into the logfile together with the current date...)
So I have solved question a) like this (it seems to be working):
open(my $ml, ">", "./../logs/maillog.txt") or die "Saving Error";
print $ml scalar(localtime()), " Email: ", $emailinput;
close($ml);
#print "\nYour eMail was successfully logged!", $emailinput, "\n\n";
print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>Mailcheck2</title>
</head>
<body>
<p>Es wurde die folgende eMail erfolgreich gelogged:</p>
</body>
</html>
HTML
exit;
Now I only need help with question b).
After performing the validation, submit the form using:
document.ec.submit(); // 'ec' is the name of your form
This will send the form data to the page defined in the action attribute of your form.
Better yet, use the onsubmit attribute of the form tag to perform the validation:
<form name="ec" action ="./loggingfiles.pl" method="get" onsubmit="return emailcheck()">
Then modify the validation method to return false on failure:
function emailcheck()
{
var emailoutline = /^[a-z0-9._%+-]+\#[a-z0-9.-]+\.[a-z]{2,4}\$\/\;
var x = document.ec.email.value;
if (!emailoutline.test(x))
{
alert("Dies ist keine richtige eMailadresse!");
return false;
}
}
Do not rely on JavaScript in this way. JS is useful an additional sanity check for your form, but it should not be the only nor the primary means of validating your data.
Instead, put all your data into a single page, and for initial development, remove any JS. Make your form a POST request, and detect when the form is posted to determine if you need to do the validation. You'll need to use your JS validation regex in your perl instead, although eventually you should upgrade to Email::Valid.
After your form and post is working without JavaScript, you can add an onSubmit method if you want to do some clientside verification to duplicate what you've done in perl.

How to exchange data with server from mobile web app

Hi I have to develop a mobile application using cross platform technology(HTML,CSS,JAVASCRIPT) wherein I need to send data to server and receive data from server.so I thought to use ajax so I tried below given code.
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("http://10.16.10.188/login/Hello.txt");
});
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button>Get External Content</button>
</body>
</html>
The above code is working when I keep this html and text file on wamp server in same directory. but its not working when I keep this html file in my android asset folder and text file on server.it is not giving any response.could someone please help me to get this done.thank you so much
I was able to solve the problem by using jsonp in below given code.
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.10.1.min.js">
</script>
<script>
$.ajax({
url:"http://10.16.10.188/login/loginValidator.php",
dataType: "jsonp",
success:function(data,status){
if(status=='success')
{
alert("Data: " + data.fullname + "\nStatus: " + status);
var val = data.fullname;
$("#imchanged").html(val);
}
}
});
</script>
</head>
<body>
<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
<button id="imchanged">Get External Content</button>
</body>
</html>
Below is loginValidator.php code where my html points.
<?php
$array = array(
'fullname' => 'Jeff Hansen',
'address' => 'somewhere no.3'
);
$data = json_encode($array);
echo $_GET['callback']."(".$data.")";
?>
Using above given code I am able to invoke php file from mobile emulator and get the response from server.

Sed script to delete everything in <head> tag except the <title> and insert script

I want to remove everything inside the <head> tag except the <title> in an html file, and also insert a script into the <head> tag after this is done. I don't want to delete the <head> tag itself.
Is this possible using Sed?
Using regex to parse HTML is not a good choice. See this famous article for a full discussion
I will suggest you to use a DOM Parser for this type of work since any regex you try will break at some point using sed or any of its variant. Since you've asked for an alternative in your comments consider following code in PHP:
$content = '
<HTML>
<HEAD>
<link href="/style.css" rel="stylesheet" type="text/css">
<title>
Page Title Goes here
</title>
<script>
var str = "ZZZZZ1233#qq.edu";
</script>
</HEAD>
';
$dom = new DOMDocument();
$dom->loadHTML($content);
$head='
<head>
<script>
// your javascript goes here
var x="foo";
</script>
';
$headTag = $dom->getElementsByTagName("head")->item(0);
if ($headTag != null) {
$title = $headTag->getElementsByTagName("title")->item(0);
if ($title != null)
$head .= '<title>' . $title->textContent . '</title>
';
}
$head .= '</head>';
var_dump($head);
OUTPUT
string(118) "
<head>
<script>
// your javascript goes here
var x="foo";
</script>
<title>Page Title Goes here</title>
</head>"