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

3rd Chap of Internet

The document contains various code snippets demonstrating file handling in PHP, including writing, reading, appending, and deleting files, as well as handling user input. It also explains the Document Object Model (DOM) and Browser Object Model (BOM) in HTML, provides JavaScript examples for variable usage, condition checking, and form validation, and discusses cookies in PHP. Additionally, it includes sample code for creating and storing cookies in PHP.

Uploaded by

arindamghorui03
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 views9 pages

3rd Chap of Internet

The document contains various code snippets demonstrating file handling in PHP, including writing, reading, appending, and deleting files, as well as handling user input. It also explains the Document Object Model (DOM) and Browser Object Model (BOM) in HTML, provides JavaScript examples for variable usage, condition checking, and form validation, and discusses cookies in PHP. Additionally, it includes sample code for creating and storing cookies in PHP.

Uploaded by

arindamghorui03
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

Write down PHP program on file handling and I/O handling?

=>
<?php

// File Handling in PHP

// Specify the file name

$filename = "sample.txt";

// Wri ng to a file

echo "Wri ng to a file...\n";

$content = "Hello, this is a sample file.\nThis is the second line of text.";

file_put_contents($filename, $content);

echo "File wri en successfully.\n";

// Reading from a file

echo "\nReading from the file...\n";

if (file_exists($filename)) {

$fileContent = file_get_contents($filename);

echo "File Content:\n$fileContent\n";

} else {

echo "File does not exist.\n";

// Appending to a file

echo "\nAppending to the file...\n";

$addi onalContent = "\nThis is an appended line.";

file_put_contents($filename, $addi onalContent, FILE_APPEND);

echo "Content appended successfully.\n";

// Reading the updated file

echo "\nReading the updated file...\n";

$fileContent = file_get_contents($filename);
echo "Updated File Content:\n$fileContent\n";

// Dele ng the file

echo "\nDele ng the file...\n";

if (file_exists($filename)) {

unlink($filename);

echo "File deleted successfully.\n";

} else {

echo "File does not exist.\n";

// I/O Handling Example

echo "\nI/O Handling Example:\n";

echo "Enter your name: ";

$name = trim(fgets(STDIN)); // Accept user input from the terminal

echo "Hello, $name! Welcome to PHP File Handling.\n";

?>

What is DOM,BOM in HTML?


=>Document Object Model (DOM) is a programming interface for HTML and XML documents, that
allows to create, manipulate, or delete the element from the document. It defines the logical
structure of documents and the way a document is accessed and manipulated. With the help of
DOM, the webpage can be represented in a structured hierarchy, i.e., we can easily access and
manipulate tags, IDs, classes, A ributes, or Elements of HTML using commands or methods provided
by the Document object, that will guide the programmers and users to understand the document in
an easier manner.
Browser Object Model (BOM) is a browser-specific conven on referring to all the objects exposed by
the web browser. The BOM allows JavaScript to “interact with” the browser. The window
object represents a browser window and all its corresponding features. A window object is created
automa cally by the browser itself. Java Script’s window.screen object contains informa on about
the user’s screen. It can also be wri en without the window prefix.

Write sample program using variables, comparison, condi on checking, itera on using
javascript?
=>// Using Variables

let number = 10;

console.log("The number is:", number);


// Comparison

if (number > 5) {

console.log(`${number} is greater than 5.`);

} else {

console.log(`${number} is less than or equal to 5.`);

// Condi on Checking with Mul ple Cases

let day = "Monday";

if (day === "Monday") {

console.log("It's the start of the workweek!");

} else if (day === "Saturday" || day === "Sunday") {

console.log("It's the weekend! Time to relax.");

} else {

console.log("It's a regular weekday.");

// Itera on: Using a for loop

console.log("\nNumbers from 1 to 5 using a for loop:");

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

console.log(i);

// Itera on: Using a while loop

console.log("\nCountdown from 3 using a while loop:");

let count = 3;

while (count > 0) {

console.log(count);

count--;
}

// Working with Arrays

let fruits = ["Apple", "Banana", "Cherry"];

console.log("\nList of fruits:");

fruits.forEach((fruit) => {

console.log(fruit);

});

// Adding an element to the array

fruits.push("Date");

console.log("\nA er adding 'Date':", fruits);

// Checking if an element exists in the array

if (fruits.includes("Banana")) {

console.log("\nBanana is in the list.");

} else {

console.log("\nBanana is not in the list.");

// Removing an element from the array

let removedFruit = fruits.pop(); // Removes the last element

console.log("\nRemoved fruit:", removedFruit);

console.log("Updated list of fruits:", fruits);

What is event? Write few javascript events. What is the syntax of javascript func on.
=>
JavaScript Events are ac ons or occurrences that happen in the browser. They can be
triggered by various user interac ons or by the browser itself.

onclick Triggered when an element is clicked.


onmouseover Fired when the mouse pointer moves over an element.

onmouseout Occurs when the mouse pointer leaves an element.

onkeydown Fired when a key is pressed down.

onkeyup Fired when a key is released.

onchange Triggered when the value of an input element changes.

onload Occurs when a page has finished loading.

onsubmit Fired when a form is submi ed.

A JavaScript func on is defined with the func on keyword, followed by a name, followed by
parentheses ().

Func on names can contain le ers, digits, underscores, and dollar signs (same rules as
variables).
The parentheses may include parameter names separated by commas:
(parameter1, parameter2, ...)
The code to be executed, by the func on, is placed inside curly brackets: {}
func on name(parameter1, parameter2, parameter3) {
// code to be executed
}
Write javascript func on to implement blank field valida ons of various form elements.
=>
// Func on to validate blank fields
func on validateForm() {
// Get form elements by ID or name
const nameField = document.getElementById("name");
const emailField = document.getElementById("email");
const messageField = document.getElementById("message");

const termsCheckbox = document.getElementById("terms");


const genderRadio = document.getElementsByName("gender");

let isValid = true;


let errorMessage = "";

// Check if the name field is blank


if (nameField.value.trim() === "") {
errorMessage += "Name field cannot be empty.\n";
isValid = false;
}

// Check if the email field is blank


if (emailField.value.trim() === "") {
errorMessage += "Email field cannot be empty.\n";
isValid = false;
}

// Check if the message textarea is blank


if (messageField.value.trim() === "") {
errorMessage += "Message field cannot be empty.\n";
isValid = false;
}

// Check if the terms checkbox is not checked


if (!termsCheckbox.checked) {
errorMessage += "You must agree to the terms and condi ons.\n";
isValid = false;
}
// Check if no radio bu on for gender is selected
let genderSelected = false;

for (let i = 0; i < genderRadio.length; i++) {


if (genderRadio[i].checked) {
genderSelected = true;
break;
}
}
if (!genderSelected) {
errorMessage += "Please select your gender.\n";
isValid = false;
}

// Display errors if valida on fails


if (!isValid) {
alert(errorMessage);
}

return isValid; // Return true if all fields are valid


}

// A ach the valida on func on to a form submission


document.getElementById("myForm").onsubmit = func on () {
return validateForm(); // Prevent form submission if valida on fails
};
What is cookies?
=> A cookie in PHP is a small file with a maximum size of 4KB that the web server stores
on the client computer. They are typically used to keep track of informa on such as a
username that the site can retrieve to personalize the page when the user visits the
website next me. A cookie can only be read from the domain that it has been issued
from. Cookies are usually set in an HTTP header but JavaScript can also set a cookie
directly on a browser.
Write sample PHP code to create and store cookies?
=> <?php

// Set a cookie
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, me() + (86400 * 30), "/"); // Cookie valid for 30
days

// Set another cookie


setcookie("theme", "dark", me() + (86400 * 30), "/"); // Cookie valid for 30 days
?>

<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, ini al-scale=1.0">
< tle>Cookie Example</ tle>
</head>
<body>
<?php
// Check if cookies are set
if (isset($_COOKIE[$cookie_name])) {

echo "<p>Welcome back, " . $_COOKIE[$cookie_name] . "!</p>";


} else {
echo "<p>Welcome, new user!</p>";
}

if (isset($_COOKIE["theme"])) {
echo "<p>Your selected theme is: " . $_COOKIE["theme"] . "</p>";
} else {
echo "<p>No theme preference set.</p>";

}
?>
</body>
</html>

You might also like