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

  1. Use <form> tag in HTML to create the form.

  2. Define action (where the form data will go) and method (how the data will be sent → GET/POST).

  3. Add input fields (text, email, password, radio, checkbox, etc.).

  4. Add a submit button.

  5. Process the data in a PHP file.

Example

<!-- form.html --> <form action="process.php" method="post"> Name: <input type="text" name="uname"><br> Email: <input type="email" name="uemail"><br> <input type="submit" value="Submit"> </form>
<!-- process.php --> <?php $name = $_POST['uname']; $email = $_POST['uemail']; echo "Welcome $name, your email is $email"; ?>

πŸ“Œ 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

  1. Create a form with method="get" or method="post".

  2. Use respective superglobal in PHP to fetch data.

  3. Process and display the result.


Example with GET

<form action="getdata.php" method="get"> Enter Age: <input type="number" name="age"> <input type="submit" value="Submit"> </form>
<!-- getdata.php --> <?php echo "Your age is " . $_GET['age']; ?>

πŸ“Œ Data appears in URLgetdata.php?age=20


Example with POST

<form action="postdata.php" method="post"> Password: <input type="password" name="pass"> <input type="submit" value="Login"> </form>
<!-- postdata.php --> <?php echo "Your password is " . $_POST['pass']; ?>

πŸ“Œ Data is not shown in URL (more secure).


Example with REQUEST

<?php echo "Name: " . $_REQUEST['uname']; ?>

πŸ“Œ $_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

  1. Start a session using session_start(); at the top of PHP file.

  2. Store session variables using $_SESSION['key'].

  3. Access session variables on other pages.

  4. Destroy session when user logs out.


Example

<!-- page1.php --> <?php session_start(); $_SESSION['username'] = "Jaydeep"; echo "Session started!"; ?>
<!-- page2.php --> <?php session_start(); echo "Welcome " . $_SESSION['username']; ?>
<!-- logout.php --> <?php session_start(); session_destroy(); echo "Session destroyed, user logged out!"; ?>

Summary Table

Feature   MethodUse CaseVisibility
$_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:

  1. Use <form> tag with action and method.

  2. Add input fields (text, password, email, etc.).

  3. Add a submit button.

  4. 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:

  1. Start session using session_start();.

  2. Store data → $_SESSION['user']="Jay";.

  3. Access session variables on another page.

  4. 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

Popular posts from this blog

Short overview on Unit-II

Unit - 1 Question and Answer

Short overview on Unit-III