MCQs on Forms and User Input | PHP Basics

Enhance your web development skills with this set of 30 multiple-choice questions focusing on HTML forms, PHP integration, GET vs POST methods, form validation, and handling user input with PHP. Ideal for learners!


Forms and User Input

HTML Forms and PHP

  1. Which HTML element is used to create a form?
    • a) <input>
    • b) <form>
    • c) <button>
    • d) <select>
  2. What is the correct method to submit a form in HTML?
    • a) GET
    • b) POST
    • c) PUT
    • d) Both a and b
  3. How can you send form data to a PHP script?
    • a) Using the action attribute of the form tag.
    • b) Using the target attribute of the form tag.
    • c) By setting the form’s method to put.
    • d) By setting the form’s action to $_POST.
  4. What is the default method used in HTML forms for data submission?
    • a) GET
    • b) POST
    • c) PUT
    • d) DELETE
  5. In an HTML form, which input type is used to create a checkbox?
    • a) type="text"
    • b) type="checkbox"
    • c) type="radio"
    • d) type="button"
  6. What attribute is used in HTML forms to specify the action URL for submitting data?
    • a) method
    • b) action
    • c) url
    • d) target
  7. How do you retrieve the value of an input field in PHP after a form is submitted?
    • a) $_GET['inputName']
    • b) $_POST['inputName']
    • c) $inputName
    • d) $GET['inputName']
  8. Which of the following is used to create a text input field in HTML?
    • a) <textarea>
    • b) <input type="text">
    • c) <input type="password">
    • d) <input type="file">
  9. What is the purpose of the name attribute in an HTML form element?
    • a) To specify the input’s default value.
    • b) To give the element an identifier for CSS styling.
    • c) To associate form data with a variable in PHP.
    • d) To specify the type of the input element.
  10. How do you define a drop-down list in an HTML form?
    • a) <input type="select">
    • b) <select>
    • c) <dropdown>
    • d) <option>

GET vs POST Methods

  1. Which of the following is true about the GET method?
    • a) Data is sent in the body of the HTTP request.
    • b) Data is visible in the URL.
    • c) It is used to send sensitive information.
    • d) It can handle large amounts of data.
  2. What is the main difference between the GET and POST methods?
    • a) GET sends data in the URL, POST sends data in the body of the request.
    • b) POST sends data in the URL, GET sends data in the body.
    • c) GET sends data as JSON, POST sends data as XML.
    • d) There is no difference.
  3. Which method is considered more secure for transmitting sensitive data?
    • a) GET
    • b) POST
    • c) Both GET and POST are equally secure.
    • d) Neither GET nor POST is secure.
  4. Which method is preferred for retrieving data from a server without changing any data on the server?
    • a) GET
    • b) POST
    • c) PUT
    • d) DELETE
  5. Which of the following can be sent using the POST method that can’t be sent with GET?
    • a) Form data
    • b) Files
    • c) Simple text
    • d) All of the above
  6. What is the maximum data size limit for GET method submissions?
    • a) No limit
    • b) Around 1 KB
    • c) Around 8 KB
    • d) 2 MB
  7. How are form data values transmitted using GET method?
    • a) Through headers
    • b) In the URL as query parameters
    • c) In the form body
    • d) In cookies
  8. What is one of the limitations of the GET method?
    • a) It can handle large amounts of data.
    • b) It exposes sensitive data in the URL.
    • c) It is slower than POST.
    • d) It is used for updating records.
  9. What is the correct HTTP status code for a successful POST request?
    • a) 200
    • b) 404
    • c) 500
    • d) 301
  10. Which of the following can the GET method be used for?
    • a) Retrieving data without changing the server state.
    • b) Sending sensitive user data.
    • c) Submitting a password.
    • d) Sending large files.

Form Validation

  1. Which of the following is used to ensure that a field in an HTML form is not empty?
    • a) required
    • b) validate
    • c) notnull
    • d) empty
  2. What is the purpose of form validation in PHP?
    • a) To check for errors before data submission.
    • b) To automatically correct form field values.
    • c) To confirm that the form data is encrypted.
    • d) To store the form data in a database.
  3. Which of the following HTML attributes can be used for basic form validation?
    • a) pattern
    • b) validate
    • c) check
    • d) confirm
  4. How would you check if a form field is empty in PHP?
    • a) if (empty($field))
    • b) if (isset($field))
    • c) if ($field == '')
    • d) Both a and c
  5. Which PHP function is commonly used for form data sanitization?
    • a) sanitize()
    • b) filter_var()
    • c) validate()
    • d) clean()
  6. What happens if a user submits an incomplete form in PHP with no validation?
    • a) The form will be rejected.
    • b) The form will be processed with empty data.
    • c) The server will display a default error.
    • d) Nothing will happen.
  7. What does the required attribute in HTML5 form inputs do?
    • a) Ensures the input is a valid email address.
    • b) Makes the input field optional.
    • c) Prevents form submission if the field is empty.
    • d) Checks the data type of the input.
  8. Which of the following is a correct way to validate email format using HTML?
    • a) <input type="text" email>
    • b) <input type="email">
    • c) <input type="email" pattern="email">
    • d) <input type="text" pattern="[a-zA-Z0-9@]+">
  9. How do you validate if a number is entered in a form field in PHP?
    • a) is_numeric()
    • b) is_string()
    • c) is_integer()
    • d) is_float()
  10. How can you display an error message for invalid form data in PHP?
    • a) echo or print the error message.
    • b) alert() the error message.
    • c) Redirect to another page.
    • d) Automatically fix the data and resubmit the form.

Answer Key

QnoAnswer
1b) <form>
2d) Both a and b
3a) Using the action attribute of the form tag.
4a) GET
5b) type="checkbox"
6b) action
7b) $_POST['inputName']
8b) <input type="text">
9c) To associate form data with a variable in PHP.
10b) <select>
11b) Data is visible in the URL.
12a) GET sends data in the URL, POST sends data in the body of the request.
13b) POST
14a) GET
15b) Files
16c) Around 8 KB
17b) In the URL as query parameters
18b) It exposes sensitive data in the URL.
19a) 200
20a) Retrieving data without changing the server state.
21a) required
22a) To check for errors before data submission.
23a) pattern
24d) Both a and c
25b) filter_var()
26b) The form will be processed with empty data.
27c) Prevents form submission if the field is empty.
28b) <input type="email">
29a) is_numeric()
30a) echo or print the error message.

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top