Forms & User Input · Lesson 3 of 6
POST Method
Submit data in a request body and validate it on the server.
Lesson
POST sends form fields in the request body and is generally used for operations that change state. It does not automatically make a request secure or private; use HTTPS, validate input, authorize the action, and protect browser forms against CSRF. Redirect after a successful form submission to avoid accidental resubmission.
Check the method and the input type before processing.
Further guidance
Treat every submitted field and uploaded file as untrusted. Validate type, length, allowed values, and request method on the server; use CSRF protection for state changes. Escape values for their output context, and never use browser validation as the only safeguard.
Practice task: Create a fresh example for POST Method. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.
Code examples
POST Method
PHP
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
echo $email === false || $email === null ? 'Invalid email' : 'Accepted';
}
Expected output
Accepted for a valid submitted email
Key points
- Submit data in a request body and validate it on the server.
- Validate input on the server before processing it.
- Escape output for its context and protect state-changing forms from CSRF.
Your learning progress
Finish this lesson
Optional practice
PHP playground
This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.
Execution is disabled. No code is sent to a server.