SL lab
SL lab
Write a script to take user input and display a greeting message with their name
=>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Greeting App</title>
</head>
<body>
<h2>Enter your name:</h2>
<input type="text" id="nameInput" placeholder="Your name">
<button onclick="greetUser()">Greet</button>
<p id="greetingMessage"></p>
<script>
function greetUser() {
const name = document.getElementById("nameInput").value;
document.getElementById("greetingMessage").innerText = `Hello, ${name}!
Welcome!`;
}
</script>
</body>
</html>
2. Create a script that calculates the factorial of a given number.
=>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Factorial Calculator</title>
</head>
<body>
<h2>Enter a number:</h2>
<input type="number" id="numberInput" placeholder="Enter a number">
<button onclick="calculateFactorial()">Calculate Factorial</button>
<p id="result"></p>
<script>
function calculateFactorial() {
const num = parseInt(document.getElementById("numberInput").value);
if (isNaN(num) || num < 0) {
document.getElementById("result").innerText = "Please enter a non-negative
integer.";
return;
}
let factorial = 1;
for (let i = 2; i <= num; i++) {
factorial *= i;
}
document.getElementById("result").innerText = `Factorial of ${num} is $
{factorial}`;
}
</script>
</body>
</html>
3. Write a script to check whether a number is prime or not.
=>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime Number Checker</title>
</head>
<body>
<h2>Enter a number:</h2>
<input type="number" id="numberInput" placeholder="Enter a number">
<button onclick="checkPrime()">Check Prime</button>
<p id="result"></p>
<script>
function checkPrime() {
const num = parseInt(document.getElementById("numberInput").value);
if (isNaN(num) || num < 2) {
document.getElementById("result").innerText = "Please enter an integer greater
than 1.";
return;
}
let isPrime = true;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
isPrime = false;
break;
}
}
document.getElementById("result").innerText = isPrime ? `${num} is a prime
number.` : `${num} is not a prime number.`;
}
</script>
</body>
</html>
4. Write a script to read a text file and count the number of words, lines, and characters
=><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Analyzer</title>
</head>
<body>
<h2>Upload a text file:</h2>
<form action="Qno4.php" method="post" enctype="multipart/form-data">
<input type="file" name="textFile" accept=".txt">
<button type="submit">Analyze File</button>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["textFile"])) {
$file = $_FILES["textFile"]["tmp_name"];
if (file_exists($file)) {
$content = file_get_contents($file);
$lines = explode("\n", $content);
$numLines = count($lines);
$numWords = str_word_count($content);
$numChars = strlen($content);
echo "<h2>File Analysis Result:</h2>";
echo "<p>Lines: $numLines</p>";
echo "<p>Words: $numWords</p>";
echo "<p>Characters: $numChars</p>";
} else {
echo "<p>Error: Unable to read the file.</p>";
}
}
?>
5. Create a script to append user-provided data to an existing file .
=><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Editor</title>
</head>
<body>
<h2>Upload a text file to append data:</h2>
<form action="Qno5.php" method="post" enctype="multipart/form-data">
<input type="file" name="textFile" accept=".txt">
<br><br>
<textarea name="appendText" rows="4" cols="50" placeholder="Enter text to
append..."></textarea>
<br><br>
<button type="submit">Append to File</button>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["textFile"]) &&
isset($_POST["appendText"])) {
$file = $_FILES["textFile"]["tmp_name"];
$appendText = $_POST["appendText"];
if (file_exists($file)) {
file_put_contents($file, "\n" . $appendText, FILE_APPEND);
echo "<h2>File Update Result:</h2>";
echo "<p>Text has been successfully appended to the file.</p>";
} else {
echo "<p>Error: Unable to open the file.</p>";
}
}
?>
6. Develop a script that searches for a specific word in a file and prints the line numbers
where it appears.
=><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Search</title>
</head>
<body>
<h2>Upload a text file to search for a word:</h2>
<form action="Qno6.php" method="post" enctype="multipart/form-data">
<input type="file" name="textFile" accept=".txt">
<br><br>
<input type="text" name="searchWord" placeholder="Enter word to search">
<br><br>
<button type="submit">Search</button>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["textFile"]) &&
isset($_POST["searchWord"])) {
$file = $_FILES["textFile"]["tmp_name"];
$searchWord = $_POST["searchWord"];
if (file_exists($file)) {
$lines = file($file);
$lineNumbers = [];
if (name == "") {
alert("Name must be filled out");
return false;
}
if (!emailPattern.test(email)) {
alert("Invalid email format");
return false;
}
if (password.length < 6) {
alert("Password must be at least 6 characters long");
return false;
}
return true;
}
</script>
</head>
<body>
<h2>Simple Form</h2>
<form name="myForm" action="Qno7.php" method="post" onsubmit="return
validateForm()">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="text" id="email" name="email"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
if (!empty($name) && filter_var($email, FILTER_VALIDATE_EMAIL) &&
strlen($password) >= 6) {
echo "<h2>Form Submitted Successfully!</h2>";
} else {
echo "<p>Error: Invalid form data.</p>";
}
}
?>
8. Create a script to dynamically add rows to an HTML table using JavaScript.
=><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dynamic Table</title>
<script>
function addRow() {
let table = document.getElementById("dataTable");
let row = table.insertRow();
let cell1 = row.insertCell(0);
let cell2 = row.insertCell(1);
let cell3 = row.insertCell(2);
cell1.innerHTML = document.getElementById("name").value;
cell2.innerHTML = document.getElementById("email").value;
cell3.innerHTML = document.getElementById("password").value;
}
</script>
</head>
<body>
<h2>Simple Form</h2>
<form name="myForm" onsubmit="addRow(); return false;">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="text" id="email" name="email"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<h2>Data Table</h2>
<table border="1" id="dataTable">
<tr>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>
</table>
</body>
</html>
9. Write a script to fetch and display live weather data using a free API(e.g., OpenWeather
API). (Implement with help of AJAX).
=>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
padding: 20px;
#weather {
margin-top: 20px;
.hidden {
display: none;
</style>
</head>
<body>
<div>
</div>
<h3>Weather Information:</h3>
<p id="cityName"></p>
<p id="temp"></p>
<p id="description"></p>
<p id="humidity"></p>
</div>
<script>
function getWeather() {
console.log(url);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
displayWeather(data);
} else {
alert('City not found or there was an issue with the API request');
};
xhr.send();
} else {
function displayWeather(data) {
document.getElementById('weather').classList.remove('hidden');
document.getElementById('description').innerText = `Weather: $
{data.weather[0].description}`;
} else {
}
}
</script>
</body>
</html>
10. Write a PHP script to connect to a database and retrieve data from a table.
=><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Database Data</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>Database Data</h1>
<?php
// Database connection parameters
$servername = "localhost"; // Database server (usually 'localhost')
$username = "root"; // Database username (default is 'root' for XAMPP/MAMP)
$password = ""; // Database password (leave blank for XAMPP/MAMP default)
$dbname = "jspractical"; // Name of your database
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["j_id"] . "</td><td>" . $row["name"] . "</td><td>" .
$row["email"] . "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
</body>
</html>
11. Create a simple login system using PHP and validate credentials from a database.
=><?php
session_start();
// Create connection
$conn = new mysqli($servername, $dbUsername, $dbPassword, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Close connection
$conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login System</title>
</head>
<body>
<h1>Login</h1>
<form method="post" action="Qno11.php">
<label for="name">Name</label>
<input type="text" id="name" name="name" required>
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
</body>
</html>
12. Develop a PHP script to process an HTML form and store the data in a MySQL database.
=>
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$name = $_POST['name'];
$password = $_POST['password'];
$email = $_POST['email'];
try {
$connection = mysqli_connect('localhost', 'root', '', 'jspractical');
$sql = "INSERT INTO js_table (name, password, email) VALUES ('$name', '$password',
'$email')";
$connection->query($sql);
if ($connection->affected_rows == 1) {
echo"Registration successful!"; // Set success message
exit();
} else {
$errorMessage = "Insert failed"; // Set failure message
}
} catch (Exception $e) {
$errorMessage = 'Connection Error: ' . $e->getMessage();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
15. 15. Explore the evolution of scripting languages and their impact on software
development practices.
=>
The evolution of scripting languages has significantly impacted software development
practices by enabling faster, more flexible, and efficient workflows.
Key Milestones:
1960s-1980s: Early scripting languages like Shell scripts and Perl automated system
tasks, increasing efficiency for administrators.
1990s: JavaScript enabled dynamic, interactive websites, while PHP and Perl powered
server-side scripting for dynamic content.
2000s: Python and Ruby simplified web development, with frameworks like Rails and
Django speeding up app creation.
2010s-Present: Node.js enabled full-stack JavaScript, while modern front-end
frameworks like React and Angular made UI development more efficient.
Impact on Software Development:
1. Increased Productivity: Simple syntax and rapid coding boost developer efficiency.
2. Faster Prototyping: Quick iterations allow for faster testing and development.
3. Improved Collaboration: Common tools and languages streamline team efforts across
front-end and back-end development.
4. Automation: Scripting is crucial for DevOps, testing, and deployment automation.
5. Cross-Platform Development: Scripting languages enable applications to run across
various platforms.
Scripting languages have evolved to dominate modern development, driving innovation,
rapid prototyping, and improved collaboration in software creation.