0% found this document useful (0 votes)
0 views

mainnn

The document contains multiple PHP and HTML code snippets for various functionalities including reversing digits, evaluating sums, performing string operations, user login, student registration, and database operations. It also includes jQuery examples for manipulating HTML elements and handling click events. Each section demonstrates different programming concepts and techniques in web development.

Uploaded by

siddartha.ps2067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

mainnn

The document contains multiple PHP and HTML code snippets for various functionalities including reversing digits, evaluating sums, performing string operations, user login, student registration, and database operations. It also includes jQuery examples for manipulating HTML elements and handling click events. Each section demonstrates different programming concepts and techniques in web development.

Uploaded by

siddartha.ps2067
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

question 1

<?php

function reverseDigits ($num) {

$reversed=strrev($num);

return $reversed;

$num ="1,2,3,4,5";

echo "original string:".$num;

echo "<br>";

echo "reversed string:".reverseDigits ($num);

?>

question 2
<html>
<body>
<h2>Enter Two Numbers to Evaluate Sum</h2>
<form method="post">
<label for="num1">Number 1:</label>
<input type="text" id="num1" name="num1" required>
<br><br>
<label for="num2">Number 2:</label>
<input type="text" id="num2" name="num2" required>
<br><br>
<button type="submit" name="evaluate">Evaluate</button>
</form>
<?php
if (isset($_POST['evaluate'])) {
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
if (is_numeric($num1) && is_numeric($num2)) {
$sum = $num1 + $num2;
echo "Result: The sum of $num1 and $num2 is $sum.";
} else {
echo "Please enter valid numbers.";
}
}
?>
</body>
</html>

question 3
<html>
<head>
<title>String Operation</title>
</head>
<body>
<h2>String Operations</h2>
<form method="POST" action="">
<label for="inputString">Enter a string:</label>
<input type="text" name="inputString" id="inputString" required>
<br><br>
<label for="substring">Enter substring (for finding or
replacing):</label>
<input type="text" name="substring" id="substring">
<br><br>
<label for="replaceText">Enter text to replace with:</label>
<input type="text" name="replaceText" id="replaceText">
<br><br>
<button type="submit" name="operation" value="length">Find Length of
String</button>
<button type="submit" name="operation" value="substring">Find
Substring</button>
<button type="submit" name="operation" value="replace">Replace
Text</button>
<button type="submit" name="operation" value="trim">Trim
Whitespace</button>
<button type="submit" name="operation" value="is_string">Check if
Value is String</button>
<button type="submit" name="operation" value="ucwords">Uppercase First
Letter of Each Word</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$inputString = $_POST['inputString'];
$operation = $_POST['operation'];
$result = '';
switch ($operation) {
case 'length':
$result = "Length of the string is: " . strlen($inputString);
break;
case 'substring':
$substring = $_POST['substring'];
$position = strpos($inputString, $substring);
$result = ($position !== false) ?
"Substring found at position: $position" : "Substring not
found.";
break;
case 'replace':
$substring = $_POST['substring'];
$replaceText = $_POST['replaceText'];
$result = "Result after replacement: " .
str_replace($substring, $replaceText, $inputString);
break;
case 'trim':
$result = "Trimmed string: '" . trim($inputString) . "'";
break;
case 'is_string':
if(is_string($inputString)&&!is_numeric($inputString)){
$result ="The value is a valid string.";
}else{
$result="The value is not a valid string.";
}
break;
case 'ucwords':
$result = "String with uppercase words: " .
ucwords($inputString);
break;
default:
$result = "Invalid operation.";
}
echo "<h3>Result:</h3><p>$result</p>";
}
?>
</body>
</html>

question no 4
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
// Simple validation
if (empty($username) || empty($password)) {
$error = 'Both fields are required!';
} else {
$_SESSION['username'] = $username; // Store username in session
header("Location: " . $_SERVER['PHP_SELF']); // Reload the page to
show welcome message
exit();
}
}
// If user is already logged in, display welcome message
if (isset($_SESSION['username'])) {
echo "<h1>Welcome, " . htmlspecialchars($_SESSION['username']) . "!</h1>";
echo '<a href="?logout=true">Logout</a>';
// Logout logic
if (isset($_POST['logout'])) {
session_destroy();
header("Location: " . $_SERVER['PHP_SELF']);
exit();
}
exit();
}
?>
<html>
<h2>Login</h2>
<form method="post" action="">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<button type="submit">Login</button>
</form>
</html>

question 5
<html>
<head>
<title>Student Registration Form</title>
</head>
<body>
<h2>Student Registration Form</h2>
<form action="" method="post">
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name"
required><br><br>
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name"
required><br><br>
<label for="gender">Gender:</label>
<input type="radio" id="male" name="gender" value="Male" required>
Male
<input type="radio" id="female" name="gender" value="Female">
Female
<input type="radio" id="other" name="gender" value="Other"> Other
<br><br>
<label for="roll_number">Roll Number:</label>
<input type="text" id="roll_number" name="roll_number"
required><br><br>
<label for="phone_number">Phone Number:</label>
<input type="text" id="phone_number" name="phone_number"
required><br><br>
<label for="course">Course:</label>
<select id="course" name="course" required>
<option value="Computer Science">Computer Science</option>
<option value="Engineering">Engineering</option>
<option value="Business Administration">Business
Administration</option>
<option value="Medicine">Medicine</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
<!-- Link to submit form data via GET method -->
<h3>Alternative Submit using GET</h3>
<form action="" method="get">
<label for="first_name_get">First Name:</label>
<input type="text" id="first_name_get" name="first_name"
required><br><br>
<label for="last_name_get">Last Name:</label>
<input type="text" id="last_name_get" name="last_name"
required><br><br>
<label for="gender_get">Gender:</label>
<input type="radio" id="male_get" name="gender" value="Male"
required> Male
<input type="radio" id="female_get" name="gender" value="Female">
Female
<input type="radio" id="other_get" name="gender" value="Other">
Other <br><br>
<label for="roll_number_get">Roll Number:</label>
<input type="text" id="roll_number_get" name="roll_number"
required><br><br>
<label for="phone_number_get">Phone Number:</label>
<input type="text" id="phone_number_get" name="phone_number"
required><br><br>
<label for="course_get">Course:</label>
<select id="course_get" name="course" required>
<option value="Computer Science">Computer Science</option>
<option value="Engineering">Engineering</option>
<option value="Business Administration">Business
Administration</option>
<option value="Medicine">Medicine</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
// Handling POST data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$first_name_post = isset($_POST['first_name']) ? $_POST['first_name']
: '';
$last_name_post = isset($_POST['last_name']) ? $_POST['last_name'] :
'';
$gender_post = isset($_POST['gender']) ? $_POST['gender'] : '';
$roll_number_post = isset($_POST['roll_number']) ?
$_POST['roll_number'] : '';
$phone_number_post = isset($_POST['phone_number']) ?
$_POST['phone_number'] : '';
$course_post = isset($_POST['course']) ? $_POST['course'] : '';
// Display POST data
echo "<h2>Student Registration (POST Method)</h2>";
echo "First Name: " .($first_name_post) . "<br>";
echo "Last Name: " .($last_name_post) . "<br>";
echo "Gender: " .($gender_post) . "<br>";
echo "Roll Number: " .($roll_number_post) . "<br>";
echo "Phone Number: " .($phone_number_post) . "<br>";
echo "Course: " .($course_post) . "<br><br>";
}
// Handling GET data
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$first_name_get = isset($_GET['first_name']) ? $_GET['first_name'] :
'';
$last_name_get = isset($_GET['last_name']) ? $_GET['last_name'] : '';
$gender_get = isset($_GET['gender']) ? $_GET['gender'] : '';
$roll_number_get = isset($_GET['roll_number']) ? $_GET['roll_number']
: '';
$phone_number_get = isset($_GET['phone_number']) ?
$_GET['phone_number'] : '';
$course_get = isset($_GET['course']) ? $_GET['course'] : '';
// Display GET data
echo "<h2>Student Registration (GET Method)</h2>";
echo "First Name: " .($first_name_get) . "<br>";
echo "Last Name: " .($last_name_get) . "<br>";
echo "Gender: " .($gender_get) . "<br>";
echo "Roll Number: " .($roll_number_get) . "<br>";
echo "Phone Number: " .($phone_number_get) . "<br>";
echo "Course: " .($course_get) . "<br><br>";
}
?>

question 6
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb1";
$conn = new mysqli($servername, $username, $password);
if($conn->connect_error){
die("Connection failed".$conn->connect_error);
}
$sql = "CREATE DATABASE IF NOT EXISTS $dbname";
if ($conn->query($sql) === TRUE) {
echo "Database created or already exists.<br>";
} else {
echo "Error creating database: " . $conn->error . "<br>";
}
$conn->select_db($dbname);
$table_sql = "CREATE TABLE IF NOT EXISTS users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL)";
if ($conn->query($table_sql) === TRUE) {
echo "Table 'users' created or already exists.<br>";
} else {
echo "Error creating table: " . $conn->error . "<br>";
}
if (isset($_POST['add_data'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$insert_sql = "INSERT INTO users (name, email) VALUES ('$name',
'$email')";
if ($conn->query($insert_sql) === TRUE) {
echo "The record is added in the database!<br>";
} else {
echo "Error: " . $conn->error . "<br>";
}
}
if (isset($_POST['delete_data'])) {
$id = $_POST['id'];
$delete_sql = "DELETE FROM users WHERE id = $id";
if ($conn->query($delete_sql) === TRUE) {
echo "A record is deleted from the database<br>";
} else {
echo "Error: " . $conn->error . "<br>";
}
}
if (isset($_POST['display_data'])) {
$select_sql = "SELECT id, name, email FROM users ORDER BY id";
$result = $conn->query($select_sql);
if ($result->num_rows > 0) {
echo "<h3>User List:</h3>";
echo "<table
border='1'><tr><th>ID</th><th>Name</th><th>Email</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["name"].
"</td><td>" . $row["email"]. "</td><td>";
}
echo "</table>";
} else {
echo "No records found.<br>";
}
}
$conn->close();
?>
<html>
<head>
<title>PHP MySQL Example</title>
</head>
<body>
<h1>PHP MySQL Database Operations</h1>
<form method="POST">
<h2>Add Data</h2>
Name: <input type="text" name="name" required><br><br>
Email: <input type="email" name="email" required><br><br>
<button type="submit" name="add_data">Add Data</button>
</form>
<hr>
<form method="POST">
<h2>Delete Data</h2>
ID: <input type="number" name="id" required><br><br>
<button type="submit" name="delete_data">Delete Data</button>
</form>
<hr>
<form method="POST">
<h2>Display Data</h2>
<button type="submit" name="display_data">Display Data</button>
</form>
</body>
</html>

question no jquery 1
<html>
<head>
<title>Button Click Events Demo</title>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$(document).ready(function(){
$("#changeColorBtn").click(function(){
$("p").css("color", "blue");
});
$("#changeContentBtn").click(function(){
$("p").text("Text content has been changed!");
});
});
</script>
</head>
<body>
<p>This is a paragraph with some text.</p>
<button id="changeColorBtn">Change Text Color</button>
<button id="changeContentBtn">Change Text Content</button>
</body>
</html>

jquery 2
<html>
<head>
<title>Element Selection Demo</title>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$(document).ready(function(){
// Selecting elements by ID
$("#elementByID").text("Element selected by ID");
// Selecting elements by class
$(".elementByClass").text("Element selected by class");
// Selecting elements by element name
$("p").text("Paragraph element selected");
// Selecting elements by attribute name
$("[data-attribute='example']").text("Element selected by attribute name");
});
</script>
</head>
<body>
<h4 id="elementByID"></h4>
<h2 class="elementByClass"></h2>
<p></p>
<p data-attribute="example"></p>
</body>
</html>

jquery 3
<html>
<head>
<title>jQuery Click Event</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<button id="myButton">Click Me!</button>
<p id="message"></p>
<script>
$(document).ready(function () {
$("#myButton").click(function () {
$("#message").text("Button was clicked!");
});
});
</script>
</body>
</html>

You might also like