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

SCRIPTING FINAL

The document contains a series of JavaScript and PHP programming tasks, including functions to check uppercase letters, leap years, email validation, palindromes, and various jQuery animations. It also includes PHP scripts for database operations such as creating, inserting, deleting, and selecting records in MySQL. The tasks are aimed at demonstrating basic programming concepts and functionalities in JavaScript and PHP.

Uploaded by

Rohit Kumar Sonu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SCRIPTING FINAL

The document contains a series of JavaScript and PHP programming tasks, including functions to check uppercase letters, leap years, email validation, palindromes, and various jQuery animations. It also includes PHP scripts for database operations such as creating, inserting, deleting, and selecting records in MySQL. The tasks are aimed at demonstrating basic programming concepts and functionalities in JavaScript and PHP.

Uploaded by

Rohit Kumar Sonu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Q1. Write a JavaScript program to test the first character of a string is uppercase or not.

function firstIsUppercase(str) {
if (str.length === 0) {
return false;
}
return str.charAt(0).toUpperCase() === str.charAt(0);
}
let string1=prompt("enter any string")
firstIsUppercase(string1);
console.log(firstIsUppercase('world'));
console.log(firstIsUppercase(''));
if (firstIsUppercase(string1)) {
console.log(' First letter is uppercase');
} else {
console.log(' First letter is NOT uppercase');
}

Q2. Write a JavaScript program to determine whether a given year is a leap year in the Gregorian calendar.

// program to check leap year

function checkLeapYear(year) {
//three conditions to find out the leap year

if ((0 == year % 4) && (0 != year % 100) || (0 == year % 400)) {

console.log(year + ' is a leap year');

} else {

console.log(year + ' is not a leap year');

// take input

const year = prompt('Enter a year:');

checkLeapYear(year);
Q3.Write a pattern that matches e-mail addresses.

The personal information part contains the following ASCII characters.

Uppercase (A-Z) and lowercase (a-z) English letters.

Digits (0-9).

Characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~

Character . ( period, dot or fullstop) provided that it is not the first or last character and it will not come one after the
other.

function valid_email(str)

var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

if(mailformat.test(str))

console.log("Valid email address!");

else

console.log("You have entered an invalid email address!");

let email=prompt("enter your email");

valid_email(email);

Q4.Write a JavaScript function that checks whether a passed string is palindrome or not?

function check_palindrome( str )

let j = str.length -1;


for( let i = 0 ; i < j/2 ;i++)

let x = str[i] ;//forward character

let y = str[j-i];//backward character

if( x != y)

// return false if string not match

return false;

/// return true if string is palindrome

return true;

//function that print output if string is palindrome

function is_palindrome( str )

// variable that is true if string is palindrome

let ans = check_palindrome(str);

//condition checking ans is true or not

if( ans == true )

console.log("passed string is palindrome ");

else

console.log("passed string not a palindrome");

// test variable

let test = "naman";

is_palindrome(test);

Q5.Write a JavaScript program to display the current date and time in the following format

Date: date/month/year

Time: hour/minute/second

<html>

<head>
<title>Get date and time in JavaScript.</title>

</head>

<body>

<h2>Get date and time using JavaScript.</h2>

<h4>Output after using the basic methods of the Date() class.</h4>

<div id="date"> </div>

<div id="time"> </div>

<script type="text/javascript">

let date = document.getElementById("date");

let time = document.getElementById("time");

// creating the date object and getting the date and time

let newDate = new Date();

let year = newDate.getFullYear();

let month = newDate.getMonth();

let todaySDate = newDate.getDate();

let hours = newDate.getHours();

let minutes = newDate.getMinutes();

let seconds = newDate.getSeconds();

date.innerHTML = " " + todaySDate + "/ " + month + "/ " + year;

time.innerHTML = hours + ": " + minutes + ": " + seconds;

</script>

Q6.Write a JavaScript program to check whether a given string is blank or not.

is_Blank = function(input) {

if (input.length === 0)

return true;

else

return false;

console.log(is_Blank(''));

console.log(is_Blank('abc'));

Q7.Write a jQuery program to animates a paragraph to fade to a specified opacity (complete the animation within 500
milliseconds).

<!DOCTYPE html>

<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>

<script>
$(document).ready(function(){

$("button").click(function(){

$("p").fadeTo(500, 0.4);

});

});

</script>

</head>

<body>

<button>Gradually change the opacity of the p element</button>

<p>This is a paragraph.</p>

</body>

</html>

Q8.Write a jQuery program to Blink text using jQuery.

<!DOCTYPE html>

<html lang="en">

<head>

<!-- Using jquery library -->

<script src=

"https://code.jquery.com/jquery-git.js">

</script>

<style>

p{

color: red;

font-size: 40px;

display: none;

</style>

</head>

<body>

<p id="a">BCA</p>

<p id="b">MCA</p>

<p id="c">BScIT</p>

<script>

// Fast fade in

$("#a").fadeIn("fast")
// Slow fade in

$("#b").fadeIn("slow")

// Fade in in 4s

$("#c").fadeIn(4000)

</script>

</body>

</html>

Q9.Double click on paragraph to toggle background color.

<!DOCTYPE html>

<html lang="en">

<head>

<script src=

"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">

</script>

<script>

$(document).ready(function () {

var toggle = true; // Toggle state

$("p").on({

dblclick: function () {

if (toggle) {

// Change background to red

$(this).css("background-color", "red");

toggle = false;

} else {

// Change background to default

$(this).css("background-color", "white");

toggle = true;

});

});

</script>

</head>

<body>

<p>Double click to change background color</p>

</body>

</html>
Q10.Attach a function to the blur event.

<!DOCTYPE html>

<html>

<head>

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<script>

$(document).ready(function(){

$("input").blur(function(){

alert("This text box has lost its focus.");

});

});

</script>

</head>

<body>

Enter your name: <input type="text">

</body>

</html>

Q11.Attach a click and double-click event to the <p> element.

<!DOCTYPE html>

<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function () {

$(".clickable_ele").bind("click", function () {

$("<h4>Single click Event called</h4>")

.appendTo(".res");

});

$(".clickable_ele").bind("dblclick", function () {

$("<h4>Double click Event called</h4>")

.appendTo(".res");

});

});

</script>

<style>

body {

text-align: center;
}

h1 {

color: green;

.clickable_ele {

font-size: 25px;

font-weight: bold;

</style>

</head>

<body>

<h3>

How to attach a click and double-click

event to an element in jQuery?

</h3>

<div class="clickable_ele">

Clickable Element

</div>

<div class="res"></div>

</body>

</html>

Q12.Write a PHP program to swap two numbers.

<?php

$a = 45;

$b = 78;

// Swapping Logic

$third = $a;

$a = $b;

$b = $third;

echo "After swapping:<br><br>";

echo "a =".$a." b=".$b;

?>

Q13.Write a PHP program to create a table in MySQL.

<?php

$servername ="localhost";

$username = "root";

$password = "";
//create connection

$conn = mysqli_connect($servername, $username, $password);

//check connection

if($conn->connect_error){

die("Connection failed:");

}else{

echo "connect successfully";

echo "<br>";

//create database

$sql = "CREATE DATABASE database1";

$result = mysqli_query($conn, $sql);

//check for database creation

if($result){

echo "the db was created successfully!";

}else{

echo "the db was not created successfully because of this error --->". mysqli_error($conn);

?>

<?php

$servername ="localhost";

$username = "root";

$password = "";

$database = "database1";

//create connection

$conn = mysqli_connect($servername, $username, $password, $database);

//check connection

if($conn->connect_error){

die("Connection failed:");

}else{

echo "connect successfully";

echo "<br>";

//create Table in the database1


$sql = "CREATE TABLE employees(

id INT(3) PRIMARY KEY,

firstname VARCHAR(30) NOT NULL,

lastname VARCHAR(30) NOT NULL,

email VARCHAR(50)

)";

If ($conn->query($sql)===TRUE){

echo "Table employees created successfully";

else{

echo "error creating table: " . $conn->error;

$conn->close();

?>

Q14.Write a PHP program to insert record into a table using Mysqli.

<?php

$servername ="localhost";

$username = "root";

$password = "";

$database = "database1";

//create connection

$conn = mysqli_connect($servername, $username, $password, $database);

//check connection

if($conn->connect_error){

die("Connection failed:");

}else{

echo "connect successfully";


echo "<br>";

//Insert data in the table Employees

$sql = "INSERT INTO employees( id ,firstname ,lastname ,email )

VALUES ('02','Rohit','kumar','[email protected]')";

If ($conn->query($sql)===TRUE){

echo "New record created successfully";

else{

echo "error: " . $conn->error;

$conn->close();

?>

Q15.Write a PHP program to delete data from table using Mysqli and then also drop the table.

<?php

$servername ="localhost";

$username = "root";

$password = "";

$database = "database1";

//create connection

$conn = mysqli_connect($servername, $username, $password, $database);

//check connection

if($conn->connect_error){

die("Connection failed:");

}else{

echo "connect successfully";


echo "<br>";

//Delete data from the table Employees

// $sql = "DELETE FROM employees WHERE id=1";

//DROP table from the database1

$sql = "DROP TABLE employees";

If ($conn->query($sql)===TRUE){

echo "delete record successfully";

else{

echo "error: " . $conn->error;

$conn->close();

?>

Q16.Write a PHP program to select data and show into table format.

<?php

$servername ="localhost";

$username = "root";

$password = "";

$database = "database1";

//create connection

$conn = mysqli_connect($servername, $username, $password, $database);

//check connection

if($conn->connect_error){

die("Connection failed:");

}else{

echo "connect successfully";

echo "<br>";

}
//create Table in the database1

$sql = "CREATE TABLE employees(

// id INT(3) PRIMARY KEY,

// firstname VARCHAR(30) NOT NULL,

// lastname VARCHAR(30) NOT NULL,

// email VARCHAR(50)

// )";

If ($conn->query($sql)===TRUE){

// echo "Table employees created successfully";

else{

echo "error creating table: " . $conn->error;

echo "<br>";

//Insert data in the table Employees

$sql = "INSERT INTO employees( id ,firstname ,lastname ,email )

// VALUES ('04','gaurav','kumar','[email protected]')";

If ($conn->query($sql)===TRUE){

echo "New record created successfully";

else{

echo "error: " . $conn->error;

//Select data from table and show into the table format

$sql = "SELECT id, firstname, lastname, email FROM employees";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

echo "<table><tr><th>ID</th><th>Name</th><th>Email</th></tr>";

// output data of each row

while($row = $result->fetch_assoc()) {

echo "<tr><td>" . $row["id"]. "</td><td>" . $row["firstname"]. " " . $row["lastname"]. "</td><td>" . $row["email"].
"</td></tr>";
}

echo "</table>";

} else {

echo "0 results";

$conn->close();

?>

Q17.Create a student Registration in PHP and Save and Display the student Records.

<?php

session_start();

if(!isset($_SESSION['user_name'])){

header("location: login.php");

else {

?>
<?php //echo $dataofpage ; ?>

<html>

<body bgcolor="" >

<form name="iksk" method="post" action="" enctype="multipart/form-data">

<tr>

<td width="74%" valign="top"><table width="739" height="895" border="0" align="center" cellpadding="0"


cellspacing="0" bordercolor="#000033" bgcolor="#FFFFFF">

<tr height="">

<td height="auto" colspan="6" align="center" bordercolor="#CC0000" bgcolor=""><?php include('header.php');?></td>

</tr>

<tr>

<td align="right" height="44">

<div align="center" class="style5 style15"><strong>Name</strong></div></td>

<td>

<div align="left">

<input name="y_name" type="text" size="50" placeholder="Enter Name......" />

<br>

</div></td>

</tr>

<tr>

<td height="39" align="right">

<div align="center" class="style5 style15"><strong>Gender </strong></div></td>

<td>

<div align="left">

<label>

<select name="gender">

<option>Male</option>

<option>Female</option>

</select>

</label>

</div></td>

</tr>
<tr>

<td height="33" align="right">

<div align="center" class="style5 style15"><strong>Date of Birth </strong></div></td>

<td>

<div align="left">

<input name="dob" type="date" size="50" placeholder="Enter date of birth ......" />

</div></td>

</tr>

<tr>

<td height="32" align="right">

<div align="center" class="style5 style15"><strong>Father's Name</strong></div></td>

<td>

<div align="left">

<input name="f_name" type="text" size="50" placeholder="Enter Father's Name......" />

</div></td>

</tr>

<tr>

<td height="33" align="right">

<div align="center" class="style5 style15"><strong>Nationality</strong></div></td>

<td>

<div align="left">

<input name="nationality" type="text" size="50" placeholder="Enter the Nationality......" />

</div></td>

</tr>

<tr>

<td height="34" align="right">

<div align="center" class="style5 style15"><strong>Marital status</strong></div></td>

<td>

<div align="left">

<label>

<select name="m_satus">

<option>Married</option>
<option>Unmarried</option>

</select>

</label>

</div></td>

</tr>

<tr>

<td height="35" align="right">

<div align="center" class="style5 style15"><strong>Address</strong></div></td>

<td>

<div align="left">

<input name="address" type="text" size="50" placeholder="Enter Address......" />

</div></td>

</tr>

<tr>

<td height="31" align="right">

<div align="center" class="style5 style15"><strong>E-mail</strong></div></td>

<td>

<div align="left">

<input name="e_mail" type="text" size="50" placeholder="Enter e_mail......" />

</div></td>

</tr>

<tr>

<td height="35" align="right">

<div align="center" class="style5 style15"><strong>Mobile Number </strong></div></td>

<td>

<div align="left">

<input name="a_r_claimed" type="text" size="50" placeholder="Enter Mobile Number......" />

</div></td>

</tr>

<tr>

<td height="32" align="right">


<div align="center" class="style5 style15"><strong>Educational Qualification</strong></div></td>

<td>

<div align="left">

<input name="edu_quali" type="text" size="50" placeholder="Enter Education qualification....." />

</div></td>

</tr>

<tr>

<td height="32" align="right">

<div align="center" class="style5 style15"><strong>Upload Image</strong></div></td>

<td>

<div align="left">

<input name="image" type="file" size="50"/>

</div></td>

</tr>

<tr>

<td height="30" align="right">

<div align="center" class="style5 style15"><strong>Registraation-id</strong></div></td>

<td>

<div align="left">

<input name="reg_id" type="text" size="50" placeholder="Enter regstration-id......" />

</div></td>

</tr>

<tr>

<td height="34" align="right">

<div align="center" class="style5 style15"><strong>Registration Date</strong></div></td>

<td>

<div align="left">

<input name="tra_date" type="date" size="50" placeholder="Enter Registration Date......" />

</div></td>
</tr>

<tr>

<td height="30" align="right">

<div align="center" class="style5 style15"><strong>Certificate Number </strong></div></td>

<td>

<div align="left">

<input name="fee_paid" type="text" size="50" placeholder="Enter Certificate Number......" />

</div></td>

</tr>

<tr>

<td height="32" align="right">

<div align="center" class="style5 style15"><strong>Roll Number </strong></div></td>

<td>

<div align="left">

<input name="agency" type="text" size="50" placeholder="Enter Roll Number......" />

</div></td>

</tr>

<tr>

<td height="34" align="right">

<div align="center" class="style5 style15"><strong>Deate of issue </strong></div></td>

<td>

<div align="left">

<input name="m_payment" type="date" size="50" placeholder="Enter Date of issue......" />

</div></td>

</tr>

<tr>

<td align="center" colspan="6"><input type="submit" name="submit" value="Published Now"></td>

</tr>

</table></td>

</tr>
</table></td>

</tr>

</table>

</form>

</body>

</html>

<?php

include("connect.php");

if(isset($_POST['submit']))

$post_y_name=$_POST['y_name'];

$post_gender=$_POST['gender'];

$post_dob=$_POST['dob'];

$post_f_name=$_POST['f_name'];

$post_nationality=$_POST['nationality'];

$post_m_satus=$_POST['m_satus'];

$post_address=$_POST['address'];

$post_e_mail=$_POST['e_mail'];

$post_a_r_claimed=$_POST['a_r_claimed'];

$post_edu_quali=$_POST['edu_quali'];

$image_name=$_FILES['image']['name'];

$image_type=$_FILES['image']['type'];

$image_size=$_FILES['image']['size'];

$image_tmp=$_FILES['image']['tmp_name'];

$post_reg_id=$_POST['reg_id'];

$post_tra_date=$_POST['tra_date'];

$post_fee_paid=$_POST['fee_paid'];

$post_agency=$_POST['agency'];

$post_m_payment=$_POST['m_payment'];

if($post_y_name=='' or $post_gender=='' or $post_dob=='' or $post_f_name=='' )

{
echo "<script>alert('Any of the fields is Empty ')</script>";

else

if($image_type=="image/jpeg" or $image_type=="image/png"

or $image_type="image/gif")

if($image_size<=500000)

move_uploaded_file($image_tmp,"images/$image_name");

else

echo "<script>alert('Image is larger, only 50kb size is allowed')</script>";

else

echo "<script>alert('image type is invalid')</script>";

$query= "insert into data

(p_y_name,p_gender,p_dob,p_f_name,p_nationality,p_m_satus,p_address,p_e_mail,p_a_r_claimed,p_edu_quali,p_img,p_reg_id,
p_tra_date,p_fee_paid,p_agency,p_m_payment)

values('$post_y_name','$post_gender','$post_dob','$post_f_name','$post_nationality','$post_m_satus','$post_address','$post_e_mai
l','$post_a_r_claimed','$post_edu_quali','$image_name','$post_reg_id','$post_tra_date','$post_fee_paid','$post_agency','$post_m_p
ayment')";

if(mysql_query($query))

echo "<script>alert('All Post And Image has been send in database')</script>";

else

echo "<script>alert('All Post And Image has Not been send in database')</script>";

}
}

?>

Q18.Write a program to Develop student registration form and display all the submitted data on another page.

<html>

<title>Student registration</title>

<h1>Student Registration Form </h1>

<body>

<form method=get action="">

Enter Student name:<input type=text name=t1 value="<?php if(isset($_GET['t1'])) echo $_GET['t1'];?>"></br>

Enter Student Roll no:<input type=text name=t2 value="<?php if(isset($_GET['t2'])) echo $_GET['t2'];?>"></br>

Enter Class:<input type=text name=t3 value="<?php if(isset($_GET['t3'])) echo $_GET['t3'];?>"></br>

Enter Age:<input type=text name=t4 value="<?php if(isset($_GET['t4'])) echo $_GET['t4'];?>"></br>

Enter Address:<input type=text name=t5 value="<?php if(isset($_GET['t5'])) echo $_GET['t5'];?>"></br>

<input type=submit value=submit></br>

</form>

</body>

</html>

<?php

if(isset($_GET['t1']))

if($name==""||$roll==""||$class==""||$age==""||$add=="")

echo "All fields are compulsory :";

else

$name=$_GET['t1'];

$roll=$_GET['t2'];

$class=$_GET['t3'];

$age=$_GET['t4'];

$add=$_GET['t5'];
echo "<h1>Student Information </h1></br>";

echo "Student name :$name</br>";

echo "Student Roll no: $roll</br>";

echo "Student Class: $class</br>";

echo "Student Age :$age</br>";

echo "Student Address: $add</br>";

?>

Q19.Write a program to read customer information like c_no, c_name, item_purchased and mob_no from customer table
and display all this information in table format on output screen.

//HTML FILE

<html>

<body>

<h3> Program to collect the customer-information </h3>

<form action ="Display.php" method="get">

<table border="5">

<tr>

<td> Enter Name:</td>

<td> <input type="text" name="name"> </td>

</tr>

<tr>

<td> Enter Address Line1: </td>

<td> <input type="text" name="address1"></td>

</tr>

<tr>
<td> Enter Address line2: </td>

<td> <input type="text" name="address2"> </td>

</tr>

<tr>

<td> Enter Email-id: </td>

<td> <input type="text" name="email"> </td>

</tr>

<tr>

<td> </td> </tr>

<tr> </tr>

<tr>

<td> <input type="submit" value="Submit"></td>

<td> <input type="Reset" value="Reset"></td>

</tr>

</br>

</table>

</form>

</body>

</html>

</html>

PHP FILE

<html>

<head><title> Display.php </title></head>

<body bgcolor="aabbcc">

<?php

$name1=$_REQUEST["name"];

$address1=$_REQUEST["address1"];

$address2=$_REQUEST["address2"];
$email=$_REQUEST["email"];

define('DB_SERVER', 'localhost:3306');

define('DB_USERNAME', 'root');

define('DB_PASSWORD', 'root123');

define('DB_DATABASE', 'customers'); //where customers is the database

$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

$query= "insert into address values('$name1','$address1','$address2','$email'); //to insert input records into a table - address

$enter= mysqli_query($db,$query);

$query="select * from address"; // Fetch all the records from the table address

$result=mysqli_query($db,$query);

?>

<h3> Page to display the stored data </h3>

<table border="1">

<tr>

<th> NAME </th>

<th> ADDRESS Line1 </th>

<th> ADDRESS Line2 </th>

<th> EMAIL-id </th> </tr>

<?php while($array=mysqli_fetch_row($result)) ?>

<tr>

<td><?echo $array[0];?></td>

<td><?echo $array[1];?></td>

<td><?echo $array[2];?></td>

<td><?echo $array[3];?></td>

</tr>

<?php endwhile; ?>


<?php mysqli_free_result($result); ?>

<?php mysqli_close($db); ?>

</table>

</body>

</html>

You might also like