PHP Form Complete
In this page:
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$errors = [];
$value = htmlspecialchars(trim($_POST["field_name"])); // sanitize
if (empty($value)) {
$errors[] = "message"; // validate
}
if (empty($errors)) {
// process the data
}
}
एक Complete Form Script Structure करना
एक complete form-handling script आमतौर पर एक pattern follow करती है: check करें कि form submit हुआ (आमतौर पर $_SERVER["REQUEST_METHOD"] के through), अगर हाँ तो हर field validate और sanitize करें, कोई भी errors collect करें, और या तो एक success message दिखाएँ या errors और पहले भरी गई values के साथ form फिर दिखाएँ।
उदाहरण: Structuring a Complete Form Script
<?php
$_SERVER['REQUEST_METHOD'] = 'POST';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$errors = [];
// validate and sanitize fields here
echo empty($errors) ? "Success" : "Redisplay with errors";
}
?>
Login to try C/C++/Java/PHP code in the editor
हर Field Validate करना
हर field को अपना validation rule मिलता है जो इसमें जो रखा है उसके हिसाब से उचित हो -- एक name field check करता है कि यह खाली नहीं है, एक email field check करता है कि यह एक valid address जैसा दिखता है, एक message field एक minimum length check कर सकता है -- और हर failed check errors array में एक clear, specific message add करता है।
उदाहरण: Validating Every Field
<?php
// Declare `$name`, set to ""
$name = "";
// Declare `$email`, set to "not-an-email"
$email = "not-an-email";
// Declare `$errors` as an empty array
$errors = [];
if (empty($name)) $errors[] = "Name is required";
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = "Invalid email";
// Print a human-readable dump of `$errors`
print_r($errors);
?>
Login to try C/C++/Java/PHP code in the editor
Input इस्तेमाल करने से पहले Sanitize करना
Sanitize करने का मतलब है किसी value को साफ करना ताकि यह इस्तेमाल करने के लिए safe हो -- htmlspecialchars() page पर वापस दिखाए जाने पर submitted text को HTML की तरह interpret होने से रोकता है, और trim() validation और storage से पहले accidental leading/trailing whitespace हटाता है।
उदाहरण: Sanitizing Input Before Using It
<?php
// Declare `$comment`, set to " <script>bad</script> "
$comment = " <script>bad</script> ";
// Declare `$clean`, set to `htmlspecialchars(trim($comment))`
$clean = htmlspecialchars(trim($comment));
// Print `$clean` to the output
echo $clean;
?>
Login to try C/C++/Java/PHP code in the editor
Errors के बाद Form फिर Populate करना
जब validation fail हो, user की पहले से type की गई values (value="" attribute इस्तेमाल करके) वापस भरे हुए form को फिर दिखाना उन्हें सब कुछ दोबारा type करने से बचाता है -- सिर्फ actually fail हुए fields को correct करने की ज़रूरत है।
उदाहरण: Re-populating the Form After Errors
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print '<input type="text" name="name" value="' . htmlspecialchars($name) . '">' to the output
echo '<input type="text" name="name" value="' . htmlspecialchars($name) . '">';
?>
Login to try C/C++/Java/PHP code in the editor
एक Complete Contact Form Example
सब कुछ साथ रखना -- submission check, validation, sanitization, error display, और value re-population -- एक पूरा, realistic contact-form script produce करता है: यह name, email, और message validate करता है; कोई check fail होने पर specific errors दिखाता है; और हर rule pass होने पर ही success confirm करता है।
उदाहरण: A Complete Contact Form Example
<?php
// Declare `$name`, set to `trim($_POST['name'] ?? '')`
$name = trim($_POST['name'] ?? '');
// Declare `$email`, set to `trim($_POST['email'] ?? '')`
$email = trim($_POST['email'] ?? '');
// Declare `$message`, set to `trim($_POST['message'] ?? '')`
$message = trim($_POST['message'] ?? '');
// Declare `$errors` as an empty array
$errors = [];
if (empty($name)) $errors[] = "Name required";
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = "Valid email required";
if (strlen($message) < 10) $errors[] = "Message too short";
// Print `empty($errors) ? "Message sent!" : implode(", ", $errors)` to the output
echo empty($errors) ? "Message sent!" : implode(", ", $errors);
?>
Login to try C/C++/Java/PHP code in the editor
- fields validate करना लेकिन उन्हें sanitize करना भी भूल जाना (या उल्टा) -- validation correctness check करता है, sanitization potentially dangerous content साफ करता है, और एक robust form को दोनों चाहिए।
- एक failed validation के बाद उन values को preserve किए बिना form फिर दिखाना जो user ने पहले से type की थीं, उन्हें सब कुछ शुरू से दोबारा type करने पर मजबूर करते हुए।
- यह भरोसा करना कि अकेले client-side (JavaScript) validation काफी है, और server-side validation को पूरी तरह skip करना -- client-side checks हमेशा bypass किए जा सकते हैं।
- एक complete form-handling script combine करती है: submitted data पढ़ना, हर field validate करना, values sanitize करना, error messages collect करना, और conditionally success या form फिर दिखाना।
- एक failed submission पर values को form में वापस populate करना चाहिए, ताकि user को सब कुछ दोबारा type न करना पड़े।
- Client-side और server-side validation दोनों का role है, लेकिन server-side validation वह है जिसे bypass नहीं किया जा सकता और इसे कभी skip नहीं करना चाहिए।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: