passing data from a client form via jquery ajax dinamicly

Posted by quantum62 on Stack Overflow See other posts from Stack Overflow or by quantum62
Published on 2012-07-07T09:08:11Z Indexed on 2012/07/07 9:15 UTC
Read the original article Hit count: 265

Filed under:
|
|
|

i wanna insert specification of members that enter in textboxs of form in the database .i do this operation with jquery ajax when i call webmetod with static value the operation do successfully.for example this code is ok.

 $.ajax({
 type: "POST",
 url:"MethodInvokeWithJQuery.aspx/executeinsert",
 data: '{ "username": "user1", "name":"john","family":"michael","password":"123456","email": "[email protected]", "tel": "123456", "codemeli": "123" }',
contentType: "application/json; charset=utf-8",
dataType: "json",                                                    
async: true,
cache: false,
success: function (msg) {
$('#myDiv2').text(msg.d);
},
error: function (x, e) {
alert("The call to the server side failed. " + x.responseText);         
                                                    }
                                                }
                                            );

but when i wanna use of values that enter in textboxes dynamically error occur.whats problem?i try this two code

<script type="text/javascript">
$(document).ready(
function () {
    $("#Button1").click(
        function () {
            var username, family, name, email, tel, codemeli, password;
            username = $('#<%=TextBox1.ClientID%>').val();
            name = $('#<%=TextBox2.ClientID%>').val();
            family = $('#<%=TextBox3.ClientID%>').val();
            password = $('#<%=TextBox4.ClientID%>').val();
            email = $('#<%=TextBox5.ClientID%>').val();
            tel = $('#<%=TextBox6.ClientID%>').val();
            codemeli = $('#<%=TextBox7.ClientID%>').val();

            $.ajax(
            {
                type: "POST",
                url: "WebApplication20.aspx/executeinsert",
                data: "{'username':'username','name':name,
                        'family':family,'password':password,
                        'email':email,'tel':tel,
                        'codemeli':codemeli}",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: true,
                cache: false,
                success: function(msg) {
                    alert(msg);
                },
                error: function (x, e) {
                    alert("The call to the server side failed. " 
                          + x.responseText);
                }
            }
        );
    }
 )
 })
 </script>

or

$(document).ready(
function () {
    $("#Button1").click(
        function () {
            var username, family, name, email, tel, codemeli, password;
            username = $('#<%=TextBox1.ClientID%>').val();
            name = $('#<%=TextBox2.ClientID%>').val();
            family = $('#<%=TextBox3.ClientID%>').val();
            password = $('#<%=TextBox4.ClientID%>').val();
            email = $('#<%=TextBox5.ClientID%>').val();
            tel = $('#<%=TextBox6.ClientID%>').val();
            codemeli = $('#<%=TextBox7.ClientID%>').val();

            $.ajax(
            {
                type: "POST",
                url: "WebApplication20.aspx/executeinsert",
                data: '{"username" : '+username+', "name": '+name+', "family":     '+family+',
                "password": '+password+', "email": '+email+', "tel": '+tel+' , 
                "codemeli": '+codemeli+'}',
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: true,
                cache: false,
                success: function(msg) {
                    alert(msg);
                },
                error: function (x, e) {
                    alert("The call to the server side failed. " 
                          + x.responseText);
                }
            }
        );
    }
 )
})

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about ASP.NET