Short overview on Unit-IV
π Unit–IV: User Data Input through Forms
4a. Steps to Create an Input Form
π A form is used to collect input from users and send it to a server for processing.
Steps
-
Use
<form>tag in HTML to create the form. -
Define
action(where the form data will go) andmethod(how the data will be sent → GET/POST). -
Add input fields (
text,email,password,radio,checkbox, etc.). -
Add a submit button.
-
Process the data in a PHP file.
Example
π Diagram:
[User] → [Form Input] → [Submit] → [Server (PHP)] → [Output Display]
4b. Steps to Use PHP Super Global Methods
PHP provides superglobals ($_GET, $_POST, $_REQUEST) to collect data.
Steps
-
Create a form with
method="get"ormethod="post". -
Use respective superglobal in PHP to fetch data.
-
Process and display the result.
Example with GET
π Data appears in URL → getdata.php?age=20
Example with POST
π Data is not shown in URL (more secure).
Example with REQUEST
π $_REQUEST can handle both GET and POST.
4c. Steps to Create and Manage Sessions
π A session stores user data across multiple pages (unlike variables which are lost when page refreshes).
Steps
-
Start a session using
session_start();at the top of PHP file. -
Store session variables using
$_SESSION['key']. -
Access session variables on other pages.
-
Destroy session when user logs out.
Example
Summary Table
| Feature | Method | Use Case | Visibility |
|---|---|---|---|
$_GET | GET | Small data, search queries | Visible in URL |
$_POST | POST | Login forms, passwords | Hidden from URL |
$_REQUEST | GET+POST | General purpose | Depends on method |
| SESSION | Server-side | Login, shopping cart | Secure, persists |
Part–1: MCQs (15 Questions)
Q1. Which HTML tag is used to create a form?
a) <input>
b) <form> ✅
c) <action>
d) <data>
Q2. Which attribute of <form> defines the method used to send data?
a) type
b) action
c) method ✅
d) submit
Q3. Which superglobal is used to collect form data sent using POST method?
a) $_POST ✅
b) $_GET
c) $_REQUEST
d) $_SESSION
Q4. Data submitted through GET method is visible in:
a) Session
b) URL ✅
c) Database
d) Cookies
Q5. Which method is more secure for password submission?
a) GET
b) POST ✅
c) REQUEST
d) SESSION
Q6. What function is used to start a PHP session?
a) start_session()
b) begin_session()
c) session_start() ✅
d) session_open()
Q7. Which of the following is NOT a superglobal variable in PHP?
a) $_POST
b) $_DATA ✅
c) $_REQUEST
d) $_SESSION
Q8. Which PHP superglobal can handle both GET and POST data?
a) $_DATA
b) $_REQUEST ✅
c) $_SESSION
d) $_FORM
Q9. Which function is used to destroy a session in PHP?
a) end_session()
b) destroy_session()
c) session_destroy() ✅
d) exit_session()
Q10. What is the default method of a form if not specified?
a) GET ✅
b) POST
c) REQUEST
d) SESSION
Q11. Which superglobal is used to store session variables?
a) $_DATA
b) $_SESSION ✅
c) $_GLOBAL
d) $_POST
Q12. Which input type is used to create a password field?
a) text
b) hidden
c) password ✅
d) email
Q13. Which form method should be used for sensitive information?
a) GET
b) POST ✅
c) REQUEST
d) SESSION
Q14. Which PHP superglobal stores cookie values?
a) $_COOKIE ✅
b) $_POST
c) $_SESSION
d) $_GLOBALS
Q15. Which diagram represents user input flow correctly?
a) Form → Server → Output ✅
b) Server → Form → Output
c) Database → Form → Server
d) Output → Form → Server
✅ Part–2: Descriptive Q&A (3–4 Marks Each)
Q1. Define PHP forms. Explain the steps to create an input form.
Answer:
A PHP form is used to collect user data and send it to the server for processing.
Steps:
-
Use
<form>tag withactionandmethod. -
Add input fields (
text,password,email, etc.). -
Add a submit button.
-
Process form data using PHP (
$_GET/$_POST).
Q2. Differentiate between GET and POST methods.
Answer:
-
GET: Sends data through the URL, suitable for small data, not secure.
-
POST: Sends data in the request body, hidden from URL, secure for sensitive data like passwords.
Q3. What is the use of PHP superglobals?
Answer:
Superglobals are built-in PHP variables accessible anywhere.
-
$_GET→ collects form data via GET. -
$_POST→ collects form data via POST. -
$_REQUEST→ handles both GET and POST. -
$_SESSION→ stores session data. -
$_COOKIE→ stores client-side data.
Q4. Explain the steps to create and manage a session in PHP.
Answer:
-
Start session using
session_start();. -
Store data →
$_SESSION['user']="Jay";. -
Access session variables on another page.
-
End session with
session_destroy();.
Q5. Why are sessions used in PHP applications?
Answer:
Sessions store user data across multiple pages. They are used for:
-
Login systems (user authentication).
-
Shopping carts in e-commerce.
-
Tracking user preferences.
-
Ensuring secure user interaction.
Comments
Post a Comment