Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Develop and demonstrate a XHTML file that includes JavaScript script for the following problems:
- a) Input : A number n obtained using prompt
- Output: The first n Fibonacci numbers
- /************************* Program1a.html *************************/
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Program to generate Fibonacci numbers</title>
- </head>
- <body>
- <script type="text/javascript">
- var f1=0;
- var f2=1;
- var f3=0;
- var limit=prompt("Enter the Limits\n","");
- if(limit!=null&&limit>0)
- {
- document.write("<h2><b/><i/>The Fibonacci Series for "+limit+"</h2>\n");
- if(limit==1)
- document.write(f1,"<br/>");
- else
- document.write(""+f1+"<br>"+f2+"<br/>");
- for(i=3;i<=limit;i++)
- {
- f3=f1+f2;
- document.write(""+f3+"<br/>");
- f1=f2;
- f2=f3;
- }
- }
- else
- alert("Please enter a valid number");
- </script>
- </body>
- </html>
- /*************************Output*************************/
- 1. b) Input : A number n obtained using prompt
- Output: a table of numbers from 1 to n and their squares using alert.
- /*************************Program1b.html *************************/
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <html >
- <body>
- <script type="text/javascript">
- var num = prompt("Enter a number : \n", "");
- if(num >0 && num !=null)
- {
- msgstr="Number and its Squares are \n";
- for(i=1;i <= num; i++)
- {
- msgstr = msgstr + i + " - " + i*i + "\n";
- }
- alert(msgstr);
- }
- else
- alert("No input supplied");
- </script>
- </body>
- </html>
- /*************************Output*************************/
- 2.a) Develop and demonstrate, using JavaScript script, a XHTML document that collects the USN ( the valid format is : A digit from 1 to 4 followed by two upper-case characters followed by two digits followed by two upper-case characters followed by three digits; no embedded spaces allowed) of the user. Event handler must be included for the form element that collects this information to validate the input. Messages in the alert windows must be produced when errors are detected.
- /*************************Program2a.html*************************/
- <?xml version="1.0" encoding="UTF-8"?
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www/w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Validate USN in Javascript</title>
- <script type="text/javascript">
- function validate_usn()
- {
- var str=document.getElementById("usn");
- var result=str.value.search(/^[1-4]{1}[A-Z]{2}\d{2}[A-Z]{2}\d{3}$/);
- if(result!=0)
- {
- alert("Entered usn is not in correct form");
- }
- else
- {
- alert("Entered usn ("+str.value+") is in correct form");
- }
- }
- </script>
- </head>
- <body bgcolor="lightpink">
- <form>
- <h3> Enter Your USN Number: </h3>
- <input type="text" id="usn">
- <br/>
- <br/>
- <input type="submit" onclick="validate_usn();" value="submit"/>
- <input type="reset" value="reset"/>
- </form>
- </body>
- </html>
- /*************************Output*************************/
- 2. b) Modify the above program to get the current semester also (restricted to be a number from 1 to 8).
- /*************************Program2b.html*************************/
- <?xml version="1.0" encoding="UTF-8"?
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www/w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Program to Validate USN and Semester</title>
- <script type="text/javascript">
- function validate_usn()
- {
- var str=document.getElementById("usn");
- var valid_usn=str.value.search(/^[1-4]{1}[A-Z]{2}\d{2}[A-Z]{2}\d{3}$/);
- var str1=document.getElementById("sem");
- var valid_sem=str1.value.search(/^[1-8]{1}$/);
- if(valid_usn>=0 && valid_sem>=0)
- {
- alert("Valid USN and Semester");
- }
- else if(valid_usn<0 && valid_sem >= 0)
- {
- alert("Invalid USN");
- }
- else if(valid_usn>=0 && valid_sem<0)
- {
- alert("Invalid Semester");
- }
- else
- {
- alert("Invalid USN and Semester");
- }
- }
- </script>
- </head>
- <body bgcolor="lightgreen">
- <form>
- <table>
- <tr>
- <td> <br/>Enter Your USN Number: </td>
- <td> <br/><input type="text" id="usn"></td>
- </tr>
- <tr>
- <td> Enter Your Semester : </td>
- <td> <input type="text" id="sem"></td>
- </tr>
- <tr>
- <td><input type="submit" onclick="validate_usn();" value="submit"/></td>
- <td><input type="reset" value="reset"/></td>
- </tr> </table>
- </form></body></html>
- /*************************Output*************************/
- 3. a) Develop and demonstrate, using JavaScript script, a XHTML document that contains three short paragraphs of text, stacked on top of each other, with only enough of each showing so that the mouse cursor can be placed over some part of them. When the cursor is placed over the exposed part of any paragraph, it should rise to the top to become completely visible.
- /*************************Program3a.html*************************/
- <?xml version="1.0" encoding="UTF-8"?
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www/w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Program to display Stacked Paragraphs</title>
- <link rel="stylesheet" type="text/css" href="Program3a.css"/>
- <script type="text/javascript">
- var topLayer="layer3";
- function stack(toTop)
- {
- var oldTop=document.getElementById(topLayer).style;
- var newTop=document.getElementById(toTop).style;
- oldTop.zIndex="0";
- newTop.zIndex="10";
- topLayer=document.getElementById(toTop).id;
- }
- </script>
- <body style="background-color:orange">
- <h3 style="text-align:center"> Program to demonstrate the usage of stacking of Paragraphs</h3>
- <div class="layer1Style" id="layer1" onmouseover="stack('layer1')">
- Welcome to SBMSIT. This college offers courses like computer science and engineering. Info science,Electrical and Electronics.
- </div>
- <div class="layer2Style" id="layer2" onmouseover="stack('layer2')">
- The College also offers courses like MTech - Computer science and Information Science and Master of Business Administration.
- </div>
- <div class="layer3Style" id="layer3" onmouseover="stack('layer3')">
- The college has an excellent infrastructure, beautiful ambience, separate library and admin blocks, A huge playground and a hostel facilities.
- </div>
- </body>
- </html>
- /*************************Program3a.css*************************/
- .layer1Style
- {
- border:solid thick black;
- padding:1em;
- width:500px;
- background-color:pink;
- position:absolute;
- top:100px; left:200px;
- }
- .layer2Style
- {
- border:solid thick black;
- padding:1em;
- width:500px;
- background-color:yellow;
- position:absolute;
- top:120px; left:220px;
- }
- .layer3Style
- {
- border:solid thick black;
- padding:1em;
- width:500px;
- background-color:cyan;
- position:absolute;
- top:140px;
- left:240px;
- }
- /*************************Output*************************/
- 3. b) Modify the above document so that when a paragraph is moved from the top stacking position, it returns to its original position rather than to the bottom.
- /**************Program3b.html************/
- <?xml version="1.0" encoding="UTF-8"?
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www/w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Program to display Stacked Paragraphs</title>
- <link rel="stylesheet" type="text/css" href="Program3a.css"/>
- <script type="text/javascript">
- var topLayer="layer3";
- var origPos;
- function stack(toTop,pos)
- {
- var newTop=document.getElementById(toTop).style;
- newTop.zIndex="10";
- topLayer=document.getElementById(toTop).id;
- origPos=pos;
- }
- function moveBack()
- {
- document.getElementById(topLayer).style.zIndex=origPos;
- }
- </script>
- <body style="background-color:orange">
- <h3 style="text-align:center"> Program to demonstrate the usage of stacking of Paragraphs</h3>
- <div class="layer1Style" id="layer1" onmouseover="stack('layer1','1')" onmouseout="moveBack();">
- Welcome to SBMST. This college offers courses like computer science and engineering. Info science, Electrical and Electronics.
- </div>
- <div class="layer2Style" id="layer2" onmouseover="stack('layer2','2')" onmouseout="moveBack();">
- The College also offers courses like MTech - Computer science and Information Science and Master of Business Administration.
- </div>
- <div class="layer3Style" id="layer3" onmouseover="stack('layer3','3')" onmouseout="moveBack();">
- The college has an excellent infrastructure, beautiful ambience, separate library and admin blocks, A huge playground and a hostel facilities.
- </div></body></html>
- /*************************Output*************************/
- 4. a) Design an XML document to store information about a student in an engineering college affiliated to VTU. The information must include USN, Name, Name of the college, Branch, Year of Joining, and e-mail identifies. Make up sample data for 3 students. Create a CSS style sheet and use it to display the document.
- /*************************Program4a.xml*************************/
- <?xml version = "1.0"?> <?xml-stylesheet type = "text/css" href = "Program4a.css" ?>
- <VTU>
- <student>
- STUDENT INFO
- <USN> 1BN08CS001 </USN>
- <name> Arun </name>
- <college> SBMSIT </college>
- <branch> CSE</branch>
- <YOJ> 2008 </YOJ>
- <email> arun@gmail.com </email>
- </student>
- <student>
- <USN> 1BN08CS002 </USN>
- <name> Gopal </name>
- <college> SBMSIT </college>
- <branch> CSE</branch>
- <YOJ> 2008 </YOJ>
- <email> gopal@gmail.com </email>
- </student>
- <student>
- <USN> 1BN08CS003</USN>
- <name> Sanjay </name>
- <college> SBMSIT </college>
- <branch> CSE</branch>
- <YOJ> 2008 </YOJ>
- <email> san@gmail.com </email>
- </student>
- </VTU>
- /************************* Program4a.css*************************/
- USN
- {
- display:block; color:blue; font-style:italic;
- }
- college
- {
- display:block;
- color:green;
- font-style:italic;
- }
- branch
- {
- display:block; color:red; font-style:italic;
- }
- email
- {
- display:block; color:blue; font-style:italic;
- }
- /*************************Output*************************/
- 4. b) Create an XSLT style sheet for one student element of the above document and use it to create a display of that element.
- /*************************Program4b.xml*************************/
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <?xml-stylesheet type="text/xsl" href="Program4b.xsl"?>
- <vtu>
- <student>
- <name>Praveen</name>
- <usn>1JT07IS007</usn>
- <college>JIT</college>
- <branch>Computer Science</branch>
- <year>2007</year>
- <email>praveen@jit.org</email>
- </student>
- <student>
- <name>Pradeep</name>
- <usn>1JT06IS045</usn>
- <college>JIT</college>
- <branch>Computer Science</branch>
- <year>2006</year>
- <email>pradeep@jit.edu</email>
- </student>
- <student>
- <name>Prakash</name>
- <usn>1JT05IS067</usn>
- <college>JIT</college>
- <branch>Computer Science</branch>
- <year>2005</year>
- <email>prakash@jit.in</email>
- </student>
- </vtu>
- /************************* Program4b.xsl *************************/
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:template match="/">
- <html>
- <body>
- <h2> VTU Student Information</h2>
- <table border="1">
- <tr bgcolor="#99cd32">
- <th>name</th>
- <th>usn</th>
- <th>collegeName</th>
- <th>branch</th>
- <th>year</th>
- <th>email</th>
- </tr>
- <xsl:for-each select="vtu/student">
- <xsl:choose>
- <xsl:when test="name = 'Praveen'">
- <tr bgcolor="#ff00ff">
- <td><xsl:value-of select="name"/></td>
- <td><xsl:value-of select="usn"/></td>
- <td><xsl:value-of select="collegeName"/></td>
- <td><xsl:value-of select="branch"/></td>
- <td><xsl:value-of select="year"/></td>
- <td><xsl:value-of select="email"/></td>
- </tr>
- </xsl:when>
- <xsl:otherwise>
- <tr>
- <td><xsl:value-of select="name"/></td>
- <td><xsl:value-of select="usn"/></td>
- <td><xsl:value-of select="collegeName"/></td>
- <td><xsl:value-of select="branch"/></td>
- <td><xsl:value-of select="year"/></td>
- <td><xsl:value-of select="email"/></td>
- </tr>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:for-each>
- </table>
- <h2> Selected Student is highlighted</h2>
- </body>
- </html>
- </xsl:template>
- </xsl:stylesheet>
- /*************************Output*************************/
- 5. a) Write a Perl program to display various Server Information like Server Name, Server Software, Server protocol, CGI Revision etc.
- Note:
- 1. Save the program /var/www/cgi-bin/ directory.
- 2.Set the cgi file permission before run the program (Change path in terminal :
- cd var/www/cgi-bin/ then :chmod 777 filename.cgi)
- /************************* Program5a.cgi *************************/
- #! /usr/bin/perl
- use CGI':standard';
- print "content-type:text/html\n\n";
- print "<html><head><title>Server Info<</title></head><body>";
- print "SERVER INFORMATION <br/><br/>";
- print "Server Name : $ENV{'SERVER_NAME'} <br/><br/>";
- print "Server Protocol : $ENV{'SERVER_PROTOCOL'} <br/><br/>";
- print "Server Software : $ENV{'SERVER_SOFTWARE'} <br/><br/>";
- print "CGI Version : $ENV{'GATEWAY_INTERFACE'} <br/><br/>";
- print "</body></html>";
- exit(0);
- /*************************Output*************************/
- 5. b) Write a Perl program to accept UNIX command from a HTML form and to display the output of the command executed.
- /************************* Program5b.html*************************/
- <?xml version="1.0" encoding="UTF-8"?
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www/w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <body>
- <form action="http://localhost/cgi-bin/Program5b.cgi" method="post">
- <h3> Enter the Unix Command! </h3>
- <input type="text" name="cmd">
- <input type="submit" value="Execute">
- </form>
- </body>
- </html>
- /*************************Program5b.cgi*************************/
- #! /usr/bin/perl
- use CGI':standard';
- print "Content-type:text/html\n\n";
- $cmd1=param('cmd');
- system($cmd1);
- exit(0);
- /*************************Output*************************/
- :
- 6. a) Write a Perl program to accept the User Name and display a greeting message randomly chosen from a list of 4 greeting messages.
- /*************************Program6a.html*************************/
- <html>
- <head><title> name </title></head>
- <body bgcolor="aabbcc">
- <h3> Enter name and receive the greetings </h3>
- <form action="/cgi-bin/Program6a.pl" method="get">
- Name:<input type="text" name="name"><br/><br/>
- <input type="submit" value="Submit">
- <input type="reset" value="Reset">
- </form>
- </body>
- </html>
- /*************************Program6a.pl*************************/
- #! /usr/bin/perl
- use CGI':standard';
- print "content-type:text/html","\n\n";
- $input=param(name);
- print "<html><body bgcolor=aabbcc><h3>Hi,$input</h3>";
- my @msgs=("Welcome to Web Programming","Welcome to Computer science","Thank you for visiting web page","Hello");
- print "<h3>The message received is: ";
- print $msgs[int rand scalar @msgs];
- print "</h3> </body> </html>";
- /*************************Output*************************/
- 6. b) Write a Perl program to keep track of the number of visitors visiting the web page and to display this count of visitors, with proper headings.
- please Change permissions going to System->Administration->SElinux management->Password “fedora” Change disabled to enforcing
- /************************* Program6b. *************************/
- #! /usr/bin/perl
- print "Content-type:text/html\n\n";
- $count_file="/var/www/cgi-bin/count.txt";
- if(open(FILE,'<'.$count_file))
- {
- $no_accesses=<FILE>;
- close(FILE);
- if(open(FILE,'>'.$count_file))
- {
- $no_accesses++;
- print FILE $no_accesses;
- close(FILE);
- print "no of visitors:",$no_accesses;
- }
- else
- {
- print "[cant write to data file]";
- }
- }
- else
- {
- print "[sorry]";
- }
- exit(0);
- *************************Output*************************/
- 7. Write a Perl Program to display a digital clock which displays the current time of the server.
- /*************************Program7.pl*************************/
- #!/usr/bin/perl
- $delay = 1;
- ($seconds,$minutes,$hour) = localtime (time);
- print "Refresh: ", $delay, "\n";
- print "Content-type: text/plain", "\n\n";
- print "Digital clock:\t";
- print "$hour:$minutes:$seconds"; exit(0);
- /*************************Output*************************/
- 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.
- Creating database and table in MYSQL:
- Go to terminal and type:
- #mysql
- mysql>create database DB_name; // To create Database
- mysql>use DB_name; //To use databse
- mysql>create table TB_name(name varchar(20),age int(2)); //To create
- table in the above Database.
- mysql>select * from TB_name;
- /************************* Program8.html*************************/
- <?xml version="1.0" encoding="UTF-8"?
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www/w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <body>
- <form action="http://localhost/cgi-bin/Program8.pl" method="post">
- <table>
- <tr>
- <td>Enter your name </td> <td><input type="text" name="name"></td>
- </tr>
- <td>Enter your age </td> <td><input type="text" name="age"></td>
- </tr>
- <tr>
- <td>Click here to send </td> <td><input type="submit" value="Send"></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- /************************* Program8.pl*************************/
- #! /usr/bin/perl
- print "Content-type: text/html\n\n";
- print "<HTML><TITLE>Result of the insert operation </TITLE>";
- use CGI ':standard';
- use DBI;
- $dbh=DBI->connect("DBI:mysql:test","root","");
- $name=param("name");
- $age=param("age");
- $qh=$dbh->prepare("insert into stud values('$name','$age')");
- $qh->execute();
- $qh=$dbh->prepare("select * from stud");
- $qh->execute();
- print "<table border size=1><tr><th>Name</th><th>Age</th></tr>";
- while ( ($name,$age)=$qh->fetchrow())
- {
- print "<tr><td>$name</td><td>$age</td></tr>";
- }
- print "</table>";
- $qh->finish();
- $dbh->disconnect();
- print"</HTML>";
- /*************************Output*************************/
- 9. Write a PHP program to store current date-time in a COOKIE and display the Last visited on data-time on the web page upon reopening of the same page.
- /*************************Program9.php*************************/
- <?php
- $inTwoMonths = 60*60*24*60+time();
- setcookie('lastVisit',date("h:i:s A - m/d/y"), $inTwoMonths);
- if(isset($_COOKIE['lastVisit']))
- $visit=$_COOKIE['lastVisit'];
- else
- echo "You've got some expired cookies!";
- echo "Your last visit was - ". $visit;
- ?>
- /*************************Output*************************/
- 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.
- /*************************Program10.php*************************/
- <?php
- session_start();
- if(isset($_SESSION['views']))
- $_SESSION['views']=$_SESSION['views']+1;
- else
- $_SESSION['views']=1;
- echo "Page views = ".$_SESSION['views'];
- ?>
- /*************************Output*************************/
- 11. Create a XHTML form with the 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.
- /*************************Program11.html*************************/
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML1 1.1//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1.dtdi">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Create of User Information</title>
- </head>
- <body>
- <form action="http://localhost/P11a.php" method="post">
- <pre> <center>
- <h1> ENTER THE USER DETAILS </h1>
- <table>
- <tr>
- <th>ENTER THE NAME</th>
- <td><input type="text" name="name1"/></td>
- </tr>
- <tr>
- <th>ENTER THE ADDRESS LINE 1</th>
- <td><input type="text" name="addline1"/></td>
- </tr>
- <tr>
- <th>ENTER THE ADDRESS LINE 2</th>
- <td><input type="text" name="addline2"/></td>
- </tr>
- <tr>
- <th>ENTER THE E-MAIL ADDRESS</th>
- <td><input type="text" name="email1"/></td>
- </tr>
- <tr>
- <td><input type="submit" value="submit"/>
- <td><input type="reset" value="reset"/>
- </tr>
- </table> </centre> </pre>
- </form>
- <form action="http://localhost/P11b.php" method="post">
- <pre>
- <center>
- <h1>TO VIEW THE USER DETAILS</h1><br/>
- <table>
- <tr>
- <th>ENTER THE NAME : </th>
- <td><input type="text" name="name1"/></td>
- </tr>
- <tr>
- <td><input type="submit" value="submit"/></td>
- <td><input type="reset" value="reset"/></td>
- </tr>
- </table> </center> </pre> </form>
- </body>
- </html>
- /*************************P11a.php*************************/
- <html>
- <body>
- <?php
- $con = mysql_connect("localhost","root","");
- if (!$con)
- {
- die('Could not connect: ' . mysql_error());
- }
- mysql_select_db("userinfo");
- $sql="INSERT INTO user (name, addr1, addr2, email)
- VALUES ('$_POST[name1]','$_POST[addline1]','$_POST[addline2]','$_POST[email1]')";
- if (!mysql_query($sql,$con))
- {
- die('Error: ' . mysql_error());
- }
- echo "Database has been Updated";
- mysql_close($con)
- ?>
- </body>
- </html>
- /*************************P11b.php*************************/
- <html>
- <head>
- <title>User information searching</title>
- </head>
- <body>
- <?php
- mysql_connect("localhost","root","") or die(mysql_error());
- mysql_select_db("userinfo") or die(mysql_error());
- $name1=$_POST['name1'];
- $res=mysql_query("select * from user where name like '$name1%'") or die(mysql_error());
- ?>
- <table border="2">
- <tr> <th>Name</th>
- <th>Address Line 1</th>
- <th>Address Line 2</th>
- <th>E-Mail Address</th>
- </tr>
- <?php
- while($array=mysql_fetch_row($res))
- {
- echo "<tr> <td>$array[0]</td>
- <td>$array[1]</td>
- <td>$array[2]</td>
- <td>$array[3]</td>
- </tr>";
- }
- ?>
- </table> </body>
- </html>
- /*************************Output*************************/
- s
- 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.
- IN windows only:
- Following will open :
- Select following :
- Following will open :
- START typing the following in rails prompt :
- rails -d mysql lab12
- it will create MVC structure
- cd lab12
- mysql -u root
- create database lab12_development;
- create database lab12_test;
- create database lab12_production;
- use lab12_development;
- create table books(id int not null auto_increment,
- accession_num varchar(100) not null,
- authors varchar(100) not null,
- title varchar(100) not null,
- edition varchar(100) not null,
- publisher varchar(100) not null,
- primary key(id));
- mysql> exit;
- type the following in cmd prompt:
- ruby script/generate scaffold Book accession_num:string authors:string title:string edition:string publisher:string
- wait for some time …
- ~~~~~~~~~~~~~~~~~~~~~
- START SEREVER :
- ruby script/server
- In browser type :
- http://localhost:3000
- Type table name in url (paste the below url in browser):
- http://localhost:3000/books
- For NEW BOOK ENTRY : (click on New book Link in the previous page)
- The new page appears as shown below.
- Enter the details and click create
- The following will display
- Click on Back link:
- For Searching a book based on name entered by the user:
- IF everything is ok again go to the same command prompt and stop the server by using ctrl+c:
- Create a controller called Home:
- In command prompt type: ruby script/generate controller home
- (it will create a file named home_controller.rb in controllers subdirectory of
- the application directory(rails_app/lab12/app/controllers/home_controller.rb).
- As well as it will also create folder named home within views directory of
- application directory (rails_app/lab12/app/views/home/)
- )
- open the home_controller.rb file using Notepad or wordpad or any other editors
- Add the following code within home_controller.rb file
- def search
- end
- def search_res
- @name=params[:search]
- @search_res=Book.find(:all, :conditions=>["title=?",@name])
- end
- finally the home_controller.rb file should look like this:
- class HomeController < ApplicationController
- def search
- end
- def search_res
- @name=params[:search]
- @search_res=Book.find(:all, :conditions=>["title=?",@name])
- end
- end
- ==============================================================================
- Go to: rails_app/lab12/app/views/home. Folder
- Create two files within the above folder as follows:
- 1.Search.html.erb or Search.rhtml
- 2.search_res.html.erb or search_res.rhtml
- (Notice the extension of these files <filename>.html.erb)
- Open the Search.html.erb or Search.rhtml using notepad or any other editor:
- Paste the following code within the file:
- <?xml version="1.0" encoding='utf-8'?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>Untitled Document</title>
- </head>
- <body>
- <% form_tag :action => 'search_res' do %>
- <h2>Search for books</h2>
- <table>
- <tr>
- <td>Book Name:</td>
- <td><input type="text" name="search" size="30" /></td>
- </tr>
- </tr>
- </table>
- <input type="submit" value="Search" />
- <input type="reset" value="Clear" />
- </p>
- <% end %>
- <a href="http://localhost:3000/books">back</a>
- </body>
- </html>
- Save the Search.html.erb or Search.rhtml file.
- ===========================================================================
- Open the Search_res.html.erb or Search_res.rhtml using notepad or any other editor:
- Paste the following code within the file:
- <?xml version="1.0" encoding='utf-8'?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>Untitled Document</title>
- </head>
- <body>
- <h2>Book search Result </h2>
- <table border= "1">
- <% if @search_res.blank? %>
- <p>There are no books currently by the given name .</p>
- <% else %>
- <tr><td>Accession No</td><td>Book Title</td><td>Authors</td><td>Edition</td><td>publisher</td></tr>
- <% @search_res.each do |book1|
- @bookasn=book1.accession_num
- @booktitle=book1.title
- @bookauthor=book1.authors
- @bookedition=book1.edition
- @bookpublisher=book1.publisher
- %>
- <tr> <td><%= @bookasn %></td> <td><%= @booktitle %></td> <td><%= @bookauthor%></td> <td><%= @bookedition%></td> <td><%= @bookpublisher%></td></tr>
- <% end %>
- <% end %>
- </table>
- <p />
- <a href="http://localhost:3000/home/search">Back</a>
- </body>
- </html>
- Save the Search_res.html.erb or Search_res.rhtml file.
- Finally open the index.html.erb file: (rails_apps/lab12/app/views/books/index.html.erb)
- Add the following code at the last line of the index.html.erb file:
- <a href="http://localhost:3000/home/search" style="color:red;text-transform:uppercase;">search book</a>
- It will create a link to search.
- -------------------------------------------------------------------------------
- ----------------------
- START SEREVER : (In command prompt)
- ruby script/server
- URL : (In browser)
- http://localhost:3000/books (you can see the link in red color SEARCH BOOK)
- Click on on the SEARCH BOOK link the following will display:
- Type computer graphics(title of book which is all ready inserted) and click search link:
- It will display the following result page:
- Now click back link in the page:
- Type noname(title of the book which is not in the database)in the edit box and click search.
- The following will display:
- THE END
- ===============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement