Server-side code

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Server-side code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where does server-side code run?
A On the web server
B In the user's web browser
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
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
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

server-side code

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
def login(): name = request.____["name"]
2.
def users(): cur = db.cursor() cur._______("SELECT * FROM users")
3.
def welcome(): return _______________("home.html")
4.
@app.route("/login", methods=["____"]) def login():
5.
def login(): if request.______ == "POST": name = request.form["name"]

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def handle(request):
B name = request.form("name")
C msg = "Hello, " + name
D return msg
Question 2
A def show_users(db)
B q = "SELECT * FROM users"
C rows = db.execute(q)
D return str(rows)
Question 3
A def get_users(db):
B cur = db.cursor()
C q = "SELECT * FORM users"
D cur.execute(q)
E return cur.fetchall()
Question 4
A def page(name):
B h = "<p>" + name + "<p>"
C return h
Question 5
A def check_age(form):
B age = int(form["age"])
C if age >= "18":
D return "Welcome"
E return "Too young"

Answer Key

Server-side code
Section A: Quick Questions
Q1
A — On the web server
Q2
A — Server-side code
Q3
B — Querying the database for a user's orders
Q4
A — The browser never talks to the database directly; server-side code does it
Q5
B — The response sent back to the client
Section B: Match the Pairs
Q1
cursor.execute("SELECT * FROM users") → Server-side code querying the database for stored data
Q2
name = request.form["name"] → Server-side code reading form data sent from the client
Q3
Server-side code builds the response that is sent back to the client → return "<h1>Hello " + name + "</h1>"
Q4
Server-side code handles a request that arrives from the browser → @app.route("/login", methods=["POST"])
Q5
<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.
Q6
def login(): name = request.form["name"] → The function never returns anything, so the server has no response to send back to the client.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
def login(): name = request.form["name"]
Q2
def users(): cur = db.cursor() cur.execute("SELECT * FROM users")
Q3
def welcome(): return render_template("home.html")
Q4
@app.route("/login", methods=["POST"]) def login():
Q5
def login(): if request.method == "POST": name = request.form["name"]
Section E: Spot the Error
Q1
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.
Q2
A — A function definition must end with a colon. It should be def show_users(db): so the server code can run.
Q3
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.
Q4
B — The closing tag must be </p>. Using <p> again opens a second paragraph, so the HTML response sent to the client is broken.
Q5
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.