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

Iwt PT 04 Ans

The document contains a series of programming questions and answers related to PHP, XML, and XSLT. It covers topics such as array declarations in PHP, form validation using regular expressions, cookie management, and the creation of XML documents with DTD and XSD validation. Additionally, it includes examples of PHP scripts for user registration and displaying student data in a table format using XSLT.

Uploaded by

Dinesh0109
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)
6 views9 pages

Iwt PT 04 Ans

The document contains a series of programming questions and answers related to PHP, XML, and XSLT. It covers topics such as array declarations in PHP, form validation using regular expressions, cookie management, and the creation of XML documents with DTD and XSD validation. Additionally, it includes examples of PHP scripts for user registration and displaying student data in a table format using XSLT.

Uploaded by

Dinesh0109
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/ 9

NATIONAL ENGINEERING COLLEGEDEPARTMENT OF

CSE
19CS63C – INTERNET AND WEB TECHNOLOGIES
QUESTIONS- PREPARATORY TEST 04
Exam Date : 20/03/2025

1. How arrays are declared in PHP? Write a PHP script to demonstrate the usage of associative
array in PHP.

In PHP, arrays can be declared using the array() function or square brackets []. PHP supports three types of
arrays:

1. Indexed Array – Arrays with numeric keys.


2. Associative Array – Arrays with named keys.
3. Multidimensional Array – Arrays that contain another array as an element.

Example: Associative Array in PHP


php
<?php
// Creating an associative array
$student = array(
"name" => "John Doe",
"age" => 22,
"email" => "john@example.com",
"course" => "Computer Science"
);

// Accessing elements
echo "Student Name: " . $student["name"] . "<br>";
echo "Age: " . $student["age"] . "<br>";
echo "Email: " . $student["email"] . "<br>";
echo "Course: " . $student["course"] . "<br>";
?>

Explanation:

• The keys (name, age, email, course) are strings, making it an associative array.
• We use $array_name["key"] to access values.

2. Implement and login form validation with username, password and email fields usingPHP
regular expression.

PHP Regular Expressions (Regex )

Regular expressions are patterns used for string matching.


• preg_match(pattern, string) is used for validation.

PHP Code for Form Validation


php
CopyEdit
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];

$username_pattern = "/^[a-zA-Z0-9_]{5,15}$/";
$password_pattern = "/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/";
$email_pattern = "/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/";

if (!preg_match($username_pattern, $username)) {
echo "Invalid Username!<br>";
}

if (!preg_match($password_pattern, $password)) {
echo "Invalid Password!<br>";
}

if (!preg_match($email_pattern, $email)) {
echo "Invalid Email!<br>";
} else {
echo "Login Successful!";
}
}
?>
<form method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Login">
</form>

3. Create an E-mail registration form using PHP. The form should contain username, password,
Email id, gender, city using various input controls such as textboxes, radio button, combo boxes,
and submit button. When you click the submit button, the form data will be submitted into the
database.
<?php
$conn = new mysqli("localhost", "root", "", "user_db");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"];
$password = password_hash($_POST["password"], PASSWORD_BCRYPT);
$email = $_POST["email"];
$gender = $_POST["gender"];
$city = $_POST["city"];

$query = "INSERT INTO users (username, password, email, gender, city) VALUES ('$username',
'$password', '$email', '$gender', '$city')";

if ($conn->query($query) === TRUE) {


echo "Registration Successful!";
} else {
echo "Error: " . $conn->error;
}
}
?>
<form method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
Email: <input type="text" name="email"><br>
Gender: <input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female<br>
City: <select name="city">
<option value="New York">New York</option>
<option value="Los Angeles">Los Angeles</option>
</select><br>
<input type="submit" value="Register">
</form>
4. How to create/retrieve and delete a cookie in PHP? Develop a code to demonstrate the concept of
cookies.
A cookie is a small piece of data stored in the user's web browser. It helps websites remember user
preferences and login details for future visits.
• Create a Cookie:
setcookie("name", "value", time() + 3600, "/");
• Retrieve a Cookie:
$_COOKIE["name"];
• Delete a Cookie:
setcookie("name", "", time() - 3600, "/");

<?php
if (isset($_POST['set_cookie'])) {
setcookie("user", "John", time() + 3600, "/");
echo "Cookie 'user' is set!";
} elseif (isset($_POST['get_cookie'])) {
if (isset($_COOKIE["user"])) {
echo "Welcome, " . $_COOKIE["user"];
} else {
echo "Cookie is not set!";
}
} elseif (isset($_POST['delete_cookie'])) {
setcookie("user", "", time() - 3600, "/");
echo "Cookie 'user' is deleted!";
}
?>
<form method="post">
<button type="submit" name="set_cookie">Set Cookie</button>
<button type="submit" name="get_cookie">Get Cookie</button>
<button type="submit" name="delete_cookie">Delete Cookie</button>
</form>
5. Explain Document Type definition with its syntax. Validate the correctness of the below
XMLdocument using XML DTD with PCDATA and CDATA
<Item>
<StudentID> 010533 </StudentID>
<Name> Ramkumar </Name>
<Nationality> 01 </Nationality>
<Gender> L </Gender>
<State> 14 </State>
<Result> 2.60 </Result>
</Item>

DTD (Document Type Definition) defines the structure, elements, and attributes of an XML document. It
ensures that XML data follows a specific format, making it valid and consistent.

1. Syntax of DTD

DTD can be defined in two ways:

1. Internal DTD – Defined within the XML file.


2. External DTD – Stored in a separate .dtd file and linked to the XML file.

Internal DTD for Given XML Document:


xml
CopyEdit
<?xml version="1.0"?>
<!DOCTYPE Item [
<!ELEMENT Item (StudentID, Name, Nationality, Gender, State, Result)>
<!ELEMENT StudentID (#PCDATA)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Nationality (#PCDATA)>
<!ELEMENT Gender (#PCDATA)>
<!ELEMENT State (#PCDATA)>
<!ELEMENT Result (#PCDATA)>
]>
<Item>
<StudentID>010533</StudentID>
<Name>Ramkumar</Name>
<Nationality>01</Nationality>
<Gender>L</Gender>
<State>14</State>
<Result>2.60</Result>
</Item>
External DTD (student.dtd) for Given XML Document:
xml
CopyEdit
<!ELEMENT Item (StudentID, Name, Nationality, Gender, State, Result)>
<!ELEMENT StudentID (#PCDATA)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Nationality (#PCDATA)>
<!ELEMENT Gender (#PCDATA)>
<!ELEMENT State (#PCDATA)>
<!ELEMENT Result (#PCDATA)>
XML File (student.xml) Referencing External DTD:
xml
CopyEdit
<?xml version="1.0"?>
<!DOCTYPE Item SYSTEM "student.dtd">
<Item>
<StudentID>010533</StudentID>
<Name>Ramkumar</Name>
<Nationality>01</Nationality>
<Gender>L</Gender>
<State>14</State>
<Result>2.60</Result>
</Item>

6. Explain XML Scheme Definition with its syntax. Validate the correctness of the below XML
document using XSD (XML Scheme Definition).
XML Schema Definition (XSD) Explanation

XML Schema Definition (XSD) is used to define the structure and rules for an XML document. It ensures that
the XML document follows a predefined format by specifying:

• Elements and attributes


• Data types (string, integer, boolean, etc.)
• Restrictions on values (e.g., min/max length, patterns)
• Required or optional fields

XSD for the Given XML Document


xml
CopyEdit
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="studentsList">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
<xs:element name="certificate" type="xs:boolean"/>
<xs:element name="scores">
<xs:complexType>
<xs:sequence>
<xs:element name="module1" type="xs:integer"/>
<xs:element name="module12" type="xs:decimal"/>
<xs:element name="module3" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Validation of the Given XML Document


Issues in the Given XML Document

1. Attribute Name Mismatch:


o In the first <student> element, the ID attribute is named id="1", but in the second <student>, it is
ind="2" instead of id="2".
o Fix: Rename ind to id.
2. Data Type Mismatch:
o The <certificate> value is "True", but XSD defines it as xs:boolean.
o Fix: Use true or false in lowercase.

Corrected XML Document


xml
CopyEdit
<?xml version="1.0" encoding="UTF-8"?>
<studentsList>
<student id="1">
<firstName>Greg</firstName>
<lastName>Dean</lastName>
<certificate>true</certificate>
<scores>
<module1>70</module1>
<module12>80</module12>
<module3>90</module3>
</scores>
</student>
<student id="2">
<firstName>Wirt</firstName>
<lastName>Wood</lastName>
<certificate>true</certificate>
<scores>
<module1>80</module1>
<module12>80.2</module12>
<module3>80</module3>
</scores>
</student>
</studentsList>

7. Implement the XML document for student object and represent the student data as tableview using
XSL.
XML Document (student.xml)
xml
CopyEdit
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="student.xsl"?>
<Students>
<Student>
<ID>010533</ID>
<Name>Ramkumar</Name>
<Nationality>India</Nationality>
<Gender>Male</Gender>
<State>Tamil Nadu</State>
<Result>2.60</Result>
</Student>
<Student>
<ID>010534</ID>
<Name>Anjali</Name>
<Nationality>India</Nationality>
<Gender>Female</Gender>
<State>Karnataka</State>
<Result>3.50</Result>
</Student>
</Students>

XSL Document (student.xsl)


xml
CopyEdit
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Student Data</title>
<style>
table { width: 50%; border-collapse: collapse; margin: 20px 0; }
th, td { border: 1px solid black; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<h2>Student Data</h2>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Nationality</th>
<th>Gender</th>
<th>State</th>
<th>Result</th>
</tr>
<xsl:for-each select="Students/Student">
<tr>
<td><xsl:value-of select="ID"/></td>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="Nationality"/></td>
<td><xsl:value-of select="Gender"/></td>
<td><xsl:value-of select="State"/></td>
<td><xsl:value-of select="Result"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy