0% found this document useful (0 votes)
5 views13 pages

8 Prcalliwd

Uploaded by

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

8 Prcalliwd

Uploaded by

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

226370307045

Practical 8.2
Aim :- Extend practical - 8(i) to validate user information using regular
expressions.

<html>
<body>
<?php
// define variables to empty values
$nameErr = $emailErr = $mobilenoErr = $genderErr = $addressErr="";
$name = $email = $mobileno = $gender = $address="";

//Input fields validation


if ($_SERVER["REQUEST_METHOD"] == "POST") {

//String Validation
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = input_data($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only alphabets and white space are allowed";
}
}

//String Validation
if (empty($_POST["address"])) {
$addressErr = "Address is required";
} else {
226370307045

$address = input_data($_POST["address"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$addressErr = "Only alphabets and white space are allowed";
}
}

//Email Validation
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = input_data($_POST["email"]);
// check that the e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}

//Number Validation
if (empty($_POST["mobileno"])) {
$mobilenoErr = "Mobile no is required";
} else {
$mobileno = input_data($_POST["mobileno"]);
// check if mobile no is well-formed
if (!preg_match ("/^[0-9]*$/", $mobileno) ) {
$mobilenoErr = "Only numeric value is allowed.";
}
//check mobile no length should not be less and greator than 10
if (strlen ($mobileno) != 10) {
$mobilenoErr = "Mobile no must contain 10 digits.";
226370307045

}
}

//Empty Field Validation


if (empty ($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = input_data($_POST["gender"]);
}
}
function input_data($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<h2>Registration Form</h2>
<span class = "error">* required field </span>
<br><br>
<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>
Address :
<textarea name="address" cols="10" rows="5"></textarea><br><br>
<span class="error">* <?php echo $addressErr; ?> </span>
<br><br>
226370307045

E-mail:
<input type="text" name="email">
<span class="error">* <?php echo $emailErr; ?> </span>
<br><br>
Mobile No:
<input type="text" name="mobileno">
<span class="error">* <?php echo $mobilenoErr; ?> </span>
<br><br>
Gender:
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
<input type="radio" name="gender" value="other"> Other
<span class="error">* <?php echo $genderErr; ?> </span>
<br><br>
<input type="submit" value="Display" name="submit">
</form>

<?php
if(isset($_POST['submit'])) {
if($nameErr == "" && $emailErr == "" && $mobilenoErr == "" && $genderErr == "")
{echo "<h3 color = #FF0001> <b>You have sucessfully registered.</b> </h3>";
echo "<h2>Your Input:</h2>";
echo "Name: " .$name;
echo "<br>";
echo "Address : ".$address;
echo "<br>";
echo "Email: " .$email;
echo "<br>";
echo "Mobile No: " .$mobileno;
echo "<br>";
226370307045

echo "Gender: " .$gender;


} else {
echo "<h3> <b>You didn't filled up the form correctly.</b> </h3>";
}
}
?>
</body>
</html>
226370307045

Practical 8.3
Aim :- Create two distinct web pages to demonstrate information passing
between them using URL - Get method.

Pra8.3.php
<html>
<h3>String Query</h3>
</html>
<?php
echo"<a href = 'string.php?a=12 & b= 1234'>"."click here";
?>

String.php
<?php
echo"Value of a : ";
echo "$_GET[a]"."<br>";
echo"Value of b : ";
echo "$_GET[b]";
?>
226370307045

Output:
22637307045

Practical 8.4
Aim :- Create two different web pages to demonstrate information passing
between web pages using Hidden variables – Post method.
22637307045

Practical 8.4
Aim :- Create two different web pages to demonstrate information passing
between web pages using Hidden variables – Post method

Pra8.4.php
<html>
<body>
<center>
<h3>INFORMATION</h3>
<form method="POST" action="hide.php">
<table border="4" align="center">
<tr>
<td>Name : </td>
<td><input type="text" name="name"></td>
</tr>

<tr>
<td>Password : </td>
<td><input type="password" name="password"></td>
</tr>

<tr>
<td><input type="hidden" name="ID" value="40">
<input type="submit" name="submit"></td>
</tr>
</table>
</form>
</center>
</body>
22637307045

</html>

hide.php
<?php
echo "ID = ".$_POST['ID']."<br>";
echo "Name = ".$_POST['name']."<br>";
echo "Password = ".$_POST['password']."<br>";
?>
226370307045

Practical 8.1
Aim :- Create a web page using a form to collect employee information.

<html>
<h3> HTML input form </h3>
<body>
<form method="POST" target="_blank">
First Name : <input type="text" name="f_name"><br><br>
Address : <textarea name="address" cols="10" rows="5"></textarea><br><br>
Email : <input type="text" name="email"><br><br>
Gender :
Male <input type="radio" name="gender" value="Male">
Female <input type="radio" name="gender" value="Female"><br><br>
Mobile no: <input type="tel" name="mobile_no"><br><br>
<input type="submit" value="Display" name="submit">

</form>
</body>
</html>

<?php
if (isset($_POST['submit'])) {
$f_name = $_POST['f_name'];
$address = $_POST['address'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$mobile_no = $_POST['mobile_no'];

echo " First Name : ".$f_name."<br>";


echo " Address : ".$address."<br>";
226370307045

echo " Email : ".$email."<br>";


echo "Gender : ".$gender."<br>";
echo " Mobile no. : ".$mobile_no."<br>";
}
?>
226370307045

You might also like