PHP Form Handling In Hindi

📔 : PHP 🔗

PHP में हम form को submit कर सकते है जिससे user data को process कर सकें , या  need के according database में save कर सकें।


PHP में generally हम form को submit / handle करने के लिए हम generally three Predefined Super Global Variables का use करते हैं।

  1. $_GET
  2. $_POST
  3. $_REQUEST

इन variables का use हम submit किये गए form के according use करते हैं। इस topic में $_GET के बारे में पढ़ेंगे और जानेंगे कि किस तरह से form handling में यह variables use होता है।

$_GET

PHP $_GET Super Global Variable है means $_GET के through form data access करने के लिए हमें इस global variable बनाने की जरूरत नहीं होती , PHP Script में इसे कहीं भी access कर सकते हैं।


$_GET variable का use हम get method type के साथ submit किये गए form को handle करने के लिए use करते हैं । किसी भी form को जब हम get method के साथ submit करते हैं तो उस form का data query string के रूप में URL के साथ key=value (जहां key name submit किये गए form का input name होता है और value, उस input में enter की गयी value ) pair में append हो जाता है , जिसे हम अपने URL में आसानी से देख सकते हैं।


$_GET Submitted Form Data को एक Array में store करता है , जहां key name input field का नाम और value input field में enter की गयी value होती है। जिसे हम input field name के साथ आसानी से access कर सकते हैं।

PHP Handle Form Using $_GET

File : test.php

Copy Fullscreen Close Fullscreen
<!DOCTYPE html>
<html>
<head>
<title>PHP Form Handling Using $_GET</title>
</head>
<body>
<form action="get.php" method="get">
<p>Name : <input name="name" type="text" placeholder="Enter Your Full Name"></p>
<p>Age : <input name="age" type="number" min="18" max="120" placeholder="Enter Your Age"></p>
<p><button type="submit">Submit</button></p>
</form>
</body>
</html>

Another file where we will access form data : get.php

Copy Fullscreen Close Fullscreen
<?php  
/* here we will handle submitted form*/
echo 'Entered All Values : <pre>';
print_r($_GET);
echo '</pre>';
echo 'Your Full Name : '.$_GET['name']."<br>";
echo 'Your Age : '.$_GET['age'];
?>
Output
PHP Form handling
After Submit :
PHP get request data

Explanation : दिए गए Example को अब हम step by step समझेंगे -

  1. सबसे पहले हमने एक form बनाया जिसका submit method get / GET है जिससे हम $_GET के through form data access कर सके , अगर आप Form का method define नहीं भी करते तो automatically get ही होता है। form action get.php है means जब हम form submit करेंगे तो वह form हम get.php file में handle करेंगे।

  2. उसके बाद दो input field name और age बनाये जिससे हम value Enter कर सके । age field के लिए हमने HTML Validation लगाएं हैं जिससे end user 0 से काम और 120 से ज्यादा age enter न कर पाए।  और एक Button जिसका type submit है।

  3. अब get.php file में हमने सबसे पहले तो $_GET Super  Global variable को print कराया।

  4. और फिर submit की गयी values को एक एक करके access किया।

  5. Note - अगर आप URL देखेंगे तो पाएंगे कि submit की गयी values URL के साथ append हो गयी है।


Note - query string के रूप में date हम form के साथ साथ किसी link में भी आसानी से append कर सकते हैं । इसके लिए हमें key value pair में data pass कर देता।

When Should We Use It ?

$_GET use करने से पहले इसके बारे में और अधिक जानते हैं जिससे यह clear हो सके कि कब हमें ये use करना चाहिए और कब नहीं।

हालाँकि get request के through हम limited data ही send कर सकते हैं , क्योंकि यह URL में visible होता है और हर कोई इसे देख सकता है। get request के through लगभग हम 2000 characters ही send कर सकते हैं।

$_GET का use कभी भी important data send करने के लिए नहीं करना चाहिए , जैसे - User Authentication या Payment Process etc...

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers