AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Communication Technology

switch, forms and the Add Term page

Week 13 · Unit 3 Creating Site Pages (6), Topics 2–3: the switch statement; the Add_Term page: designing the form, GET and POST, form controls, including the header and connection pages · 3 lessons
3

The form controls of the Add Term page

ICT Secondary 2 Term 1, Unit 3, Topics 2–3: pp. 93–100
GoalBuild the controls of the Add Term page and give each a clear name.
New words
text box · a one-line control: <input type="text">text area · a control for longer text: <textarea>file box · a control that chooses a file to send: <input type="file">submit button · the button that sends the form: <input type="submit">
Explanation

The Add Term page has a title in its own div and a form below it. The form has four controls for the four things we store, and a button. A text box takes the term and another takes its translation. A text area takes the definition, which is longer. A file box lets the visitor choose the picture. The submit button sends everything to add_term.php, the same page, which will handle it in Week 14. Every control has a name, and the name matters, because PHP finds the value by that name: $_POST['term'] will hold what was typed in the box named term. The book’s names (txt_term, TextArea1, File1, Submit1) are not clear, so we use term, trans, defe, picture and add_btn. Keep the names exactly the same in the form and in the PHP. The PHP that includes the header goes at the top of the page, and the PHP that reacts to the button goes below the form.

ControlnameWhat PHP gets
text boxterm$_POST['term']: the typed term
text boxtrans$_POST['trans']: its translation
text areadefe$_POST['defe']: the definition
file boxpicture$_FILES['picture']: the chosen file
submit buttonadd_btn$_POST['add_btn']: set when pressed
Worked examples
Html
1<div><h2>Add a term</h2></div>
2<form method="post" action="add_term.php" enctype="multipart/form-data">
3<p>Term: <input name="term" type="text"></p>
4<p>Translation: <input name="trans" type="text"></p>
5<p>Definition:<br><textarea name="defe" rows="4" cols="40"></textarea></p>
6<p>Picture: <input name="picture" type="file"></p>
7<p><input name="add_btn" type="submit" value="Add"></p>
8</form>
Output
The page shows the heading Add a term; then a text box for the term, a text box for the translation, a larger box for the definition, a file chooser for the picture, and a button labelled Add.
The whole form for the Add Term page, with a title in its own division. The picture of an invented file router.webp would be chosen with the file box.
Form saysPHP asks forWorks?
name="term"$_POST['term']yes
name="term"$_POST['Term']no: different letter case
name="defe"$_POST['definition']no: different name
A mismatch between the form and the PHP is a common mistake. Karim’s form names the box term, but his PHP asks for Term. PHP names are case-sensitive, so it finds nothing.
Class activity

On paper design the form for adding a book to a library (title, writer, short summary, a cover picture and a button). Write the HTML for each control with a clear name, then a table with the control, its name and what PHP would read.

Practice
1
Which control takes the definition, and why?
2
Why must the control names be the same in the form and the PHP?
3
Where does PHP find the chosen picture file?
4
What does the Add button send, and to which page?