Web Programming Chapter 4
Web Programming Chapter 4
Model Paper- 4
Section-A
I. Answer any four question. Each question carries two marks: (4 X 2 = 8)
4. What is window.alert() ?
Window.alert() is a method used to display an alert box with the specified message.
Example : window.alert("Hello, World!");
• 2. Multi-line Comments: Multi-line comments start with '/' and end with */'. They are used to add
comments to multiple lines of code.
Example : Multiline Comments
<?php
/*This is a multi-line comment
that spans multiple lines of code
$x = 5;
/* This is another multi-line comment */
?>
Section-B
II. Answer any four question. Each question carries five marks: (4 X 5 = 20)
function helpMessage(msgNo) {
document.getElementById('helperText').value = messages[msgNo];
}
</script>
</head>
<body>
<form>
<label>Enter Your Name: </label>
<input type="text" size="40" onfocus="helpMessage(0)"><br><br>
<label>Enter Address : </label>
<input type="text" size="40" onfocus="helpMessage(1)"><br><br>
<label>Enter Email : </label>
<input type="text" size="40" onfocus="helpMessage(2)"><br><br>
<label>Help Box : </label>
<input type="text" id="helperText" size="40"><br><br>
</form>
</body>
</html>
The following steps outline the general process of how web services work :
1. Service Publication: A provider publishes the description of the web service, including its Interface,
operations, and data formats, using a standardized format such as WSDL (Web Services Description
Language). This description is made available to potential service consumers.
2. Service Discovery: Potential consumers can discover and identify available web services through
service registries or directories, such as UDDI (Universal Description, Discovery, and Integration).
which provide information about the location and capabilities of web services.
3. Service Invocation: A consumer (client) of the web service initiates a request by sending a message to
the service endpoint. The request is typically formatted as an XML-based SOAP (Simple Object
Access Protocol) message, which includes the details of the operation to be performed and any
necessary input parameters.
4. Message Transmission: The SOAP message is transmitted over a network using standard
communication protocols, such as HTTP or HTTPS. This allows the message to reach the web service
provider's endpoint.
5. Service Processing: Upon receiving the request, the web service provider processes the incoming
message, performs the requested operation, and generates a response. The processing may involve
accessing data, executing business logic, or interacting with other systems.
6. Response Generation: The web service provider constructs a response message, typically in the form
of another SOAP message, containing the results of the requested operation or any relevant data.
7. Message Transmission: The response message is transmitted back to the consumer over the network,
following the same communication protocols used for the request.
8. Response Handling: The consumer receives the response message and processes the returned data or
results as needed for its own application logic.
10.Explain Variable Scope in PHP. Discuss the types of Variable Scope with examples.
Variable Scope in PHP refers to the part of a program where a variable can be accessed or referenced.
Understanding variable scope is crucial for effective programming. Variable scope determines the
accessibility or visibility of a variable within different parts of a PHP script. There are four main types of
variable scope in PHP: local, global, static, and superglobal.
1.Local Variables and local scope:
• Local Variables: Local variables in PHP are declared inside a function and can only be accessed
within that function. They are temporary and exist only for the duration of the function's
execution. Once the function ends, the local variable is destroyed, and its value is no longer
accessible.
• Local Scope: They are not the local scope can only be accessed within the function where they are
declared. They are not known outside of their function's scope, meanfunweawore they them from
outside the function in which they are declared.
Example
function testFunction() {
$localvar "I'm local"; // Local to testFunction
echo $localvar;
}
testFunction(); // Outputs: I'm local
// Outside the function
echo $localVar; // Error: Undefined variable: localvar
<?php
function display($fontSize ="medium", $theme = "light") {
return "Font Size: $fontSize, Theme: $theme";
}
echo display()."<br>";
echo display("large")."<br>";
echo display("large", "dark")."<br>";
Output:
Font Size: medium, Theme: light
Font Size: large, Theme: light
Font Size: large, Theme: dark
Section-C
III. Answer any four question. Each question carries eight marks: (4 X 8 = 32)
13.Explain Handling Events from Text box and Password Elements with an example.
Focus and Blur Events :
The focus and blur events are JavaScript events that are associated with text box and
password input elements.
1. The onfocus event is triggered when an element receives focus, such as when the user
clicks on it or tabs into it using the keyboard.
2. The onblur event is triggered when an element loses focus, such as when the user
clicks on another element or tabs out of the current element.
• These events can be used in web development to perform actions in response to user
input, such as validating user input or updating the user interface
<!DOCTYPE html>
<html>
<title>Focus and Blur Event</title>
<script type="text/javascript">
<head>
function checkUserName() {
var username = document.getElementById("nameText").value;
var message = '';
if (username.length < 8) {
message "User name cannot be less than 8 characters";
}
document.getElementById("usernameMessage").innerText message;
}
function displayPwdRules() {
var message = '';
message = "Password must be minimum 6 characters";
document.getElementById("passwordMessage").innerText = message;
}
</script>
</head>
<body>
<form>
<input type="text" id="nameText" onblur="checkUserName()"/>
<span id="usernameMessage" style="color:red;"></span><br/><br/>
<input type="password" id="pwdText" onfocus="displayPwdRules()">
<span id="passwordMessage" style="color: blue;"></span>
</form>
</body>
</html>
Onselect Event :
The onselect event is a JavaScript event that is triggered when the user selects text in an
input element, such as a text box or a text area. This event can be used to perform
actions in response to user input, such as updating the user interface or validating user
input.
Example :
Let us create one text box called age. It display the default value in the text box Select
Age". When we select the words in the box, onselect event will be triggend and it
should display that "Please Enter only integers
<!DOCTYPE html>
<html>
<head>
<title>Onselect Event Example</title>
<script type="text/javascript">
function showSelectMessage() {
alert("Please Enter only integers");
}
</script>
</head>
<body>
<form>
<label for="age">Age:</label>
<input type="text" id="age" value="Select Age" onselect="showSelectMessage()">
</form>
</body>
</html>
<!DOCTYPE html>
<title>Mouseup and Mousedown Example</title>
<img id="image" src="boy.jpg"
<html>
<head>
</head>
<body>
onmouseup="changeImage()"
onmousedown="revertImage()">
<script>
function changeImage() {
document.getElementById('image').src = 'girl.jpg';
}
function revertImage() {
document.getElementById('image').src = 'boy.jpg';
}
</script>
</body>
</html>
15. Explain the basic syntax of XML. What are the XML syntax rules? What is the
difference between well formed and valid XML documents?
XML Syntax :
An xml file consists of various elements. This section presents a brief introduction to key
terminologies used in relation to xml. This section shows how XML documents are constructed
and its syntax.
1.XML. Declaration:
• The XML declaration indicates that the document is written in XML and specifies which
Version of XML. The XML declaration, if included, must be on the first line of the document.
The XML declaration can also specify the language encoding for the document. In our
example, we specify that the document uses UTF-8 encoding. Although the XML declaration is
optional, the W3C recommends that we include it in our XML documents. In any case, we will
need the XML declaration to successfully validate our document.
2. Root Element :
• All XML documents must have one (and only one) root element. All other elements must be
nested inside this root element. In other words, the root element must contain all other elements
within the document. Therefore, the first tag in the document will always be the opening tag of
the root element (the closing tag will always be at the bottom of the document).
• XML documents contains an element which is the parent of all other child elements. This first
element of the document is called the root element.
Example : <books>
----------
----------
</books>
<books> is the root element of the XML document. It serves as the top-level element encloses all
other elements in the document. In this case, '<books" is the outermost encapsulating the entire
content of the XML document.
3. Child Elements :
These are the elements that are contained within the root element Elements are usual
represented by an opening and closing tag. Data and other elements reside between opening and
closing tag of an element.
Example
<books>
<book>
<title>Web programming</title>
<author> Srikanth.S</author>
</book>
</books>
• <book is a child element of the 'books>' element. It is nested within the '<books>' element and
serves as a direct descendant of the root element. In this context, '<book>' is a sub-element of
books>', representing a specific book within the collection.
• Within the books' element, '<title>' and '<author>' are child elements. They are nested within
the 'book' element and represent specific attributes or properties of the book. In this case,
titles and <author>' are sub-elements of '<bool>', providing details about the title and author of
the book, respectively.
• XML tags are similar as in html. Tag starts with < and ends with > characters. Tags can have
attributes to provide information about tag.
4.Empty Elements :
Empty elements in XML are elements that do not have any content, and they are represented
in a specific format. In XML, empty elements can be expressed using a self-closing tag, which
Combines the start tag and end tag into a single tag
Example
<book>
<title>Web programming</title>
<author/>
</books>
• The <author>' element is also a child element of '<bool>', but it is an empty element. It does not
have any content, and it is represented using a self-closing tag. This is also known as bodyless
tag. It has a start tag but doesnot have a matching endtag
• Empty elements are used in XML to represent elements that do not have any content. In this
case, the <author>' element is empty because it does not contain any text or child elements.
The use of a self-closing tag ('<author/>') indicates that the element is empty.
5.Elements:
• The content between start tag and end tag including tags is called element. An element can
contain other child elements.
Example
<book>
<title>Web programming</title>
<author> Srikanth.sc/author>
</books>
Here, book is a tag and it has two children title and author. The "<title>Web programming</ title>"
and "<author> Srikanth S</author>" are elements. The "Web programming and "Srikanth S" are
text.
6.Attributes:
• Elements can also contain one or more attributes. An attribute is a name/value pair, that we
place within an opening tag, which allows us to provide extra information about an element. We
may be familiar with attributes in XHTML. For example, the XHTML img tag requires the src
attribute which specifies the location of an image
Example
<img src="myImage.gif"/>
• XML elements can contain attributes which provides some additional information about element.
For example, book element contains "type" attribute and it is assigned value "technical".
<book type="technical">
</book>
• Attributes often provide information that is not a part of the data. Attribute values must al be
enclosed in either single or double quotes. If the value of the attribute contains d quotes then use
single quote to enclose the value of the attribute.
7. Comments:
XML. comments begin with <!-- and end with -->. Similar to XHTML comments, XML, comment
allow us to write comments within the document and cooments are ignored by XML. parsers We
normally write comments as an explanatory note to ourselves or another programmer
Comments can appear anywhere within the document.
Example </books>
XML Syntax Rules:
The syntx of XML is governed by a set of rules that define how XML documents should be
structured and formatted. The syntax rules for XML documents are discussed in this section .
1. All XML Documents must have root tag: All XML documents must contain a single tant pair to
define the vet sub (children) bier elements must be nested within the root element. All elements
can have sub (children) elements. Sub elements must be in pairs and correctly nested within their
parent element:
<root>
<child>
<subchild>
</subchild>
</child>
</root>
2. XML Is Case Sensitive: XML tags are case sensitive. The tag <Author> is different from the tag
<author>. Opening and closing tags must therefore be written with the same case:
3. All XML Elements must have a Closing Tag: In XML all elements must have a closing tag like this:
<p>This is a paragraph</p>
<p>This is another paragraph</p>
4. All XML Elements must be properly Nested: In XML all elements must be properly nested within
each other like this
5. Attribute Values must always be Quoted: XML elements can have attributes in name/value pairs
just like in HTML. In XML, the attribute value must always be quoted.
6. XML Restricts the Use of Certain Characters: XML tags use angle brackets (> and <symbols) for
enclosing tag names. Thus, these characters cannot appear in the element data. If we want to
include these symbols in our data, we must use replacement character sequences. The below
table lists all special characters and their corresponding replacement character sequences in XML.
7. Elements in an XML: Document Can be Nested: An XML document typically consists of several
element declarations. The document starts with a root element, and all other elements appear
under this root. The element declarations can be nested; however, we must observe proper
scoping while nesting the elements. A partial overlap during nesting is not permitted
• After the first character, numbers are allowed as are the "-" and "." characters.
Correct: <K-12 First Grade</K-12>
Correct: cwww.gmail.com></www.gmail.com>
2. Valid Document:
• A valid XML document not only meets the criteria for being well-formed but also conform to a
specific Document Type Definition (DTD) or XML Schema.
• It must adhere to the rules and structure defined in the DTD or XML Schema, including the
allowed elements, attributes, and their arrangement.
• Validation ensures that the document complies with the constraints and rules specified in the
DTD or XML Schema, providing a higher level of structural integrity and data consistency.
• Validation can be performed using an internal DTD, an external DTD, or an XML Schema.
16.Elobrate the process of performing database operations in PHP, including
querying, inserting, updating, and deleting records.
Connecting to Database:
Connecting to a database in PHP is a fundamental step when building dynamic web applications
that interact with databases. Databases store and manage data, and PHP provides various
database extensions and libraries to connect and work with different types of databases.
Connecting to a database in PHP typically involves using the PHP Data Objects (PDO) extension
or MySQLI (MySQL Improved) extension. PDO provides a data-access abstraction layer, which
means we can use the same functions to issue queries and fetch data regardless of the database
we are using. MySQLi is a PHP extension specifically designed for use with MySQL databases.
18.Write a PHP program to find the sum, subtraction, multiplication, and division
of two number using switch statement.
<!DOCTYPE HTML>
<html>
<head>
<title>PHP Program To Calculate Arithmetic operations using Switch Case</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter 1st number"/> </td>
</tr>
<tr>
<td> <input type="text" name="num2" value="" placeholder="Enter 2nd number"/> </td>
</tr>
<tr>
<td> <input type="text" name="option" value="" placeholder="Enter any option"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$a = $_POST['num1'];
$b = $_POST['num2'];
$ch = $_POST['option'];
echo "1 -> Addition<br>";
echo "2 -> Subtraction<br>";
echo "3 -> multiplication<br>";
echo "4 -> Divison<br>";
switch($ch)
{
case 1: $r = $a+$b;
echo " Addition = ".$r ;
break;
case 2: $r = $a-$b;
echo " Subtraction = ".$r ;
break;
case 3: $r = $a*$b;
echo " Multiplication = ".$r ;
break;
case 4: $r = $a/$b;
echo " Division = ".$r ;
break;
default: echo ("invalid option\n");
}
return 0;
}
?>
</body>
</html>