← Back to PHP Course | Chapter 2: Output & Input | Lesson 3 of 8

PHP User Input (HTML Forms)

एक web form एक paper sign-up sheet जैसा है: visitors blanks भरते हैं और इसे सौंप देते हैं। PHP वह person है जो desk पर बैठा है जो पढ़ता है कि उन्होंने क्या लिखा और decide करता है कि इसका क्या करना है।
Syntax
php
<form action="script.php" method="POST">
    <input type="text" name="field_name">
    <button type="submit">Send</button>
</form>

<?php
if (isset($_POST["field_name"])) {
    echo $_POST["field_name"];
}
?>

HTML Forms और PHP

जब एक HTML <form> submit होता है, browser हर named input की value package करता है और इसे उस URL पर भेजता है जो form का action attribute point करता है, form द्वारा specify किए गए method (GET या POST) का इस्तेमाल करते हुए।

उस URL पर चल रहा PHP फिर corresponding superglobal array से submitted values पढ़ता है।

उदाहरण: HTML Forms and PHP

php
<!DOCTYPE html>
<html>
<body>
<form action="process.php" method="POST">
  <input type="text" name="username">
</form>
<?php
// PHP reads the submitted value from $_POST['username']
?>
</body>
</html>

Simple Form Text Handle करना

$_POST और $_GET associative arrays हैं जहाँ हर key किसी HTML input के name attribute से match करती है।

एक live form में, PHP हर request पर इन्हें automatically populate करता है; सीखते समय, मैन्युअली $_POST में values assign करके एक submission simulate करना normal है ताकि आप बिना browser के handling logic test कर सकें।

उदाहरण: Handling Simple Form Text

php
<?php
// Simulating a form submission for testing
$_POST['username'] = "Alice";
echo $_POST['username'];
?>

Form Submit हुआ या नहीं Check करना

जब कोई form actually submit ही नहीं हुआ तब $_POST[email] पढ़ना एक 'undefined array key' warning trigger करता है।

पहले isset($_POST[email]) check करना, या $_SERVER[REQUEST_METHOD] inspect करना, आपकी script को एक genuine submission को पहली बार page visit से safely अलग करने देता है।

उदाहरण: Checking if Form is Submitted

php
<?php
// Check whether `isset($_POST['email'])`
if (isset($_POST['email'])) {
    // Print `"Form submitted with: " . $_POST['email']` to the output
    echo "Form submitted with: " . $_POST['email'];
// Otherwise, run this branch
} else {
    // Print "No form submitted yet." to the output
    echo "No form submitted yet.";
}
?>

Form Input Sanitization

$_GET, $_POST, या $_COOKIE से आई कोई भी value आपके control के बाहर से आती है और इसे untrusted माना जाना चाहिए।

htmlspecialchars() के बिना raw input को HTML output में pass करना एक attacker को एक <script> tag inject करने देता है जो किसी दूसरे user के browser में चलता है — एक classic Cross-Site Scripting (XSS) vulnerability।

उदाहरण: Form Input Sanitization

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']);
?>

Missing Inputs के लिए Default Values

एक ऐसी form key access करना जो कभी submit ही नहीं हुई (मान लीजिए एक unchecked checkbox) चुपचाप कुछ न return करने के बजाय एक warning throw करता है।

$_POST[newsletter] ?? false key के missing होते ही एक sensible default पर fall back करके इससे बच जाता है, किसी isset() check की ज़रूरत नहीं।

उदाहरण: Default Values for Missing Inputs

php
<?php
// Declare `$newsletter`, set to `$_POST['newsletter'] ?? false`
$newsletter = $_POST['newsletter'] ?? false;
// Print a detailed dump (with types) of `$newsletter`
var_dump($newsletter);
?>
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 submit हुआ है यह check किए बिना $_POST[email] पढ़ना, जो पहले page load पर एक 'Undefined array key' warning trigger करता है।
  2. htmlspecialchars() के बिना echo $_POST[name]; से submitted values print करना, जो cross-site scripting (XSS) की अनुमति देता है।
  3. किसी <input> पर name attribute भूल जाना, ताकि field कभी भेजी न जाए और $_POST में न दिखे।
🔒

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.