Web Technology II (JS / PHP) Imp Programs
<head><title>
Factorial
</title>
</head>
<body>
<input type="text" placeholder="Input number" id="n"><br>
<button onclick="fact()">Calculate</button>
</body>
<script>
function fact() {
var n = parseInt(document.getElementById("n").value);
var fact = 1;
for (var i = 1; i <= n; i++) {
var fact = fact * i;
}
document.write("factorial = ", fact);
}
</script>
</html>
//)display 1 to 10 using for,while,do-while loop using JavaScript
<script>
//for loop
document.write("using for loop<br>");
for(var i=1;i<=10;i++){
document.write("<br>" ,i);
}
//while loop
document.write("<br>using for while<br>");
var i=1;
while(i<=10){
document.write("<br>",i);
i++;
}
//do-while loop
document.write("<br>using for do-while<br>");
var i=1;
do{
window.document.write("<br>",i);
i++;
}while(i<=10);
</script>
#) WAP to make connection, create table,store data, fetch data from database using PHP
<?php
// create connection to database named demo
$conn = new mysqli("localhost", "root", "", "demo");
if ($conn->connect_error) {
die("Connection failed");
} else {
echo "Database Connected Successfully";
}
// create table named student
$sql = "CREATE TABLE student (
id INT(6) AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
mark INT(3),
email VARCHAR(50)
)";
if ($conn->query($sql) === TRUE) {
echo "Table student created successfully";
} else {
echo "Operation Failed" ;
}
//insert data into table student
$first_name = "Pankaj";
$last_name = "Dahal";
$mark = 85;
$email = "pankaj.dahal33@gmail.com";
$sql = "INSERT INTO student (first_name, last_name, mark, email) VALUES ('$first_name', '$last_name', '$mark', '$email')";
if ($conn->query($sql) === TRUE) {
echo "New record inserted successfully";
} else {
echo "Error occured";
}
//fetch data from table student
$sql = "SELECT * FROM student";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
//display data in different rows
echo "<br>" ."id: ".$row["id"]."<br>";
echo "Name: ".$row["first_name"]." ".$row["last_name"]."<br>";
echo "Mark: ".$row["mark"]."<br>";
echo "Email: ".$row["email"]."<br>";
}
?>
Comments
Post a Comment