← Back to jQuery Course | Chapter 9: AJAX Basics | Lesson 7 of 7

Form Data को Serialize करना

serialize method form controls को एक URL-encoded string में convert करता है। AJAX से form data भेजते समय यह उपयोगी है।
Syntax
javascript
var query = $(form).serialize();
var array = $(form).serializeArray();

serialize() क्या है?

serialize() किसी form के named inputs पर जाता है और उनकी current values को एक single URL-encoded query string में encode कर देता है, वही format जो browser एक normal form submission पर generate करता।

उदाहरण: What is serialize()?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <form id="myForm"><input name="q" value="hello"></form>
    <script>
      console.log($("#myForm").serialize());
    </script>
  </body>
</html>

Form Inputs Serialize करना

serialized string में सिर्फ named, successful form controls शामिल होते हैं, इसलिए हर input जिसे submit होना चाहिए उसे एक name attribute चाहिए।

उदाहरण: Serialize Form Inputs

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <form id="myForm"><input name="q" value="hello"><input value="ignored, no name"></form>
    <script>
      console.log($("#myForm").serialize());
    </script>
  </body>
</html>

Checkboxes और Radios Serialize करना

Checked checkboxes और selected radio buttons तब शामिल होते हैं जब उनके पास names हों, लेकिन unchecked checkboxes empty values की तरह नहीं बल्कि string से पूरी तरह छूट जाते हैं।

उदाहरण: Serialize Checkboxes and Radios

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <form id="myForm"><input type="checkbox" name="subscribe" checked><input type="checkbox" name="promo"></form>
    <script>
      console.log($("#myForm").serialize());
    </script>
  </body>
</html>

Serialized Data भेजना

serialized string को एक AJAX POST request के साथ server पर भेजा जा सकता है, सबसे common रूप से $.post() या $.ajax() के through, एक normal form submission को reproduce करते हुए।

उदाहरण: Send Serialized Data

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <form id="myForm"><input name="q" value="hello"></form>
    <script>
      $.post("data.php", $("#myForm").serialize(), function(response) {
      console.log(response);
      });
    </script>
  </body>
</html>

एक छोटा Form Project

serialize() contact forms, registration forms, feedback forms, और search forms के लिए उपयोगी है -- कोई भी ऐसा form जिसकी values capture करके भेजनी हों।

उदाहरण: Small Form Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <form id="contactForm"><input name="email" value="[email protected]"><input name="message" value="Hi"></form>
    <script>
      const data = $("#contactForm").serialize();
      console.log(data);
    </script>
  </body>
</html>
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. यह उम्मीद करना कि .serialize() बिना name attribute वाले inputs भी include करेगा, जबकि सिर्फ named fields include होते हैं।
  2. यह मान लेना कि unchecked checkboxes empty values की तरह दिखेंगे, जबकि वे string से पूरी तरह छूट जाते हैं।
  3. <form> के बजाय किसी input पर .serialize() call करना, या एक JavaScript object की उम्मीद करना जबकि यह एक URL-encoded string return करता है (objects के लिए .serializeArray() इस्तेमाल करें)।
चैप्टर सारांश
  • AJAX page reload किए बिना server से data load करता है।
  • load(), get(), post(), और getJSON() common तरीकों से data fetch या भेजते हैं।
  • getScript() scripts load करता है, और भेजने के लिए form data serialize किया जा सकता है।

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.