Forms and Files: One Mark Questions, Book Back Answers and Notes
This chapter shows how an HTML form collects data from the user and how a PHP script on the server receives and checks it. It covers the GET and POST methods, client-side and server-side validation, and the PHP functions fopen, fread, fwrite and fclose for working with files.
Study this chapter
Asked in previous board exams
2 questions from this chapter came in 2 past public exams. Open any of them to see the answer and explanation.
-
March 2026
What form control allows the user to select multiple values?See answer in the solved paper →
-
March 2023
In which input field only one option can be selected ?See answer in the solved paper →
About Forms and Files
Medium ~90 min study
A static page can only show information. As soon as a page needs something from the user, such as a name, an email address or a choice of course, it needs a form. Text boxes, checkboxes, radio buttons and submit buttons are all form controls. This chapter follows that data from the browser to the server.
The chapter joins two things you have already seen: HTML for building the page and PHP for the server side. When the user clicks submit, the browser sends the form data using the method named in the form, GET or POST. PHP picks up the values from $_GET or $_POST, checks that they are valid, and can then store them or read other data from a file on the server.
Exam questions from this chapter usually ask for the difference between GET and POST, the meaning of client-side and server-side validation, and the purpose and syntax of fopen(), fread(), fwrite() and fclose(). Book back MCQs often give a task, such as closing an opened file, and ask which function does it, so learn each function together with what it does.
What you'll learn
- Build an HTML form using text inputs, radio buttons, checkboxes, drop-down lists and a submit button.
- State the differences between the GET and POST methods and choose the right one for a form.
- Add client-side validation to form fields using attributes such as required.
- Write PHP code that checks submitted values on the server before using them.
- Read submitted form data in PHP using the $_GET and $_POST superglobals.
- Open, read, write and close a file on the server using fopen, fread, fwrite and fclose.
Before you start
- HTML tags and the basic layout of a form.
- Basic PHP syntax and variables.
- The idea of a client and a server, and what an HTTP request is.
Topics covered in this chapter
Forms and Files explained
Web Forms and File Handling in PHP
Collecting Input with HTML Forms
A form is written inside the <form> tag and holds the controls the user fills in. The common controls are a single-line text input, a multi-line text area, checkboxes (the user may tick more than one), radio buttons (only one in a group can be chosen), a drop-down list made with select, a file upload control and the submit button. Two attributes of the form tag decide what happens on submit: action gives the PHP file that will receive the data, and method says whether it is sent by GET or POST. Choosing the right control for each question, for instance radio buttons for gender and checkboxes for hobbies, keeps the collected data clean.
Sending Data: GET and POST
With GET, the form values are added to the end of the URL as a query string, like result.php?name=Kavi&roll=12. Anyone can see them in the address bar, the page can be bookmarked, and the amount of data is limited by the URL length. This suits search boxes and other harmless requests. With POST, the values travel inside the body of the HTTP request, so they do not appear in the URL and larger amounts of data can be sent. Use POST for passwords and login forms. Keep in mind that POST only hides the data from the address bar; it is not encryption. On the server, PHP stores the received values in the superglobal arrays $_GET and $_POST, so a field named email sent by POST is read as $_POST["email"].
Client-Side and Server-Side Validation
Validation means checking the submitted data against rules before using it. The textbook gives rules such as: a name must contain only letters and spaces, an email must contain @ and a dot, a website field must hold a valid URL, and at least one option must be chosen from radio buttons, checkboxes or a drop-down list. Client-side validation happens in the browser before the form is sent, using HTML attributes like required or JavaScript, and gives the user instant feedback. Server-side validation happens in the PHP script after the data arrives. The browser check can be switched off or skipped, so the server check must always be done. Using both gives quick feedback for the user and safe data for the server.
Working with Files in PHP
PHP can store data in files on the server and read it back. fopen() opens a file and takes two arguments, the file name and the mode, such as "r" for reading or "w" for writing, as in $myfile = fopen("student.txt", "r") or die("Unable to open file!");. It returns a file pointer (handle), which the other functions use, or false if the file cannot be opened. fread($myfile, filesize("student.txt")) reads the file up to the number of bytes given. fwrite($myfile, $txt) writes text into the file. When you are done, fclose($myfile) closes the file and frees the resources held for it. Servers use these functions to keep logs, save form entries and store reports.
Common mistakes to avoid
- Sending a password with the GET method. The password then appears in the URL and in the browser history. Use POST for such forms.
- Depending only on browser validation. A user can disable JavaScript or send a request directly, so the PHP script must check the data again on the server.
- Calling fread() or fwrite() on a file that was never opened. First get a file object from fopen() with the correct mode, then pass that object to the other functions.
- Opening a file in "w" mode when you meant to read it or add to it. Write mode empties an existing file before writing, so the old contents are lost.
- Not calling fclose() after finishing with a file. The file stays open and holds server resources until the script ends.
Test yourself on these with the practice test, then check the worked reasoning in the solved MCQs.
Frequently asked questions
What is the difference between GET and POST in PHP?
GET adds the form data to the URL as a query string, so it is visible, can be bookmarked and is limited in size. POST sends the data in the request body, so it does not show in the URL and can carry more data. Use POST for passwords and GET for searches. PHP reads them from $_GET and $_POST.
Why is server-side validation necessary?
Client-side checks run in the user's browser, and the user can turn off scripts or send a request without using the form at all. Server-side validation in PHP checks every value after it reaches the server, so the script only works with data that meets the rules.
How do you open and read a file in PHP?
Open it with fopen(), giving the file name and the mode "r" for reading. This returns a file object. Pass that object to fread() along with the number of bytes to read, usually filesize() of the file. Finally close the file with fclose().
What does the action attribute do in an HTML form?
action names the file, usually a PHP script, that receives the form data. When the user clicks submit, the browser sends all the form values to that file, using the method given in the method attribute.
How do you check required fields in a form?
In the browser, add the required attribute to the input tag so the form cannot be submitted while that field is empty. On the server, use an if statement in PHP to check that the value in $_POST or $_GET is not empty before you use it.
What happens if I forget to close a file in PHP?
The file stays open and keeps using server resources. PHP closes open files when the script finishes, but a long-running script or many open files can cause problems, and other scripts may have trouble writing to the same file. Calling fclose() as soon as you finish is good practice.
Last updated 15 September 2026