Click here to Skip to main content
15,887,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am at learning phase of jquery ajax and i want to insert values to database but it is sending null values to database. please guide me.
thanks

What I have tried:

here is my code.
HTML
<br>
    <hr>
    <div class="form-group">
        <br>
        <br>
        
        <br>
        <br>
        
        <br>
        <br>
        
        <br>
        <br>
    </div>

JavaScript
$(document).ready(function () {
            $("#insertForm").validate({
                rules: {
                    'textt': {
                        required: true,
                        minlength: 5,
                        maxlength: 8

                    },
                    'text1': {
                        required: true,
                        minlength: 5,
                        maxlength: 8
                    },
                    'text2': {
                        required: true,
                        email: true
                    },
                    'text3': {
                        required: true,
                        minlength: 5,
                        maxlength: 8
                    }

                },
                messages: {
                    'textt': {
                        required: "Enter the Name",
                        minlength: "Minimum Length 5",
                        maxlength: "Maximum Length 8"

                    },
                    'text1': {
                        required: "Enter Username",
                        minlength: "Minimum Length 5",
                        maxlength: "Maximum Length 8"
                    },
                    'text2': {
                        required: "Valid Email Required",
                        email: "Please Enter valid email abc@gmail.com"
                    },
                    'text3': {
                        required: "Passoword is required",
                        minlength: "Minimum Length of Password 5",
                        maxlength: "Maximum Length of Password 8"
                    }
                },
                submitHandler: function (form) {
                    form.submit();
                }
            });

            $("#btn1").click(function () {
                var isvalid = $("#insertForm").valid();

                if (isvalid) {
               
                    Employee_Ajax();

                }
                else {
                    bootbox.alert("Data is not entered");
                }


            });


            function Employee_Ajax() {
                var employee ={
                    textt: $("txt1").val(),
                    text1: $("txt2").val(),
                    text2: $("txt3").val(),
                    text3: $("txt4").val()
                }
                $.ajax({
                  
                    url: "@Url.Action("EmployeesData", "Home")",
                    data: JSON.stringify("employees", employee),
                    type: "POST",
                    contentType: "application/json;charset=utf-8",
                    dataType: "JSON",
                    success: function (data) {
                        if (data != "") {
                            bootbox.alert("Data Saved Successfully!!!!");
                            
                        }
                        else {
                            bootbox.alert("data not saved! ERROR");
                        }
                    },

                    error: function (fail) {
                        console.log(fail);
                    }

                });


            }
        });
Posted
Updated 3-Jan-18 18:32pm
v2
Comments
Vincent Maverick Durano 4-Jan-18 0:49am    
Could you please post the relevant HTML markup so we could see where your text input fields are located. I'm guessing that this line below is null:

$("txt1").val()
Member 13605985 4-Jan-18 1:01am    
yes i have changed this $("txt1").val(); TO $("#txt1").val();
but still getting null values ...
Karthik_Mahalingam 4-Jan-18 0:59am    
post the controller action code.
Member 13605985 4-Jan-18 1:08am    
[HttpPost]
public JsonResult EmployeesData(Models.Employees employees)
{
var response = Json(new {resp=employees.create() },JsonRequestBehavior.AllowGet);
return response;
}
Karthik_Mahalingam 4-Jan-18 1:55am    
Employees class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900