Sunday, June 28, 2026

Data save in sql server table using Java Script

 JavaScript running in a browser cannot directly insert records into a SQL Server database for security reasons. The correct architecture is:

HTML Form → JavaScript → Server-side API (ASP.NET, PHP, Node.js, Java, etc.) → SQL Server

If you want to learn the process, here is a complete example using HTML, JavaScript, and an ASP.NET Web API.

Step 1: HTML Page

<!DOCTYPE html>
<html>
<head>
<title>Insert Record</title>
</head>
<body>

<h2>Student Registration</h2>

Name :
<input type="text" id="txtName"><br><br>

Email :
<input type="text" id="txtEmail"><br><br>

<input type="button" value="Save" onclick="saveData()">

<p id="msg"></p>

<script>
function saveData() {

var name = document.getElementById("txtName").value;
var email = document.getElementById("txtEmail").value;

if(name=="" || email=="")
{
alert("Please fill all fields.");
return;
}

fetch("https://localhost:5001/api/student", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
Name: name,
Email: email
})
})
.then(response => response.text())
.then(data => {
document.getElementById("msg").innerHTML = data;
})
.catch(error => {
console.log(error);
});

}
</script>

</body>
</html>

Step 2: SQL Server Table

CREATE TABLE Student
(
Id INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(100),
Email VARCHAR(100)
)

Step 3: ASP.NET C# API

[HttpPost]
public string Post(Student s)
{
SqlConnection con = new SqlConnection(
"Server=.;Database=CollegeDB;Trusted_Connection=True;");

con.Open();

SqlCommand cmd = new SqlCommand(
"INSERT INTO Student(Name,Email) VALUES(@Name,@Email)", con);

cmd.Parameters.AddWithValue("@Name", s.Name);
cmd.Parameters.AddWithValue("@Email", s.Email);

cmd.ExecuteNonQuery();

con.Close();

return "Record Saved Successfully";
}

Data Flow

HTML TextBox
│
▼
JavaScript
(fetch API)
│
▼
ASP.NET Web API
│
▼
SQL Server Database

Important Note

❌ This is not possible:

// JavaScript cannot connect directly to SQL Server
var con = new SqlConnection(...);

Browsers do not allow direct database connections because it would expose your database credentials to anyone using the website.

If you are teaching Web Technology, I can also provide a complete CRUD (Insert, Update, Delete, Select) project using:

  • HTML
  • CSS
  • JavaScript
  • ASP.NET Web API
  • SQL Server

with source code and database scripts.

Saturday, June 27, 2026

DATA TRANSFER BETWEEN ONE PAGE TO AN OTHER PAGE USING JAVA SCRIPT

 You can transfer data from one HTML page to another using JavaScript in several ways. One of the simplest methods is by using URL Query String.

Example: Transfer Data Using Query String

Page 1 (index.html)

<!DOCTYPE html>
<html>
<head>
<title>Page 1</title>
<script>
function sendData() {
var name = document.getElementById("txtName").value;
var city = document.getElementById("txtCity").value;

if (name.trim() == "" || city.trim() == "") {
alert("Please fill all fields.");
return;
}

window.location.href = "display.html?name="
+ encodeURIComponent(name)
+ "&city="
+ encodeURIComponent(city);
}
</script>
</head>
<body>

<h2>Page 1</h2>

Name:
<input type="text" id="txtName"><br><br>

City:
<input type="text" id="txtCity"><br><br>

<button onclick="sendData()">Next</button>

</body>
</html>

Page 2 (display.html)

<!DOCTYPE html>
<html>
<head>
<title>Page 2</title>
<script>
function getData() {

var params = new URLSearchParams(window.location.search);

var name = params.get("name");
var city = params.get("city");

document.getElementById("lblName").innerHTML = name;
document.getElementById("lblCity").innerHTML = city;
}
</script>
</head>

<body onload="getData()">

<h2>Received Data</h2>

Name : <span id="lblName"></span><br><br>

City : <span id="lblCity"></span>

</body>
</html>

Output

Page 1

Name : Nilesh Gupta
City : Nagpur

[ Next ]

After clicking Next, the browser URL becomes:

display.html?name=Nilesh%20Gupta&city=Nagpur

Page 2

Received Data

Name : Nilesh Gupta

City : Nagpur

Another Method: Using localStorage

Page 1

<script>
function sendData() {

localStorage.setItem("Name", document.getElementById("txtName").value);
localStorage.setItem("City", document.getElementById("txtCity").value);

window.location = "display.html";
}
</script>

Page 2

<script>
window.onload = function () {

document.getElementById("lblName").innerHTML =
localStorage.getItem("Name");

document.getElementById("lblCity").innerHTML =
localStorage.getItem("City");

}
</script>

Common Methods to Transfer Data Between Pages

MethodBest Use Case
Query String (URLSearchParams)Small, non-sensitive data
localStorageData that should persist even after page refresh
sessionStorageData only for the current browser tab/session
CookiesSmall data shared across pages or sessions
Server-side (PHP, ASP.NET, Node.js, etc.)Secure or database-backed applications

For beginners, localStorage and Query String are the easiest and most commonly used methods in JavaScript.

Java Script Test Box Blank

 <!DOCTYPE html>

<html>

<head>

    <title>Textbox Validation</title>

    <script>

        function validateTextBox() {

            var name = document.getElementById("txtName").value;


            if (name.trim() === "") {

                alert("Please enter your name.");

                return false;

            }


            alert("Submitted Successfully!");

            return true;

        }

    </script>

</head>

<body>


    <label>Name:</label>

    <input type="text" id="txtName">


    <br><br>


    <button onclick="validateTextBox()">Submit</button>


</body>

</html>

Java Script Varible Concept

 

JavaScript Variables – Simple Example with Result

A variable in JavaScript is used to store data such as numbers, text, or other values.

Syntax

var variableName = value;

or

let variableName = value;

or

const variableName = value;

Example 1: Using var

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Variable Example</title>
</head>
<body>

<h2>JavaScript Variable Example</h2>

<p id="result"></p>

<script>
var studentName = "Rahul";
var age = 21;

document.getElementById("result").innerHTML =
"Student Name: " + studentName + "<br>" +
"Age: " + age;
</script>

</body>
</html>

Output

Student Name: Rahul
Age: 21

Example 2: Adding Two Numbers

<!DOCTYPE html>
<html>
<head>
<title>Addition Example</title>
</head>
<body>

<h2>Addition of Two Numbers</h2>

<p id="demo"></p>

<script>
var num1 = 25;
var num2 = 15;
var sum = num1 + num2;

document.getElementById("demo").innerHTML =
"Number 1 = " + num1 + "<br>" +
"Number 2 = " + num2 + "<br>" +
"Sum = " + sum;
</script>

</body>
</html>

Output

Number 1 = 25
Number 2 = 15
Sum = 40

Example 3: Using let

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
let city = "Nagpur";
let state = "Maharashtra";

document.getElementById("demo").innerHTML =
"City: " + city + "<br>" +
"State: " + state;
</script>

</body>
</html>

Output

City: Nagpur
State: Maharashtra

Example 4: Using const

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
const PI = 3.14159;
const radius = 5;

let area = PI * radius * radius;

document.getElementById("demo").innerHTML =
"Radius = " + radius + "<br>" +
"Area = " + area;
</script>

</body>
</html>

Output

Radius = 5
Area = 78.53975

Difference Between var, let, and const

KeywordCan be ReassignedCan be RedeclaredScope
var✅ Yes✅ YesFunction Scope
let✅ Yes❌ NoBlock Scope
const❌ No❌ NoBlock Scope

Thursday, June 25, 2026

JAVA SCRIPT GET ELEMENT BY ID CONCEPT

 

JavaScript getElementById() with HTML Control – Simple Example with Result

What is getElementById()?

getElementById() is a JavaScript method used to access an HTML element by its id.

Syntax:

document.getElementById("elementId");
  • document → Represents the HTML document.
  • getElementById() → Finds an element using its id.
  • "elementId" → The id of the HTML element.

Example 1: Change Text

HTML + JavaScript

<!DOCTYPE html>
<html>
<head>
<title>getElementById Example</title>
</head>
<body>

<h2 id="heading">Welcome to NRsolution4u</h2>

<button onclick="changeText()">Click Here</button>

<script>
function changeText()
{
document.getElementById("heading").innerHTML = "Welcome to JavaScript!";
}
</script>

</body>
</html>

Result

Before Clicking

Welcome to NRsolution4u

[Click Here]

After Clicking

Welcome to JavaScript!

[Click Here]

Example 2: Read Value from TextBox

<!DOCTYPE html>
<html>
<head>
<title>Textbox Example</title>
</head>
<body>

Name :
<input type="text" id="txtName">

<button onclick="showName()">Show Name</button>

<p id="result"></p>

<script>
function showName()
{
var name = document.getElementById("txtName").value;

document.getElementById("result").innerHTML =
"Your Name is : " + name;
}
</script>

</body>
</html>

Result

If the user types

Nilesh Gupta

and clicks Show Name, the output will be

Your Name is : Nilesh Gupta

Example 3: Change Background Color

<!DOCTYPE html>
<html>
<head>
<title>Color Example</title>
</head>
<body>

<div id="box" style="width:200px;height:100px;background:lightgray;">
</div>

<br>

<button onclick="changeColor()">Change Color</button>

<script>
function changeColor()
{
document.getElementById("box").style.backgroundColor = "orange";
}
</script>

</body>
</html>

Result

Before

+----------------------+
| |
| Gray Box |
| |
+----------------------+

[Change Color]

After Clicking

+----------------------+
| |
| Orange Box |
| |
+----------------------+

Example 4: Enable/Disable Button

<!DOCTYPE html>
<html>
<head>
<title>Button Example</title>
</head>
<body>

<button id="btnSave">Save</button>

<button onclick="disableButton()">Disable Save Button</button>

<script>
function disableButton()
{
document.getElementById("btnSave").disabled = true;
}
</script>

</body>
</html>

Result

Before

[Save]

[Disable Save Button]

After Clicking

[Save]   (Disabled)

[Disable Save Button]

Example 5: Show Selected Option from DropDown

<!DOCTYPE html>
<html>
<head>
<title>Dropdown Example</title>
</head>
<body>

<select id="course">
<option>Java</option>
<option>Python</option>
<option>JavaScript</option>
<option>C#</option>
</select>

<button onclick="showCourse()">Show Course</button>

<p id="output"></p>

<script>
function showCourse()
{
var course = document.getElementById("course").value;

document.getElementById("output").innerHTML =
"Selected Course : " + course;
}
</script>

</body>
</html>

Result

If the selected option is

Python

After clicking Show Course

Selected Course : Python

Common Properties Used with getElementById()

PropertyDescriptionExample
innerHTMLChange HTML contentelement.innerHTML = "Hello";
valueGet or set textbox valueelement.value
style.colorChange text colorelement.style.color = "red";
style.backgroundColorChange background colorelement.style.backgroundColor = "yellow";
disabledEnable or disable controlelement.disabled = true;
srcChange image sourceelement.src = "image.jpg";

Summary

document.getElementById() allows you to access and manipulate HTML elements by their unique id. It is commonly used to:

  • Read user input from text boxes (value)
  • Change text or HTML (innerHTML)
  • Modify CSS styles (style)
  • Enable or disable controls (disabled)
  • Change images (src)
  • Work with dropdowns, buttons, checkboxes, and other HTML elements

It is one of the most fundamental and frequently used methods in JavaScript for interacting with web pages.

Cookies login

  Match login id and password using cookies under 1 minutes if user not input valid I'd pass in 1 min then cookies expired and go to hom...