InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

jQuery tutorials

    jQuery Ajax

    • jQuery JSONP
    • jQuery .post()
    • jQuery .load()
    • load script with cache enabled
    • jQuery .ajax() file upload

    jQuery CSS

    • change table cells color based on value
    • check if element is visible in viewport
    • display auto fade out message
    • fitvids.js for automatically resizing videos
    • get class list of an element
    • jQuery toggleClass() examples
    • make a div stick at top on scroll
    • table with alternate color rows

    jQuery Events

    • Javascript/jQuery - disable right click
    • jQuery - text input field - change keyup and paste events
    • trigger click using jQuery or Javascript

    jQuery DOM

    • jQuery html() vs text()
    • Check if an element exists
    • find total DOM elements
    • jQuery encode string to html entities
    • jQuery - read and modify iframe content
    • jQuery - create iframe with content
    • jQuery - find select element selectedIndex, value and text
    • jQuery - get checkbox value and checked state
    • jQuery - get javascript object

    jQuery Cookbook

    • get jQuery version
    • jQuery - add text to existing div
    • jQuery sliding effect - slideUp, slideDown, slideToggle

    jQuery UI

    • jQuery UI make element movable
    • jQuery ui slider and input text box - one way binding
    • jQuery ui slider and input text box - two way binding
     
    • Home
    • > Tutorials
    • > Javascript
    • > jQuery

    jQuery post form data using ajax

    on Feb 14, 2016

    Javascript code snippet to submit a form using POST to server and process returned data. Here when submit button is clicked we serialize whole form and POST it to server endpoint url. For the purpose of this tutorial, the endpoint is just returning the posted params.

    Post form data using Ajax without modification

    We’ll use jQuery .serialize() and post it using jQuery .post(). Note this approach will not include input files.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <form id="form1" action="/api/post-dump.php" method=POST>
    name1: <input type="text" name="name1" value="value1" /><br/>
    name2: <input type="text" name="name2" value="value2" />
    <input type="submit" name="submit" value="submit"/>
    </form><br/>
    Response from server:
    <pre id="result" style="background-color:lightgray;"></pre>
    
    <script type="text/javascript">
    $("#form1").submit(function() {
      var action = $("#form1").attr("action");
      $.post(action, $("#form1").serialize(), function(ret) {
        $("#result").html(ret);
      });
      return false;
    });
    $("#form1").trigger("submit");
    </script>
    refresh done
    try it online

    Post form data using Ajax with modification

    We’ll use jQuery .serializeArray(), modify returned array and post it using jQuery .post(). Note this approach will not include input files.

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <form id="form1" action="/api/post-dump.php" method=POST>
    name1: <input type="text" name="name1" value="value1" /><br/>
    name2: <input type="text" name="name2" value="value2" />
    <input type="submit" name="submit" value="submit"/>
    </form><br/>
    Response from server:
    <pre id="result" style="background-color:lightgray;"></pre>
    
    <script type="text/javascript">
    $("#form1").submit(function() {
      var action = $("#form1").attr("action");
      dataArr = $("#form1").serializeArray();
      dataArr.push({name:"name3", value: "value3"});
      $.post(action, dataArr, function(ret) {
        $("#result").html(ret);
      });
      return false;
    });
    $("#form1").trigger("submit");
    </script>
    refresh done
    try it online

    Few points to note

    1. jQuery("#form1").serialize() will serialize form data and it will produce the following string in our example:

      name1=value1&name2=value2
      
    2. jQuery("#form1").serializeArray() will serialize form data and it will produce an array of Objects which can be modified.
    3. jQuery serialize method excludes the submit button name and value in serialized data.

    Suggested posts:

    1. CSS – align text in center horizontally
    2. jQuery encode string to html entities
    3. Requirejs – quickstart guide for beginners
    4. jQuery ui slider and input text box – one way binding
    5. CSS flexbox – display flex and inline-flex
    6. How to trigger click using jQuery or Javascript
    7. CSS general sibling selector
    8. Online tools to create comic strips
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Javascript, jQuery, jQuery Ajax, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress