You are on page 1of 13

EX.NO.

9 PHP FORM VALIDATION

AIM: To validate the form using PHP regular expression and store data into
database.

ALGORITHM:
Step 1: Create a PHP file with filename. extension.
Step 2: Include the following fields in the PHP form.
o Name
o Email
o Website
o Comment
o Gender
o Submit
Step 3: Do the validation using PHP regular expression function such as
preg_match.
Step 4: Run the xampp server and the get the output.
Program:
Index.php:
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {

1
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
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"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}

if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// check if URL address syntax is valid
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"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;

2
}
?>
<h2>Form Validation using Regular Epression</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 "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>

3
OUTPUT:

RESULT:

Thus the program for PHP form validation was executed successfully.

b) Database Connectivity:

index.php:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>

4
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$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"]);
// check if name only contains letters and whitespace
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"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}

if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// check if URL address syntax is valid
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"]);

5
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "iplab";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
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">

6
<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:

Thus the program for PHP form validation and storing in the database
executed successfully.

7
Ex.No:10

WEB SERVICE FOR FINDING PEOPLE’S OPINION ON A


CONSUMER

AIM:

To create a web service for finding people’s opinion on a consumer product in


Netbeans.

ALGORITHM:

Server:

1. Open the Netbeans IDE and create a Web Application project for server.
Select File- New Project. From ‘Categories’, select Java Web and from
‘Projects’, select
Web Application. Click Next.
Type a project name, ‘ProductReviewServer’, and click Next.
2. Right click the project and create a new web service from scratch in the web
application
project created in step 1.
3. A java file will be created. Add a web method in the web service to insert the
customer
review data to the database.
4. Run the server project.
5. Test the web service created. To do this, expand the web services folder in
the server
project, right-click the web service and click ‘Test web service’.
6. This deploys the application and opens up a test client in the browser.
Client:
1. Create a new Web Application project for client.
2. Right Click the client project and create new Web Service Client.
3. Specify the wsdl file of the web service created by browsing the server
project location.
4. Create a JSP page in the client project.
5. In that JSP page, using Web Services Client Resources, call Web Service
operation.

8
Add all the web service operations. (Right click the jsp page to find the option '
Web
Services Client Resources').
6. Design a form get the feedback from the user.
7. Now run the jsp page.

PROGRAM:

dbConnect.java:

package iplab;
import java.sql.*;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "dbConnect")
public class dbConnect {
Connection con;
@WebMethod(operationName = "connect")
public int connect(@WebParam(name = "name") String name,
@WebParam(name =
"mobile") int mobile, @WebParam(name = "email") String email,
@WebParam(name =
"rating") int rating) {
int status = -1;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ex10",
"sheela",
"12345");
PreparedStatement ps = con.prepareStatement("insert into productreview
values(?,?,?,?)");
ps.setString(1, name);
ps.setInt(2, mobile);
ps.setString(3, email);
ps.setInt(4, rating);
status = ps.executeUpdate();
return status;
} catch (Exception e) {
e.printStackTrace();
}
return status;
}

9
}
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Product Review</title>
</head>
<body>
<form action="review.jsp">
<img src="book_image.jpg" height="400" width="300"><br>
<input type="submit" value="Review Product" style="width:300px">
</form>
</body>
</html>
review.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Review</title>
</head>
<body>
<h1>Product Review!</h1>
<form action="review.jsp">
<input type="text" placeholder="Enter Username" name="name" required>
<input type="text" placeholder="Mobile Number" name="mobile" required>
<input type="text" placeholder="Mail id" name="email" required><br><br>
<h5>Rate your overall satisfaction with the product: </h5>
[1= very dissatisfied | 2= somewhat dissatisfied | 3=neutral | 4=somewhat
satisfied |
5= very satisfied]<br><br>
1 <input type="radio" id="1" name="rating" value="1">
2 <input type="radio" id="1" name="rating" value="2">
3 <input type="radio" id="1" name="rating" value="3">
4 <input type="radio" id="1" name="rating" value="4">
5 <input type="radio" id="1" name="rating" value="5">
<br><br>
<input type="submit" value="submit">
</form>
<%-- start web service invocation --%>
<%

10
try {
iplab.DbConnect_Service service = new iplab.DbConnect_Service();
iplab.DbConnect port = service.getDbConnectPort();
java.lang.String name = request.getParameter("name");
int mobile = Integer.parseInt(request.getParameter("mobile"));
java.lang.String email = request.getParameter("email");
int rating = Integer.parseInt(request.getParameter("rating"));
int result = port.connect(name, mobile, email, rating);
if(result>0){
response.sendRedirect("finalPage.html");
}
else{
out.println("Please try again");
}
} catch (Exception ex) {
ex.printStackTrace();
}
%>
<%-- end web service invocation --%>
</body>
</html>

finalPage.html:
<!DOCTYPE html>
<html>
<head>
<title>Feedback Submitted </title>
</head>
<body>
<h3>Thank you for reviewing our product..!!!</h3>
<a href="index.html">Goto Home Page</a>
</body>
</html>

OUTPUT:

11
12
13

You might also like