0% found this document useful (0 votes)
270 views9 pages

7-12 Web Lab Programs Vtu

The document provides code for a Rails application to manage a book database. It includes: 1) Code to generate a Book model with fields for accession number, title, author, edition, and publisher. 2) Code for a MainController with actions to display the number of books and search by title. 3) Views for the welcome and search results pages, including a form to search by title and display the results in a table. 4) Instructions for connecting to a MySQL database, generating the required files, and accessing the application pages for data entry and search.

Uploaded by

Reshma BJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
270 views9 pages

7-12 Web Lab Programs Vtu

The document provides code for a Rails application to manage a book database. It includes: 1) Code to generate a Book model with fields for accession number, title, author, edition, and publisher. 2) Code for a MainController with actions to display the number of books and search by title. 3) Views for the welcome and search results pages, including a form to search by title and display the results in a table. 4) Instructions for connecting to a MySQL database, generating the required files, and accessing the application pages for data entry and search.

Uploaded by

Reshma BJ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

7) Write a Perl program to display a digital clock which displays the current time of the

server.
7.html

<html>
<head>
<title>Digital Clock</title>
<body bgcolor="aabbcc">
<center>
<form action="/cgi-bin/7.cgi" method="post">Click here for digital clock<br/><br/>
<input type="submit" value="OK" /></form></br></br></br>
</center>
</center>
</body>
</head>
</html>
7.cgi

#!/usr/bin/perl
use CGI':standard';
print "refresh:1\n";
print "Content-type:text/html \n\n";
($s,$m,$h)=localtime(time);
print "digit clock<br/>";
print "$s seconds $m minutes $h hours";

8) Write a Perl program to insert name and age information entered by the
user into a table created using MySQL and to display the current contents
of this table.
8.html

<html>
<body bgcolor="orange">
<form action="/cgi-bin/8.cgi">
<h2>Name : <input type="text" name="pname"> <br/>
Age :<input type="text" name="age"> <br>
<input type="submit" value="Submit"></h2>
</form>
</html>
8.cgi

#!/usr/bin/perl
use CGI':standard';
use strict;
use DBI;
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

my $q=new CGI;
my $nm=$q->param("pname");
my $age=$q->param("age");
my $db1="reshma";
my $host="localhost";
my $user="root";
my $pwd="";
my $db= DBI->connect("DBI:mysql:reshma",$user,$pwd,{RaiseError => 1,AutoCommit =>
1}) or die "$DBI::errstr\n";
my $st="insert into resh1 values ('$nm','$age')";
$q=$db->prepare($st);
$q->execute();
$q=$db->prepare("select * from resh1");
$q->execute();
print "content-type: text/html \n\n",
"<html> <head><title>Database Information</title></head>",
"<body bgcolor=lightblue>",
"<h1>Inserting Of Record Successful!!!</h1><hr>",
"<center><h2>User Information</h2><hr>",
"<table border=1 cellpadding=10>",
"<tr><th><h2>NAME<th><h2>AGE</tr>";
while(($nm,$age)=$q->fetchrow())
{
print "<tr><td><h3>$nm<td><h3>$age</tr>";
}
$q->finish();
$db->disconnect();
print"</center></table></body></html>";

9) Write a PHP program to store current date-time in a COOKIE and


display the Last visited on date-time on the web page upon reopening of
the same page
9.html

<html xmlns="http://w3.org/1999/xhtml">
<head>
<title>cookie</title>
</head>
<body bgcolor="aabbcc">

<form action="cookie.php" method="POST">


<center>
<font size="6px" color="red"><b>Cookie program</b></font><br/><br/><hr/>
<br/><br/><br/><br/>
<font size="8px" color="blue"><b>Please Click Here For The Last Login Time</b></font>
<br/>
<input type="submit" name="submit" value="CLICK"/>
</center>
</form>
</body>
</html>
9.php

<html>
<body bgcolor="aabbcc">
<center>
<table border="5.0" width="250px" height="250px">
<tr>
<th>
<?php
$sys_time=60*60*24*60+time();
setcookie('cookie_time',date("h:m:s-d/m/y"),$sys_time);
if(isset($_COOKIE['cookie_time']))
{
$lastvisitedtime=$_COOKIE['cookie_time'];
echo "Last login time ", $lastvisitedtime;
}
else
{
echo "visited for the first time";
}
?>
</font>
</th>
</tr>
</table>
</center>
</body>
</html>

10) Write a PHP program to store page views count in SESSION, to


increment the count on each refresh, and to show the count on web page

10.html

<html>
<head>
<title>session</title>
</head>
<body bgcolor="aabbcc">
<form action="session.php" method="POST">
<center>
<font size="6px">Session Program</font>
<br/><br>
<font size="6px"><b>Please Click Here</b></font>
<br/><br/><br/>
<input type="submit" name="submit" value="click"/>
</center>
</form>
</body>
</html>
10.php

<html>
<body bgcolor="aabbcc">
<center>
<font size="5px"><b>You Visited This</b></font></br>
<table border="5.0" width="100px" height="100px">
<tr>
<td>
<?php session_start();
if(isset($_SESSION['session1']))
$_SESSION['session1']=$_SESSION['session1']+1;
else
$_SESSION['session1']=1;
echo " ",$_SESSION['session1'],"times";
?>
</td>
</td>
</table>
</body>
</html>
11) Create a XHTML form with Name, Address Line 1, Address Line 2, and
E-mail text fields. On submitting, store the values in MySQL table.
Retrieve and display the data based on Name.
11a.html

<html>
<head><title>Program 11</title></head>
<script type="text/javascript">
function disp()
{
document.location="11b.html";
}
</script>
<body>
<center>
<form action=11a.php method="get">
Name:<input type="text" name="name"><BR><BR>
Address 1:<input type="text" name ="add1"><BR><BR>
Address 2:<input type="text" name ="add2"><BR><BR>
email:<input type="text" name ="email"><BR><BR>
<input type="submit" value="submit"><BR><BR>
<input type="button" value="display by name" onclick="disp()"/>
</center>
</form>
</body>
</html>
11b.html
<html>
<head><title>Program 11</title></head>
<body>
<center>
<form action=11b.php method="get">
Name:< input type =text name="name"></br></br>
< input type ="submit" value="submit"></br></br>
</center>
</form>
</body>
</html>
11a.php
<html>
<head><title>Search result</title></head>
<body>
<hr>
<?php
$link=mysql_connect("localhost","root");
mysql_select_db("reshma");
$name=$_GET["name"];

$add1=$_GET["add1"];
$add2=$_GET["add2"];
$email=$_GET["email"];
$sql="INSERT INTO resh VALUES('$name','$add1','$add2','$email')";
$res=mysql_query($sql);
if(!$res)
{
die('could not enter data:'.mysql_error());
}
else
echo "entered data successfully\n";
mysql_close($link);
?>

11b.php
<html>
<head><title>Book Information</title></head>
<script type="text/javascript">
function disp1()
{
document.location="11b.html";
}
</script>
<body>
<?php
$connection=mysql_connect('localhost','root') or die('unable to connect');
#echo 'Select the database';
mysql_select_db("reshma");
#get the values from the html form
$name=$_GET['name'];
$q="Select * from resh where name='".$name."'";
$result=mysql_query($q);
#check if records were returned
echo '<center><h1>Contact details</h1></center>';
if(mysql_num_rows($result)>0)
{
echo '<table width=100% cellpadding=10 cellspacing=10 border=5 bordercolor="blue">';
echo '<tr>
<th>name
<th>add1
<th>add2
<th>email
</tr>';

while($row=mysql_fetch_row($result))
{
echo '<tr>';
echo '<td><center>'.$row[0].'</td>';
echo '<td><center>'.$row[1].'</td>';
echo '<td><center>'.$row[2].'</td>';
echo '<td><center>'.$row[3].'</td>';
}
echo '</table>';
}
else echo 'Record not found';
?>
<form action=11b.html>
<input type="button" value="back" onclick="disp1()"/>
</form>
</body>
</html>

12) Build a Rails application to accept book information viz. Accession


number, title, authors, edition and publisher from a web page and store
the information in a database and to search for a book with the title
specified by the user and to display the search results with proper
headings.

CONNECT TO MySQL

Launch the Rails Application.


i.
mysql -u root
ii.

create database P12_development;

iii.

use P12_development;

iv.

create table books (


id int not null auto_increment,
accno varchar(30) not null,
title varchar(50) not null,
author varchar(50) not null,
edition varchar(20),
publisher varchar(50),
primary key(id));

Type the command exit to exit the MySQL


v.

rails -d mysql P12

vi.

cd P12

vii.

ruby script/generate scaffold Book accno:string title:string


author:string edition:string publisher:string

viii. ruby script/generate controller main


ix.

Open the \\InstantRails-2.0win\rails_apps\P12\app\controllers\main_controller.rb and add the


following code.

class MainController < ApplicationController


def welcome
@num_books = Book.count
end
def result
@searchresults = Book.find(:all,:conditions => ["title =
?", params[:s]])
end
end
X. ruby script/server
copy this two files inside app/views/main

<!welcome.rhtml-->

<html>
<title> Welcome template for books </title>
<body>
<p> Total number of books = <%= @num_books %> </p>
<form action = "result" method="post">
Enter the title:&nbsp;&nbsp;&nbsp;&nbsp;<input type="text"
name="s" size="20"/>
<input type="submit" value="Search"/>
</form>
</body>
</html>

<!result.rhtml-->

<html>
<title> Welcome template for books </title>
<body>
<table border=1>
<% @searchresults.each do |b|
@ano = b.accno
@title = b.title

@author = b.author
@edition = b.edition
@publisher = b.publisher %>
<caption> Entered book is <%= @title%>
</caption>
<tr>
<th>Accn.No</th><th>Title</th><th>Author</th><th>Edition</th>
<th>Publisher</th></tr>
<tr align = "center">
<td> <%=@ano %></td>
<td> <%=@title %></td>
<td> <%= @author %> </td>
<td> <%= @edition %> </td>
<td> <%= @publisher %> <td>
</tr>
<% end %>
</table>
</body>
</html>

Open the Web Browser and type the command:


http://localhost:3000/books for inserting
for seraching purpose
http://localhost:3000/main/welcome

You might also like