Internet Programming Lab Manual
Internet Programming Lab Manual
ENGINEERING - VILLUPURAM
(A Constituent College of Anna University, Chennai)
VILLUPURAM - 605103
NAME : …………………………
REG.NO : …………………………
DEPARTMENT : …………………………
YEAR / SEM : …………………………
UNIVERSITY COLLEGE OF ENGINEERING VILLUPURAM
(A CONSTITUENT COLLEGE OF ANNA UNIVERSITY CHENNAI)
DATE:
PROGRAM :
India.html
<html>
<head>
<title>Image map</title>
</head>
<body>
<map id="picture">
<area href="tamilnadu.html" shape="circle" coords="155,358,30"
alt="tamilnadu"\>
<area href="karnataka.html" shape="circle" coords="115,322,20" alt="karnataka"\>
<area href="andhrapradesh.html" shape="circle" coords="161,300,30"
alt="andhra pradesh"\>
<area href="kerala.html" shape="circle" coords="122,367,30" alt="kerala"\>
</map>
<img src=" india.jpg" alt="India" usemap="#picture"\>
</body>
</html>
Tamilnadu.html
<html>
<head>
<title>About Tamilnadu</title>
</head>
<body>
<center><h1>Tamilnadu</h1></center>
<hr/>
<ul>
<li>Area: 1,30,058 sq.kms</li>
<li>Capital: Chennai</li>
<li>Language: Tamil</li>
<li>Population: 6,21,10,839</li>
</ul>
</body>
</html>
Karnataka.html
<html>
<head>
<title>About Karnataka</title>
</head>
<body>
<center><h1>Karnataka</h1></center>
<hr/>
<ul>
<li>Area: 1,91,791 sq.kms</li>
<li>Capital: Bangalore</li>
<li>Language: Kannada</li>
<li>Population: 5,27,33,598</li>
</ul>
</body>
</html>
Andhrapradesh.html
<html>
<head>
<title >About Andhrapradesh</title>
</head>
<body>
<center><h1>Andhrapradesh</h1></center>
<hr/>
<ul>
<li>Area: 2,75,068 sq.kms</li>
<li>Capital: Hyderabad</li>
<li>Language: Telugu</li>
<li>Population: 7,57,27,541</li>
</ul>
</body>
</html>
Kerala.html
<html>
<head>
<title>About Kerala</title>
</head>
<body>
<center><h1>Kerala</h1></center>
<hr/>
<ul>
<li>Area: 38,863 sq.kms</li>
<li>Capital: Trivandrum</li>
<li>Language: Malayalam</li>
<li>Population: 3,18,38,619</li>
</ul>
</body>
</html>
OUTPUT:
Result:
EX:NO:2 WEB PAGES WITH CSS
DATE:
PROGRAM:
newcss.css
h1,h2{
text-decoration: underline;
font-style: italic;
text-align: center;
}
#body{
background-color: tan;
border: red dotted;
text-align: center;
}
.div{
border: peru solid;
}
*{
letter-spacing:1px;
}
a:link{
color: black;
}
a:visited{
color: yellow;
}
a:hover{
color: green;
}
a:active{
color: blue;
}
ul li{
font-size:small;
}
cssfile.html
<html>
<head>
<title>FLOWERS</title>
<!--Extended Style Sheet -->
<link rel="stylesheet" type="text/css" href="newcss.css">
<!--Embed Style sheet-->
<style type="text/css">
p{
background-color: lightgrey;
text-align: justify;
margin: 2em 7em;
}
</style>
</head>
<body id="body">
<h1>FLOWER</h1>
<p>
<span style="font: 200 x-large fantasy">Flower</span>
sometimes known as a bloom or blossom, is the
reproductive structure found in flowering plants.
The flower is God's finest workmanship in the
world. It is his finest gift to the mankind.We have
seen the flowers of many kinds and to many colors.
In India we see the flowers like
</p>
<!--Inline Style Sheet-->
<table style="background-position: center; text-align: center; padding: 3px;">
<tr>
<td align="left">
<div class="div">
<ul>
<li><a href="">Lily</a></li>
<li><a href="">Lotus</a></li>
<li><a href="">Rose</a></li>
<li><a href="">Jasmine</a></li>
</ul>
</div>
</td>
</tr>
</table>
</body>
</html>
OUTPUT:
Result:
EX:NO:3 HTML FORM VALIDATION USING JAVA SCRIPT
DATE:
PROGRAM:
Main.html:
<html>
<head>
<title> ONLINE BOOK STORES</title>
</head>
<body bgcolor="pink">
<marquee>
<h1 align=”center”>
<b><u><font color="white">ONLINE BOOK STORAGE</u></font> </b>
</h1>
</marquee>
<h2 align="center">
<b>
</h2>
<h3 align="center">
REGISTRATION FORM</font></i><br><br>
<a href="payment.html"><font
color="black"><i>PAYMENT</font></i><br><br>
</body>
</html>
Login.html:
<html>
<body bgcolor=”blue”>
<br><br><br>
<script language=”javascript”>
function validate(){
var flag=1;
flag=0;
}
if(flag = =1){
alert(“VALID INPUT”);
window.open("catalog.html","right");
}else{
alert(“INVALID INPUT”);
//document.myform.focus();
}
}
</script>
<form name=”myform”>
<div align=”center”>
<pre>
LOGIN ID: <input type=”text” name=”id”><br>
</body>
</html>
Reg.html:
<html>
<body bgcolor=”blue”>
<br><br>
<script language=”javascript”>
Function validate(){
var str = document.myform.phno.value;
var x;
if(flag= =1){
alert("VALID INPUT");
}else
{
alert("INVALID INPUT");
document.myform.focus();
}
}
</script>
<form name="myform">
<div align="center">
<pre>
NAME :<input type="text" name = "name"><br>
</div>
<br><br>
<div align="center">
<input type="submit" value="ok" onClick="validate()">
<input type="reset" value="clear">
</div>
</form>
</body>
</html>
Catalog.html:
<html>
<body bgcolor="pink">
<br><br><br>
<div align="center">
<pre>
</pre>
<br><br>
</div>
<br><br>
<div align="center">
</div>
</body>
</html>
Order.html:
<html>
<body bgcolor="pink">
<br><br><br>
<div align="center">
<pre>
</pre>
<br><br>
</div>
<br><br>
<div align="center">
</div>
</body>
</html>
Payment.html:
<html>
<body bgcolor="pink">
<br><br><br>
<script language="javascript">
function validate(){
var flag=1;
if(
document.myform.id.value = ="" ||
document.myform.pwd.value = ="" ||
document.myform.amount.value = ="" ||
document.myform.num.value = =""
){
flag=0;
}
var str=document.myform.amount.value;
var x;
x=str.substr(i,1);
if(!(x<=9)){
flag=0;
break;
}
str=document.myform.num.value;
for(var i=0; i<str.lenght ;i++){
x=str.substr(i,1);
if(!(x<=9)){
flag=0;
break;
}
}
if(flag = =1){
alert("VALID INPUT");
}else{
alert("INVALID INPUT");
document.myform.focus();
}
}
</script>
<form name="myform">
<div align="center">
<pre>
LOGIN ID :<input type="text" name="id"><br>
PASSWORD :<input type="password" name="pwd"><br>
AMOUNT :<input type="text" name="amount"><br>
</pre>
<br><br>
</div>
<br><br>
<div align="center">
</div>
</form>
</body>
</html>
Profile.html:
<html>
<body bgcolor="pink">
<br><br><br>
<script type="text/javascript">
function validate(){
var flag=1;
alert("VALID INPUT");
}else{
alert("INVALID INPUT");
document.myform.focus();
}
</script>
<form name="myform">
<div align="center">
<pre>
</div>
<br><br>
<div align="center">
</div>
</form>
</body> </html>
OUTPUT :
Result:
EX:NO:4a INVOKING SERVLETS FROM HTML FORMS
DATE:
0
PROGRAM
Form.html
<html>
<head>
<title>Student Information Form</title>
</head>
<body>
<center>
<form name="form1"
action="http://localhost:8080/examples/servlet/my_servletDemo">
<h3>Enter student information in following fields -</h3>
<table>
<tr>
<td><b>Roll Number</b></td>
<td><input type="text" name="Roll Number" size="25" value=" "></td>
</tr>
<tr>
<td><b>Student Name</b></td>
<td><input type="text" name="Student Name" size="25" value=" "></td>
</tr>
<tr>
<td><b>Student Address</b></td>
<td><input type="text" name="Address" size="50" value=" "></td>
</tr>
<tr>
<td><b>Phone</td>
<td><input type="text" name="Phone" size="25" value=" "></td>
</tr>
<tr>
<td><b>Total Marks</td>
<td><input type="text" name="Total Marks" size="10" value=" "></td>
</tr>
</table>
<input type="submit" value="submit">
</form>
</center>
</body>
</html>
my_servletDemo .java
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class my_servletDemo extends GenericServlet
{
public void service(ServletRequest req , ServletResponse res)
throws ServletException , IOException
{
PrintWriter out=res.getWriter();
Enumeration en=req.getParameterNames();
while(en.hasMoreElements())
{
String name_received=(String)en.nextElement();
out.print(name_received+ " = ");
String value_received = req.getParameter(name_received);
out.println(value_received);
out.println(" ");
}
out.close();
}
}
web.xml
Result:
EX:NO:4b SESSION TRACKING USING HIDDEN FORM FIELDS(cookies)
DATE:
Program:
register.html:
<html>
<center>
<h1>WELCOME TO REGISTRATION PAGE</h1>
<select name="profession">
<option value="engineer">ENGINEER</option>
<option value="teacher">TEACHER</option>
<option value="businessman">BUSINESSMAN</option>
</select><br><br>
</form>
</center>
</body>
</html>
web.xml:
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>register.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>RegistrationServletOne</servlet-name>
<servlet-class>RegistrationServletOne</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegistrationServletOne</servlet-name>
<url-pattern>/registerone</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>RegistrationServletTwo</servlet-name>
<servlet-class>RegistrationServletTwo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegistrationServletTwo</servlet-name>
<url-pattern>/registertwo</url-pattern>
</servlet-mapping>
</web-app>
RegistrationServletOne.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
response.setContentType("text/html");
out.println("<center>");
out.println("<h1>COMPLETE THE REGISTRATION</h1>");
out.println("</center>");
out.println("</body></html>");
out.close();
}}
RegistrationServletTwo.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
response.setContentType("text/html");
out.println("<h1>REGISTRATION SUCCESSFUL..........</h1>");
out.println("</center>");
out.println("</body></html>");
out.close();
}
OUTPUT:
Result:
EX:NO:4c SESSION TRACKING FOR HIT COUNT
DATE:
Program:
PageHitCounter.java:
import java.io.*;
import java.sql.Date;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class PageHitCounter extends HttpServlet {
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
response.setContentType("text/html");
// This method executes whenever the servlet is hit
// increment hitCount
hitCount++;
PrintWriter out = response.getWriter();
"</body> </html>");
}
}
web.xml:
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>PageHitCounter</servlet-name>
<servlet-class>PageHitCounter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PageHitCounter</servlet-name>
<url-pattern>/PageHitCounter</url-pattern>
</servlet-mapping>
</web-app>
OUTPUT:
✓ Create a servlet(i.e)PageHitCounter.java
✓ Compile the servlet by using the following command javac
PageHitCounter.java
✓ The class file will generated, copy this class file to the location C: \Program
Files\Apache Software Foundation\webapps\WEB-INF\classes
✓ Edit the web.xml file present at WEB-INF folder
✓ Open the web browser and give the URL for PageHitCounter.java
Result:
EX:NO:5 ONLINE EXAMINATION RESULTS USING SERVLET
DATE:
PROGRAM :
onlineexam.html
<html>
<head><title>Online Examination</title>
<script language="javascript">
function validation(Form_obj) {
if(Form_obj.Seat_no.value.length= =0){
alert("Please,fill up the Seat Number");
Form_obj.Seat_no.focus();
return false;
}
if(Form_obj.Name.value.length= =0){
alert("Please,fill up the Name");
Form_obj.Name.focus();
return false;
}
return
true;
}
</script>
</head>
<body bgcolor="lightgreen">
<center>
<h1>OnLine Examination</h1>
</center>
<form action="StudentServlet3" method="post" name="entry"
onSubmit="return validation(this)">
<input type="hidden" value="list" name="action">
<table>
<tr>
<td><h3>Seat Number:</h3></td>
<td><input type="text" name="Seat_no"></td>
</tr>
<tr>
<td><h3>Name:</h3></td>
<td><input type="text" name="Name" size="50"></td>
</tr>
<hr/>
<tr>
<td><b>Total Marks:10(Each question carries equal marks) </b></td>
<td></td>
<td></td>
<td></td>
<td><b>Time: 15 Min.</b></td>
</tr>
</table>
<hr/>
<b>1. Apache is an open source web server</b><br/>
<input type="radio" name="group1" value="True">True
<input type="radio" name="group1" value="False">False<br> <br>
<center>
<input type = "submit" value="Submit">
<input type = "reset" value="Clear"><br><br>
</center>
</form>
</body>
</html>
StudentServlet3.java
import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
Connection connect=null;
Statement stmt=null;
ResultSet rs=null;
public void doPost (HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
//Establishing Connection to the Database try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:StudentDB1";
connect = DriverManager.getConnection(url, " ", " ");
} catch(ClassNotFoundException e){
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();}
catch(Exception e){
e.printStackTrace();
}
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body bgcolor=pink>");
out.println("<center>");
out.println("<br><br>");
out.println("<h2>Students Database</h2>");
out.println("<table border=5>");
try
{
stmt = connect.createStatement();
String query = "SELECT * FROM StudentTable WHERE
Name="+"'"+Name+"'"; rs =
stmt.executeQuery(query);
out.println("<th>"+"Seat_no"+"</th>");
out.println("<th>"+"Name"+"</th>");
out.println("<th>"+"Marks"+"</th>");
while(rs.next()) {
out.println(" <tr>");
out.println(" <td>"+ rs.getInt(1) + "</td>");
out.println(" <td> "+ rs.getString(2) +" </td>");
out.println(" <td> "+rs.getString(3) +" </td>");
out.println(" </tr>");
}
out.println("</table>");
}catch(SQLException e){}
finally {
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(connect!=null)
connect.close();
}catch(SQLException
e){out.println("<h1>Thanks</h1>\n");
out.println("</table>");
out.println("</center>");
out.println("</body></html>");
}
}
}
web.xml
DATE:
Program:
Exam.jsp
<%@ page language="java" import="java.sql.*" %>
<%
a1= a2 = a3 = a4 = a5=0;
Connection connect=null;
Statement stmt=null;
ResultSet rs=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
if(request.getParameter("action") != null){
SeatNum = request.getParameter("Seat_no");
Name = request.getParameter("Name");
ans1 =request.getParameter("group1");
if(ans1.equals("True"))
a1=2;
else
a1=0;
ans2 = request.getParameter("group2");
if(ans2.equals("True"))
a2=0;
else
a2=2;
ans3 = request.getParameter("group3");
if(ans3.equals("True"))
a3=0;
else
a3=2;
ans4 = request.getParameter("group4");
if(ans4.equals("True"))
a4=2;
else
a4=0;
ans5 = request.getParameter("group5");
if(ans5.equals("True"))
a5=0;
else
a5=2;
int Total=a1+a2+a3+a4+a5;
stmt = connect.createStatement();
VALUES (' " +SeatNum + " ', ' " + Name + " ', ' "+Total+ " ')";
int result = stmt.executeUpdate(query);
stmt.close();
stmt = connect.createStatement();
%>
<html><head><title>Student Mark List</title>
</head>
<body bgcolor="khaki">
<center>
<h2>Students Marksheet</h2>
<tr>
<td><b>Seat_No</b></td>
<td><b>Name</b></td>
<td><b>Marks</b></td>
</tr>
<%while(rs.next() { %>
<tr>
<td><%=rs.getInt(1)%></td>
<td> <%=rs.getString(2)%></td>
<td> <%=rs.getString(3)%></td>
</tr>
<% }
rs.close();
stmt.close();
connect.close();
%>
</table>
</center>
<br/> <br/><br/>
<table>
<tr><td><b>Date:<%=new java.util.Date().toString()
%></td></tr>
<tr><td><b>Signature: X.Y.Z. <b></td></tr>
</table>
<div>
<a href="http://localhost:8080/jspconnectivity/Exam.jsp">Click
here to go back</a>
</div>
</body>
</html>
<html>
<head><title>Online Examination</title>
<script language="javascript">
function validation(Form_obj) {
if(Form_obj.Seat_no.value.length = =0){
return false;
}
if(Form_obj.Name.value.length= =0){
}
return true;
</script>
</head>
<form action="http://localhost:8080/jspconnectivity/Exam.jsp"
method="post"
name="entry" onSubmit="return validation(this)">
<tr>
<td><h3>Seat Number:</h3></td>
<td><input type="text" name="Seat_no"></td>
</tr>
<tr>
<td><h3>Name:</h3></td>
<tr>
<td><b>Total Marks:10(Each question carries equal marks)
</b></td>
<td></td>
<td></td>
<td></td>
<td><b>Time: 15 Min.</b></td>
</tr>
</table>
<hr/>
<br/>
value="False">False<br>
<br/>
value="False">False<br>
<br/>
value="False">False<br>
<hr/>
<center>
<input type = "submit" value="Submit">
</form>
<%}%>
OUTPUT :
Result:
EX:NO:7 ONLINE BOOKS CATALOGUE
DATE:
Program:
index.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
img:hover{
width:100%;
height:100%;
</style>
</head>
<body>
<h1>Books Catalogue</h1>
<form action="index.jsp">
name="category">
<option value="Maths">Maths</option>
<div id ="s1"></div>
<script>
function display() {
var m = document.getElementById("category").value;
XMLHttpRequest();
xhttp.onreadystatechange=function(){
{
document.getElementById("s1").innerHTML = this.responseText;
}
};
xhttp.open("GET","booklist.jsp?category="+m,true);//url rewriting
xhttp.send();
</script>
booklist.jsp:
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style>
.box {
display: flex;
align-items: center;
justify-content: center;
.box div {
width: 100%;
height: 100%;
.price {
color: grey;
font-size: 25px;
img{
width:20%;
}
</style>
</head>
<body>
<%
String category = request.getParameter("category");
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con=
DriverManager.getConnection("jdbc:derby://localhost:1527/shopping");
Statement st = con.createStatement();
String query="select bookid,author,price from catalogue where
category='"+category+"'";
ResultSet rs = st.executeQuery(query);
%>
<h1>List of Books</h1>
<div class="box">
<%
while(rs.next())
{%>
<div>
<img src="<%=rs.getString(1)%>.jpg"/>
<p>Author:<%=rs.getString(2)%></p>
<p class="price">Rs.<%=rs.getString(3)%></p>
</div> <%
}
con.close();
%>
</div>
</body>
</html>
Output:
Result:
EX:NO:8 EXTRACTING INFORMATION FROM THE XML
DATE:
Program:
UserInfo.xml
<!DOCTYPE html>
<html>
<head>
<title>Searching for XML Elements </title>
<script>
function readXMLData()
{
var xmlDocumentObject, id , name , addr, phone, email;
xmlDocumentObject=new XMLHttpRequest();
xmlDocumentObject.open("GET","userlist.xml",false);
xmlDocumentObject.send();
xmlDocumentObject=xmlDocumentObject.responseXML;
id = xmlDocumentObject.getElementsByTagName("userid");
name = xmlDocumentObject.getElementsByTagName("username");
address = xmlDocumentObject.getElementsByTagName("address");
phone = xmlDocumentObject.getElementsByTagName("phone");
email = xmlDocumentObject.getElementsByTagName("email");
for (i = 0; i < id.length; i++)
{
output=id[i].firstChild.nodeValue;
if (output == document.getElementById("myText").value)
{
displayDIV.innerHTML = id[i].firstChild.nodeValue + "<br> " +
name[i].firstChild.nodeValue + "<br> " +
address[i].firstChild.nodeValue + "<br> " +
phone[i].firstChild.nodeValue + "<br>" +
email[i].firstChild.nodeValue;
}
}
}
</script>
</head>
<body>
<h1>Search User</h1>
<input type="text" id="myText" value="">
<input type="button" value="Get User Details" onclick="readXMLData()">
<p>
<div id="displayDIV"> </div>
</body>
</html>
Output:
Result:
EX:NO:9a FORM VALIDATION USING PHP REGULAR EXPRESSION
DATE:
Program:
index.php:
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {
color: #FF0000;
}
</style>
</head>
<body>
<?php
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] = = "POST") {
if (empty($_POST["name"])) {
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-
a-z0-
9+&@#\/%=~_|]/i",$website)) {
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$gender = test_input($_POST["gender"]);
}}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<br><br>
$comment;?></textarea>
<br><br>
Gender:
<input type="radio" name="gender" <?php if (isset($gender) &&
$gender=="female") echo "checked";?> value="female">Female
</form>
<?php
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
Output:
Result:
EX:NO:9b STORING A FORM DATA IN PHP
DATE:
A
Program:
index.php
<!DOCTYPE html>
<html>
<head>
<style>
.error {
color: #FF0000;
}
</style>
</head>
<body>
<?php
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website =$result= "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-
9+&@#\/%=~_|]/i",$website))
{
$websiteErr = "Invalid URL";
}}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "iplab";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO register (name, email,website,comments,gender)
VALUES ('$name', '$email', '$website','$comment', '$gender')";
if ($conn->query($sql) = = = TRUE)
{
$result="New Record Inserted Successfully";
} else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Registration Form</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo $result;
?>
</body></html>
OUTPUT:
RESULT:
EX:NO:10 WEB SERVICE FOR FINDING PEOPLE’S OPINION
DATE:
Aim:
Program:
Index.php
<!Doctype html>
<html lang="en">
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<div class="wrap">
<?php
// retrive post
include('config.php');
include ('function.php');
dbConnect();
$query = mysql_query(
'SELECT *
FROM post
WHERE post_id = 1');
$row = mysql_fetch_array($query);
?>
<div class="post">
</div>
<?php
$comment_query = mysql_query(
"SELECT *
FROM comment
LIMIT 15");
?>
<h2>Comments.....</h2>
<div class="comment-block">
<div class="comment-item">
<div class="comment-avatar">
</div>
<div class="comment-post">
</div>
</div>
<?php endwhile?>
</div>
<label>
<span>Name *</span>
</label>
<label>
<span>Email *</span>
</label>
<label>
<span>Your comment *</span>
here...." required></textarea>
</label>
</form>
</div>
</body>
</html>
Ajax_Comment.php
<?php
include('config.php');
include('function.php');
dbConnect();
$name = mysql_real_escape_string($_POST['name']);
$mail = mysql_real_escape_string($_POST['mail']);
$comment = mysql_real_escape_string($_POST['comment']);
$postId = mysql_real_escape_string($_POST['postid']);
mysql_query("
INSERT INTO comment
?>
<div class="comment-item">
<div class="comment-avatar">
</div>
<div class="comment-post">
</div>
</div>
<?php
dbConnect(0);
endif
?>
Config.php
<?php
# db configuration
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'root');
define('DB_NAME', 'dbname');
?>
Function.php
<?php
/**
* @param bool
*/
function dbConnect($close=true){
if (!$close) {
mysql_close($link);
return true;
if (!mysql_select_db(DB_NAME, $link))
return false;
/**
* gravatar Image
* @see http://en.gravatar.com/site/implement/images/
*/
$url = "http://www.gravatar.com/avatar/";
return $url;
?>
Style.CSS
/* general styling */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-font-smoothing: antialiased;
-moz-font-smoothing: antialiased;
-o-font-smoothing: antialiased;
font-smoothing: antialiased;
text-rendering: optimizeLegibility;
}
body{
text-transform: inherit;
color: #333;
background: #e7edee;
width: 100%;
.wrap{
width: 720px;
background: white;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
overflow: hidden;
a{
text-decoration: none;
color: #333;
}
h1{
font-size: 2.8em;
text-align: center;
margin: 25px 0;
h2{
font-size: 1.5em;
margin: 8px 0
h3{
font-size: 1.2em;
margin: 5px 0;
h3 span{
font-weight: normal;
font-size: 1em;
.item{
clear: both;
margin:0;
padding: 10px;
overflow: hidden;
.item:last-child{
.item:hover{
background: #f9f9f9
.post{
padding: 10px 0;
.comment-block{
.comment-item{
overflow: hidden;
width: 500px;
clear: both;
padding: 10px;
border-radius: 5px;
margin: 5px;
.comment-avatar{
width: 60px;
float: left;
.comment-avatar img{
width: 60px;
height: 60px;
border-radius: 5px;
.comment-post{
width: 400px;
float: left;
#form{
clear: both;
margin: 10px;
width: 500px;
/* form styling */
input[type="text"],
input[type="email"],
input[type="tel"],
input[type="url"],
textarea {
width:100%;
background: #fff;
font-size: 13px;
line-height: 20px;
margin: 0;
margin:0 0 5px;
border-radius:5px;
textarea {
height:100px;
max-width:100%;
input[type="submit"] {
cursor:pointer;
width:100%;
border:none;
background:#991D57;
color:#FFF;
margin:0 0 5px;
padding:10px;
border-radius:5px;
input[type="submit"]:hover {
input[type="submit"]:active {
input:focus,
textarea:focus {
outline:0;
label{
display: block;
margin: 5px 0;
font-weight: 900;
cursor: pointer;
.alert{
display: none;
margin: 20px 0;
color: #468847;
background-color: #dff0d8;
border-color: #d6e9c6;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
Script.js
$(document).ready(function(){
var form = $('form');
form.on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'ajax_comment.php',
type: 'POST',
cache: false,
beforeSend: function(){
submit.val('Submitting...').attr('disabled', 'disabled');
},
success: function(data){
$('.comment-block').append(item);
form.trigger('reset');
submit.val('Submit Comment').removeAttr('disabled');
},
error: function(e){
alert(e);
});
});
})
Output:
Result: