Title: How to pass textfield data from html to servlet without using forms
Tags: java;javascript;ajax;jsp;jakarta-ee
Question: I am making an online shopping website. I want to provide an option to user that he/she can select different shipping address :-
      Shipping Details 

```         <input type="button" value="Same as billing address"   style="color: #FFFFFF;" class="link-style" onclick="test();"  /&gt;
             <table cellpadding="20"&gt;
                    <tr&gt;
                        <td &gt;Name : * </td&gt;
                        <td&gt;<input type="text" id="name" name="name" /&gt;</td&gt;

                    </tr&gt;
                     <tr&gt;
                        <td&gt;Contact Number : </td&gt;
                        <td&gt;<input type="text" name="cno" id="cno"  /&gt;</td&gt;
                    </tr&gt;
                    <tr&gt;
                        <td&gt;Address : * </td&gt;
                        <td&gt;<input type="text" name="address "id="address" /&gt;</td&gt;
                    </tr&gt;

                    <tr&gt;
                        <td&gt;City : *</td&gt;
                        <td&gt;<input type="text" name="city" id="city"  /&gt;</td&gt;
                    </tr&gt;
                    <tr&gt;
                        <td&gt;State : * </td&gt;
                        <td&gt;<input type="text" name="state" id="state" /&gt;</td&gt;
                    </tr&gt;
                    <tr&gt;
                        <td&gt;Country : *</td&gt;
                        <td&gt;<input type="text" name="country" id="country" /&gt;</td&gt;
                    </tr&gt;


                </table&gt;                
```

if user clicks on button then all fields are disabled. Otherwise he/she can fill diffrent shipping address. Problem is i don't want to use forms here. How to pass these parameters to a jsp or servlet in session or post? I already tried AJAX but i am unable to work with it properly 

```function setValue() {
    alert("hi");
    var name = document.getElementById("name").value;
    var cno = document.getElementById("cno").value;
    var address = document.getElementById("address").value;
    var city = document.getElementById("city").value;
    var state = document.getElementById("state").value;
    var country = document.getElementById("country").value;
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.open("post", "test.jsp", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-      urlencoded");
    xmlhttp.send("name=" + name);
    xmlhttp.send("cno=" + cno);
    xmlhttp.send("address=" + address);
    xmlhttp.send("city=" + city);
    xmlhttp.send("state=" + state);
    xmlhttp.send("country=" + country);



}
```

Test.jsp is not getting the parameters.


Here is the accepted answer: You must only call ```xmlhttp.send``` once, which requires you to concatenate the request parameters.

``` xmlhttp.send("name="+name+"&amp;cno="+cno+"&amp;address="+address+"&amp;city="+city+"&amp;state=" + state + "&amp;country="+country);
```


Here is another answer: try like this:

```var inputParam='{"name":"'+name+'","cno":"'+cno+'","address":"'+address+'","city":"'+city+'","state":"'+state+'","country":"'+country+'"}';

$.ajax({
        type: "POST",
        url: your base_url,
        data: {inputParam:inputParam,module :'your module name'},
        dataType: "json",
        async:false,
        success: function(msg)
        {
             console.log(msg);
        }
    });
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:03/15/14', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<java><javascript><ajax><jsp><jakarta-ee>', 'Title': 'How to pass textfield data from html to servlet without using forms', 'ViewCount': 1067, 'id': 22422160, 'user_AboutMe': '<p>I am a btech CSE student. Love to program in java and jsp :D</p>\n', 'user_AccountId': 3043588.0, 'user_DisplayName': 'Lets Code', 'user_Reputation': 693.0, 'user_Views': 71.0, 'user_id': 2579930.0}}