← Back to PHP Course | Chapter 10: Forms & Validation | Lesson 1 of 8

PHP Form Handling

Form handling PHP का किसी web form में visitor द्वारा type किया गया receive करना और उसके साथ कुछ उपयोगी करना है। यह किसी receptionist जैसा है जो हर भरा हुआ card पढ़ता है और उसे आगे पास कर देता है।
Syntax
php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $value = $_POST["field_name"];
}

$value = $_GET["field_name"];

GET Forms Handle करना

एक HTML form का data $_GET या $_POST superglobal array के through PHP तक पहुँचता है, form के method attribute के आधार पर, हर array key corresponding input field के name attribute से match करते हुए।

उदाहरण: Handling GET Forms

php
<?php
// Set `$_GET['search']` to "php tutorials"
$_GET['search'] = "php tutorials";
// Print `$_GET['search']` to the output
echo $_GET['search'];
?>

POST Forms Handle करना

किसी script के top पर $_SERVER[REQUEST_METHOD] === POST check करना यह detect करने का standard तरीका है कि form actually submit हुआ या नहीं, बनाम page सिर्फ पहली बार normally load हो रहा हो।

उदाहरण: Handling POST Forms

php
<?php
// Set `$_SERVER['REQUEST_METHOD']` to 'POST'
$_SERVER['REQUEST_METHOD'] = 'POST';
// Check whether `$_SERVER['REQUEST_METHOD'] === 'POST'`
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Print "Form was submitted" to the output
    echo "Form was submitted";
// Otherwise, run this branch
} else {
    // Print "Just a normal page load" to the output
    echo "Just a normal page load";
}
?>

Form Submission Validate करना

$_POST या $_GET से आने वाली हर value को untrusted user input माना जाना चाहिए — इसे database query, file path, या displayed HTML में इस्तेमाल करने से पहले हमेशा इसका type और content validate करें।

उदाहरण: Validating Form Submission

php
<?php
// Set `$_POST['age']` to "25"
$_POST['age'] = "25";
// Declare `$age`, set to `$_POST['age']`
$age = $_POST['age'];
// Check whether `is_numeric($age) && $age > 0`
if (is_numeric($age) && $age > 0) {
    // Print "Valid age: $age" to the output
    echo "Valid age: $age";
}
?>

Multi-value Inputs Process करना

htmlspecialchars() को किसी भी user-submitted value को HTML में वापस echo करने से पहले उसे wrap करना चाहिए, किसी दूसरे visitor के browser में execute होने से किसी form field में type किए गए malicious script tag को रोकते हुए (cross-site scripting)।

उदाहरण: Processing Multi-value Inputs

php
<?php
// Set `$_POST['comment']` to "<script>alert('xss')</script>"
$_POST['comment'] = "<script>alert('xss')</script>";
// Print `htmlspecialchars($_POST['comment'])` to the output
echo htmlspecialchars($_POST['comment']);
?>

Empty Inputs Check करना

एक successful form submission के बाद redirect करना (Post/Redirect/Get pattern) browser को उसी form data को दोबारा submit करने से रोकता है अगर user resulting page refresh कर दे।

उदाहरण: Checking Empty Inputs

php
<?php
// After a successful POST, redirecting avoids duplicate submissions on refresh
$submitted = true;
if ($submitted) {
    echo "header('Location: /thank-you.php'); // Post/Redirect/Get pattern";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. जब form method="get" इस्तेमाल करता है तब $_POST पढ़ना, या इसका उल्टा।
  2. बिना validate या escape किए user input पर भरोसा करना।
  3. form fields पर name attributes भूल जाना, ताकि वे भेजे ही न जाएँ।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.