← QCE Digital Solutions Unit 3

Server-side code

Code that runs on the web server, not the browser. It handles a request, processes form data, queries the database and builds the response sent back to the client.

Understanding Server-side code in QCE Digital Solutions Unit 3

What it does: The server builds the HTML text and returns it as the response, which the browser then displays. This route tells the server which function runs when a POST request for /login arrives, so it handles the request. Server-side code runs on the web server. Code that runs in the browser is client-side code. Database queries are made on the server. Hover effects happen in the browser using CSS or JavaScript. The server must first understand the request data and fetch what it needs before it can build a response that uses the results.

Common mistakes: Submitted form data is read by indexing the form with square brackets, request.form["name"]. The form is not a function, so it cannot be called with round brackets. A function definition must end with a colon. It should be def show_users(db): so the server code can run.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 15–119) and word search (Modules 15–176).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Where does server-side code run?
On the web serverIn the user's web browser
Matches
<script>db.query("SELECT * FROM users")</script>
↓
Database queries are put in browser code, but the browser should never access the database directly; the server-side code must run the query.
Applying
Meanings
Term
server-side code
Meaning
Code that runs on the web server, not the browser. It handles a request, processes form data, queries the database and builds the response sent back to the client.
Practice
form = {"name": "Ava"}
Testing
Gaps
name = request.["name"]
Check Answer
Spots
1def handle(request):
2 name = request.form("name")

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Reef Shark' badge — Reef Ruler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Where does server-side code run?
A. On the web server ✓
B. In the user's web browser
Server-side code runs on the web server. Code that runs in the browser is client-side code.
2. A student submits a login form. Which code checks the typed password against stored data?
A. Server-side code ✓
B. The HTML form tag on its own
The server-side code processes the form data and compares it with the records in the database. The form tag only collects and sends the data.
3. Which job is done by server-side code?
A. Changing a button colour when the mouse hovers over it
B. Querying the database for a user's orders ✓
Database queries are made on the server. Hover effects happen in the browser using CSS or JavaScript.
4. Which statement is true?
A. The browser never talks to the database directly; server-side code does it ✓
B. The browser sends SQL straight to the database server
Server-side code sits between the client and the database. This keeps the database safe and controlled.
5. What does server-side code build after it has processed a request?
A. The user's keyboard input
B. The response sent back to the client ✓
After processing the form data and querying the database, the server-side code builds the response (such as an HTML page) and sends it to the client.

Match the Pairs

cursor.execute("SELECT * FROM users") → Server-side code querying the database for stored data
name = request.form["name"] → Server-side code reading form data sent from the client
Server-side code builds the response that is sent back to the client → return "<h1>Hello " + name + "</h1>"
Server-side code handles a request that arrives from the browser → @app.route("/login", methods=["POST"])
<script>db.query("SELECT * FROM users")</script> → Database queries are put in browser code, but the browser should never access the database directly; the server-side code must run the query.
def login(): name = request.form["name"] → The function never returns anything, so the server has no response to send back to the client.

Key Term

server-side code
Code that runs on the web server, not the browser. It handles a request, processes form data, queries the database and builds the response sent back to the client.

Fill in the Blanks

1. def login(): name = request.form["name"]
Hint: Type the request attribute that holds the fields a user submitted in an HTML form.
2. def users(): cur = db.cursor() cur.execute("SELECT * FROM users")
Hint: Type the cursor method that sends an SQL query to the database server.
3. def welcome(): return render_template("home.html")
Hint: Type the Flask function that builds an HTML page to send back to the client as the response.
4. @app.route("/login", methods=["POST"]) def login():
Hint: Type the HTTP method a browser uses to send form data to the server.
5. def login(): if request.method == "POST": name = request.form["name"]
Hint: Type the request attribute that tells the server which kind of HTTP request arrived.

Spot the Error

Question 1
A def handle(request):
B name = request.form("name")
C msg = "Hello, " + name
D return msg
Line B — Submitted form data is read by indexing the form with square brackets, request.form["name"]. The form is not a function, so it cannot be called with round brackets.
Question 2
A def show_users(db)
B q = "SELECT * FROM users"
C rows = db.execute(q)
D return str(rows)
Line A — A function definition must end with a colon. It should be def show_users(db): so the server code can run.
Question 3
A def get_users(db):
B cur = db.cursor()
C q = "SELECT * FORM users"
D cur.execute(q)
E return cur.fetchall()
Line C — The SQL keyword is FROM, not FORM. The database would reject this query, so the server could not get the data. It should be SELECT * FROM users.
Question 4
A def page(name):
B h = "<p>" + name + "<p>"
C return h
Line B — The closing tag must be </p>. Using <p> again opens a second paragraph, so the HTML response sent to the client is broken.
Question 5
A def check_age(form):
B age = int(form["age"])
C if age >= "18":
D return "Welcome"
E return "Too young"
Line C — The form value was converted to an integer, so it must be compared with the number 18, not the string "18". Comparing an int with a string causes an error.

Student with a class code? Join your class →