PHP Module 2-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

WEB PROGRAMMING USING PHP

MODULE 2
MAJLIS ARTS AND SCIENCE COLLEGE, PURAMANNUR

DEPARTMENT OF COMPUTER SCIENCE

WEB PROGRAMMING USING PHP (5th Semester Online Study Material)

B.Sc Computer Science & BCA

(Questions and answers based on second Module)

1. JavaScript is developed by ------

Ans:Netscape

2. In JavaScript ----keyword is used to declare a function.

Ans: function

3. The blur event is the opposite of the ----event.

Ans: focus

4. Name various java script events.

Ans: onclick,ondblclick,onsubmit,Onreset,Onmousemove,Onkeypress,onload etc.

5. Java script is embedded between ----HTML tags.

Ans: <script>

6. What is an event? Give an example.

Ans: An event occurs when something happens.

JavaScript's interaction with HTML is handled through events that occur when the user or the browser
manipulates a page.

2 types

1. Window events : Which occur when something happens to a window.

e.g : load ,focus

2. User events : Which occur when the user interacts with elements in the page using pointing device.

2 types:

Mouse events eg : onclick


Keyboard events eg : onkeypress

7. Explain how to declare the variables in java script.

Ans:

variables are used to store data values.

JavaScript uses the var keyword to declare variables.

An equal sign is used to assign values to variables.

Eg:

var x;

x = 6;

In this example, x is defined as a variable. Then, x is assigned the value 6

8. What is a function? How can you define and call a function in java script?

Ans:

A JavaScript function is a block of code designed to perform a particular task.

A JavaScript function is defined with

the function keyword,

followed by a name,

followed by parentheses ().

Syntax:

function name(parameter 1, parameter 2,… ,parameter n)

// code to be executed

Eg:

function myFunction(p1, p2)

{
return p1 * p2;

A JavaScript function is executed when "something" invokes it (calls it).

When an event occurs (eg:when a user clicks a button)

When it is invoked (called) from JavaScript code

Example:

<HTML>

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

function myFunction ()

alert ("Hello JavaScripters!");

</SCRIPT>

</HEAD>

<BODY>

an event occurs

<button onclick=“myFunction() “/>

</BODY>

</HTML>

9. How will you insert a comment in java script?

Ans:

JavaScript comments can be used to explain JavaScript code, and to make it more readable.

It can also be used to prevent execution.

Two types:
1. Single Line Comments

2. Multi-line Comments

Single line comments

Single line comments start with //

Any text between // and the end of the line will not be executed.

Eg:

//this is an example code

Multi-line comments

Multi-line comments start with /* and end with */

Any text between /* and */ will be ignored by JavaScript.

Eg:

/*this is

an example*/

10. Explain popup boxes in java script.

1. Alert

An alert box is often used if you want to make sure information comes through to the user.

When an alert box pops up, the user will have to click "OK" to proceed.

Syntax

alert("sometext"); // window. alert("sometext");

Eg:

alert(”Javascript!");

2. Confirm

A confirm box is often used if you want the user to verify or accept something.

When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.

If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Syntax

confirm("sometext");

// window.confirm("sometext");

3. Prompt Box

A prompt box is often used if you want the user to input a value before entering a page.

When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering
an input value.

If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.

Syntax

prompt("sometext","defaultText");

// window.prompt("sometext","defaultText");

11. Write short notes on break and continue in java script.

break

jump out of a current loop

syntax: break;

continue

skip a part of a loop

syntax: continue;

12. Explain conditional statements in used in java script.

Ans:

Conditional Statements

1. Simple if
2. if.…else
3. else if Statement
4. Switch….case
Simple if
Syntax:
if (condition)
{
// block of code to be executed if the condition is true
}

The if statement specifies a block of code to be executed if a condition is true.


Eg:
<html>
<head>
<script language=“javascript”>
function check()
{
var a=5;
if(a == 5)
document.write(a);
}
</script></head>
<body>
<button onclick=“check()“>
</body></html>

If…else

Syntax

if (condition)

// block of code to be executed if the condition is true

else

// block of code to be executed if the condition is false

Eg:

<html>

<head>

<script language=“javascript”>
function check()

var a=50;

if(a % 2==0)

document.write(“Even number”);

else

document.write(“odd number”);

</script></head>

<body>

<button onclick=“check() “>

</body></html>

else if

Syntax

if (condition1)

// block of code to be executed if condition1 is true

else if (condition2)

// block of code to be executed if the condition1 is false and condition2 is true

else

// block of code to be executed if the condition1 is false and condition2 is false


}

Switch…case

Syntax

switch(expression)

case value 1:

// code block

break;

case value2:

// code block

break;

……..

……..

case valuen:

// code block

break;

default:

// code block

• The switch expression is evaluated once.


• The value of the expression is compared with the values of each case.
• If there is a match, the associated block of code is executed.
• If there is no match, the default code block is executed.
• expression can be of type numbers or strings.
• Duplicate case values are not allowed.
• The default statement is optional.
• The break statement is optional.
• The break statement is used inside the switch to terminate a statement sequence.
Example:

switch (new Date().getDay())


{
case 0:
day = “Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}

13. How do we write java script with HTML programming? Give an example.

Ans: 3 ways

1. In the <head> of a page


2. In the <body> section
3. In an external file

In the <head > of a page

a JavaScript function is placed in the <head> section of an HTML


page.

Eg:

<html>
<head>
<script language=“javascript”>
function myFunction()
{
document.write("Hello world");
}
</script>
</head>
<body>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>

In the <body> section

a JavaScript function is placed in the <body> section of an HTML page.

Eg:

<html>

<body>

<script language=“javascript”>

function myFunction()

document.write("hello world");

</script>

<h2>javascript in body section</h2>

<button type="button" onclick="myFunction()">Try it</button>

</body></html>

In an external file

External scripts are practical when the same code is used in many different web pages.

JavaScript files have the file extension .js

To use an external script, put the name of the script file in the src (source) attribute of a <script> tag.
External scripts can be referenced with a full URL or with a path relative to the current web page.

You can place an external script referencein <head> or <body> s you like.

External scripts cannot contain <script> tags.

Eg:

message.js

function msg()

document.write("Hello world");

<html>

<head>

<script type="text/javascript" src="message.js"></script>

</head>

<body>

<form>

<input type="button“ value=“click it” onclick="msg()“/>

</form>

</body>

</html>

14. Differentiate write and writeln methods in javascript.

Ans:

write() Method

used to write some content or JavaScript code in a document.

Syntax:

document.write( exp1, exp2, exp3, ... )


Eg:

document.write("<h1>Hello World!</h1>”);

document.write(a);

writeln() Method

used to write a document with additional property of newline character after each statement.

Syntax:

document.writeln( exp1, exp2, exp3, ... )

Eg:

document.writeln("<h1>Hello World!</h1>”);

document.writeln(a);

15. Describe various operators used in java script.

Ans:

1. Arithmetic Operators
2. Assignment Operators
3. String Operators
4. Comparison Operators
5. Logical Operators
6. Bitwise Operators
7. Conditional Operator

Arithmetic operators
Operator Description

+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
** Exponentiation
++ Increment
-- Decrement
Eg: 1
var x = 5;
var z = ++x; // var z = x++;
document.write(z);
Eg: 2
var x = 5;
var z = x ** 2;
// x ** y produces the same result as Math.pow(x,y)

Assignment opeartor
Operator Example Same As
= x=y x=y
+= x += y x=x+y
-= x -= y x=x–y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y

String opreators
The + operator can also be used to concatenate strings.
Example: 1
var txt1 = "John";
var txt2 = "Doe";
var txt3 = txt1 + txt2; // txt3=JohnDoe
Example: 2
var txt1 = "What a very ";
txt1 += "nice day"; // txt1=What a very nice day
Example: 3
var x = 5 + 5; // x=10
var y = "5" + 5; // y=55
var z = 5 + "5"; // z=55

Comparison operators

Operator Description Comparing Returns


eg: var x = 5;

== equal to x == 8 false
x == 5 true
x == "5“ true
=== equal value and equal type x === 5 true
x === "5“ false
!= not equal x != 8 true
!== not equal value or not equal type x !== 5 false
x !== "5“ true
x !== 8 true
> greater than x>8 false
< less than x<8 true
>= greater than or equal to x >= 8 false
<= less than or equal to x <= 8 true

Logical operators

Operator Description Example

Eg: var x=6; var y=3;

&& and (x < 10 && y > 1) is true


|| or (x == 5 || y == 5) is false
! Not !(x == y) is true

Bitwise operators
Operator Description Example Same as Result Decimal

& AND 5&1 0101 & 0001 0001 1


| OR 5|1 0101 | 0001 0101 5
~ NOT ~5 ~0101 1010 10
^ XOR 5^1 0101 ^ 0001 0100 4
<< left shift 5 << 1 0101 << 1 1010 10
>> right shift 5 >> 1 0101 >> 1 0010 2

Conditional operator
syntax:
variablename = (condition) ? value1:value2

Example:
varvoteable = (age < 18) ? "Too young":"Old enough";

16. Explain different types of loops in java script.

Loops can execute a block of code a number of times.

mainly two types of loops:

Entry Controlled loops:


In this type of loops the test condition is tested before entering the loop body.

eg: For Loop and While Loop

Exit Controlled Loops:

In this type of loops the test condition is tested or evaluated at the end of loop body. Therefore,
the loop body will execute atleast once, irrespective of whether the test condition is true or false.

eg: do .. while loop

for loop

Syntax:

for (initialization; testing condition; increment/decrement)

{ statement(s) }

Eg:

var text = ““;

var i;

for (i = 1; i < =5; i++)

text += i;

document.write(text);

while loop

Syntax

while(condition)

statement(s)

Eg:
var text = ““;

var i=1;

while(i<=5)

text += i;

i++;

document.write(text);

do…while loop

Syntax

do

statement(s)

} while(condition);

Eg:

var text = ““;

var i=1;

do

text += i;

i++;

}while(i<=5);

document.write(text);

17. Explain any four array methods in java script.

Ans:
sort() :sorts an array alphabetically

Example

var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.sort(); // Apple,Banana,Mango,Orange

reverse() :reverses the elements in an array.

[sort an array in descending order.]

Example

var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.sort();

fruits.reverse(); // Orange,Mango,Banana,Apple

concat() :creates a new array by merging (concatenating) existing arrays.

Example

varmyGirls = [“ammu", “minnu"];

varmyBoys = [“appu", “kichu“];

varmyChildren = myGirls.concat(myBoys);

// ammu, minnu, appu, kichu

slice(): slices out a piece of an array into a new array.

Example

var fruits = [“Banana", "Orange", "Lemon”];

var citrus = fruits.slice(1); // Orange,Lemon

[The slice() method creates a new array. It does not remove any elements from the source array.]

Example

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];

var citrus = fruits.slice(1, 3); // Orange, Lemon

18. Explain string methods in java script.


Ans:

A JavaScript string stores a series of characters.

Creating instance of a string

string_object_name = new String(value)

eg: mystring = new String(“Welcome to Majlis”);

alert(mystring);

String methods

Method purpose

big() displays string as if in a <big> element

bold() displays string as if in a <bold> element

italics() displays string as if in a <i> element

small() displays string as if in a <small> element

strike() displays string as if in a <strike> element

sub() displays string as if in a <sub> element

sup() displays string as if in a <sup> element

fontcolor()displays a string using a specified color(<font color= “green”>

fontsize() displays a string using a specified size

string.fontcolor("color")

indexOf() : returns the index of (the position of) the first occurrence of a specified text in a string

Example

mystring = new String(“Welcome to Majlis”);

pos = mystring.indexOf(“to"); // 8

lastIndexOf() : Returns the position of the last found occurrence of a specified value in a string

Eg:

mystring = new String(“Welcome to Majlis to CS”);


pos = mystring.lastIndexOf(“to");

substr() : Extracts the characters from a string, beginning at a specified start position, and through the
specified number of character.

Syntax

string.substr(start, length)

Start : The position where to start the extraction. First character is at index 0.

start>=length returns empty string

start –ve returns last character

length : Optional. The number of characters to extract. If omitted, it extracts the rest of the string

substring() : Extracts the characters from a string, between two specified indices

Syntax

string.substring(start, end)

Start : Required.

The position where to start the extraction.

First character is at index 0

End : Optional. The position (up to, but not including) where to end the extraction. If omitted, it
extracts the rest of the string

toLowerCase() Converts a string to lowercase letters

toUpperCase() Converts a string to uppercase letters

trim() Removes whitespace from both ends of a string

charAt() Returns the character at the specified index

19. Explain different events in java script.

Ans:

An event occurs when something happens.

2 types

Window events : Which occur when something happens to a window.


e.g : load ,focus

User events : Which occur when the user interacts with elements in the page using pointing device.

2 types:

Mouse events eg : onclick

Keyboard events eg : onkeypress

Window events

onload : when an object has been loaded

onunload : once a page has unloaded (or the browser window has been closed).

onresize : when the browser window is resized.

onerror : when an error occurs while loading an external file (e.g. a document or an image).

onfocus : the moment that the element gets focus.

[onfocus is most often used with <input>, <select>, and <a>.]

Onblur : the moment that the element loses focus.

[ The onblur is the opposite of the onfocus]

Onsubmit : when a form is submitted.

Onreset : when a form is reset.

Onchange : the moment when the value of the element is changed.

Oninput : when an element gets user input.

Onselect : after some text has been selected in an element.

Keyboard events

Onkeydown : The event occurs when the user is pressing a key.

Onkeyup : The event occurs when the user releases a key.

onkeypress : The event occurs when the user presses a key.

Example:
<html>

<body>

<input type="text" onkeypress="myFunction()“>

<script>

function myFunction()

alert("You pressed a key inside the input field");

</script>

</body>

</html>

Mouse events

Events that occur when the mouse interacts with the HTML document.

Onclick : The event occurs when the user clicks on an element

Ondblclick : The event occurs when the user double-clicks on an element

Onmousedown : The event occurs when the user presses a mouse button over an element.

Onmouseup : The event occurs when a user releases a mouse button over an element.

onmouseover : The event occurs when the pointer is moved onto an element.

Onmouseout : The event occurs when a user moves the mouse pointer out of an element.

Onmousemove : The event occurs when the pointer is moving while it is over an element.

Onmouseenter : The event occurs when the pointer is moved onto an element.

Eg:

<button onclick="myFunction()">

You might also like