Table of Contents
PHP form handling introduction
हेलो दोस्तों आज हम इस post tutorial में form के बारे में जानेगे की actually form में data को fill करके submit करने के बाद क्या process होती है कैसे data को server side में collect करा जाता है और collect करने के लिए कोनसे method का इस्तेमाल किया जाता है ये सब हम basic से और पूरी जानकारी पूरी details से जानेगे छोटी सी छोटी बातो को ध्यान रखके सब हम जानेगे!.
actually form को create करने के लिए HTML का इस्तेमाल होता है. HTML में ऐसे कई सरे form supporting element है जिसके इस्तेमाल हम form में करके user की information को get करा सकते है. form element के attribute जैसे की action, method ये attribute form के खास attribute होते है. जिसमे action attribute यह define करता है की form के submit बाद data कोनसे location पर बजाना है मतलब की कोनसी file में data को send करना है वह deside करता है. जब की method attribute वह बताता है की जो form के data define किये हुवे वह कोनसे method से बजे जाये. data को GET और POST method से बेजा जा सकता है.
HTML form view
<!DOCTYPE html>
<html>
<head>
<title>PHP Form Handling</title>
</head>
<body>
<h2>PHP Form Handling</h2>
<form action="action.php" method="POST">
<div>
<label for="FirstName">First Name:<sup>*</sup></label>
<input type="text" name="firstname" id="FirstName" required>
</div>
<div>
<label for="Email">Email:<sup>*</sup></label>
<input type="email" name="email" id="Email" required>
</div>
<div>
<label for="phone">Phone:<sup>*</sup></label>
<input type="number" name="phone" id="Phone" required>
</div>
<div>
<label for="Subject">Subject:</label>
<input type="text" name="subject" id="Subject">
</div>
<div>
<label for="Gender">Gender:</label>
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
</div>
<div>
<input type="checkbox" id="book" name="book" value="book">
<label for="book"> I have a book</label><br>
<input type="checkbox" id="book1" name="book1" value="book1">
<label for="book1"> I have a book1</label><br>
<input type="checkbox" id="book2" name="book2" value="book2">
<label for="book2"> I have a book2</label><br><br>
</div>
<div>
<label for="Langauge">Langauge</label>
<select name="langauge">
<option>Select langauge:</option>
<option value="php">PHP</option>
<option value="html">HTML</option>
<option value="css">CSS</option>
</select>
</div>
<div>
<label for="Comment">Message:<sup>*</sup></label>
<textarea name="message" id="Comment" rows="4" cols="20">
</textarea>
</div>
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
output
PHP Form Handling
Form method attribute और $_GET, $_POST, $_REQUEST variable
Form Post Method का उपयोग करके सभी Field Names और Form fields के सभी data होते है. और post method में Data URL में दिखाई नहीं देता है. और php side data को collect करने के लिए $_POST varible का इस्तेमाल किया जाता है!.
form get method का उपयोग करके सभी field name और form fields के सभी data को send किया जाता है लेकिंग इस method से send की जनि वाले सभी data URL में दिखे देती है और php side इस data को collect करने के लिए $_GET variable का इस्तेमाल किया जाता है!. इसके आलावा $_REQUEST variable का बी इस्तेमाल किया जाता है!.
Form action attribute
Form submit करने के बाद action attribute में हमने जो location सेट की होती है वह form data send हो जायेंगे. और हमने action.php name की एक file set की है तो इस file में हमें form data को supperglobal variable $_POST and $_GET variable का इस्तेमाल करके data को collect कर सकते है. इन variable का इस्तेमाल करके data को collect करने के लिए form element के name attribute का इस्तेमाल करके data को get किया जाता है!.
action.php
<?php
if(isset($_POST['submit'])){
$firstname = $_POST['firstname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$gender = $_POST['gender'];
$book = $_POST['book'];
$book1 = $_POST['book1'];
$book2 = $_POST['book2'];
$langauge = $_POST['langauge'];
$message = $_POST['message'];
}
?>