To write an AngularJS program that uses expressions ({{ }}) and ng-init to display data in a web page.
<script> tag.ng-app directive to a <div> to define the AngularJS application.ng-init to create an object sort with three properties: one, two and three.{{ sort.one }}, {{ sort.two }} and {{ sort.three }}.<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<title>AngularJS Expression</title>
</head>
<body>
<h1 style="color:green">GeeksforGeeks</h1>
<h3>AngularJS Expressions</h3>
<div ng-app=""
ng-init="sort={one:'quick sort', two:'merge sort', three:'bubble sort'}">
<p>Sorting Algorithms:</p>
<ul>
<li>{{ sort.one }}</li>
<li>{{ sort.two }}</li>
<li>{{ sort.three }}</li>
</ul>
</div>
</body>
</html>
Sorting Algorithms:
The AngularJS expressions were evaluated and the three sorting algorithm names were displayed successfully in the list.
To write a program that uses AJAX (XMLHttpRequest) to fetch data from the server and update part of a web page without reloading it.
id="button1") and an empty <div id="output">.ajax.php that returns some text.XMLHttpRequest object and call open("GET","ajax.php",true).onload, check if xhr.status is 200. If yes, put xhr.responseText inside the output div; otherwise log an error.xhr.send() to send the request.“ ” must be straight quotes " " (curly quotes cause syntax errors).Var must be lowercase var.getElementById("button") does not match the button's id button1.Console.log must be console.log; Xhr.send() must be xhr.send() (JavaScript is case-sensitive).; after the onload block was tidied; the ajax.php file was missing, so it is added.index.html
<!DOCTYPE html>
<html>
<head>
<title>AJAX Example</title>
</head>
<body>
<button id="button1">Click me!</button>
<div id="output"></div>
<script>
document.getElementById("button1").addEventListener("click", function () {
var xhr = new XMLHttpRequest();
xhr.open("GET", "ajax.php", true);
xhr.onload = function () {
if (xhr.status === 200) {
document.getElementById("output").innerHTML = xhr.responseText;
} else {
console.log("There was an error.");
}
};
xhr.send();
});
</script>
</body>
</html>
ajax.php (keep in the same folder)
<?php
echo "<p>Hello! This text was loaded using AJAX from ajax.php</p>";
echo "<p>Server time: " . date("H:i:s") . "</p>";
?>
Before clicking the button:
After clicking the button (page is not reloaded):
Hello! This text was loaded using AJAX from ajax.php
Server time: 10:42:15
(The time shown will be your server's current time.)
The AJAX request was sent to the server and the response was displayed inside the page without reloading it.
To write a program that uses jQuery selectors (element, id and class selectors) to select HTML elements and change their style.
<script> tag.id="special", and one with class="note".$(document).ready(), use the element selector $("p") to set a pink background on all paragraphs.$("#special") to change the text colour to blue.$(".note") to make the text bold.googlepis.com is a typo; correct is googleapis.com.type="tetx/javascript" is a typo; the second script should be text/javascript (or no type).src value must be in quotes, and use https://.<body> tag was missing before the paragraphs.<!DOCTYPE html>
<html>
<head>
<title>First jQuery Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("p").css("background-color", "pink"); // element selector
$("#special").css("color", "blue"); // id selector
$(".note").css("font-weight", "bold"); // class selector
});
</script>
</head>
<body>
<p>This is first paragraph</p>
<p id="special">This is second paragraph</p>
<p class="note">This is third paragraph</p>
</body>
</html>
This is first paragraph
This is second paragraph
This is third paragraph
The jQuery element, id and class selectors selected the required elements and applied the styles successfully.
To write a PHP program that accepts data from an HTML form, validates it, and displays the result.
htmlspecialchars($_SERVER["PHP_SELF"]).$_SERVER["REQUEST_METHOD"] is "POST".$_POST and remove extra spaces using trim().preg_match), and not empty.filter_var(..., FILTER_VALIDATE_EMAIL).htmlspecialchars for safety).trim() and an empty-name check.htmlspecialchars() when printing the values (prevents XSS attacks).isset-safe reading and a clear success message..php and run on XAMPP/WAMP (it will not work by double-clicking).form.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Form Handling</title>
</head>
<body>
<h2>PHP Form Handling Example</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<label for="name">Name:</label>
<input type="text" name="name" id="name"><br><br>
<label for="email">Email:</label>
<input type="text" name="email" id="email"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$errors = [];
if ($name === "" || !preg_match("/^[a-zA-Z ]*$/", $name)) {
$errors[] = "Invalid name format. Only letters and white spaces allowed.";
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email format.";
}
if (count($errors) > 0) {
foreach ($errors as $e) {
echo "<p style='color:red'>$e</p>";
}
} else {
echo "<h3>Form submitted successfully</h3>";
echo "<p><strong>Name:</strong> " . htmlspecialchars($name) . "</p>";
echo "<p><strong>Email:</strong> " . htmlspecialchars($email) . "</p>";
}
}
?>
</body>
</html>
Case 1: Valid input (Name: John Doe, Email: john@example.com)
Name: John Doe
Email: john@example.com
Case 2: Invalid input (Name: John123, Email: john@)
Invalid name format. Only letters and white spaces allowed.
Invalid email format.
The PHP program received the form data, validated the name and email, and displayed either the submitted details or the proper error messages.