$(document).ready(function() { appendCountries(); $('#form_country').change(function() { appendStates(); }); $('#form_state').change(function() { appendCity(); }); $('.btn').click(function() { AddListItem(); }); }); function appendCountries() { $.ajax({ url: "../_vti_bin/ListData.svc/CountryStateCity()?$orderby= Title asc&$select=Title", type: "get", headers: { "Accept": "application/json;odata=verbose" }, success: function(data) { var values = []; var uniqueValues = []; var option = ""; var valuesArray = data.d.results; $.each(valuesArray, function(i, result) { values.push(result.Title); }); $.each(values, function(i, el) { if ($.inArray(el, uniqueValues) === -1) { uniqueValues.push(el); option += ""; } }); $("#form_country").append(option); }, error: function(data) { alert(data.responseJSON.error); } }); } function appendStates() { var country = $('#form_country').val(); $.ajax({ url: "../_vti_bin/ListData.svc/CountryStateCity()?$select=State&$filter=Title eq '" + country + "'&$expand=State&$orderby= State/Title asc", type: "get", headers: { "Accept": "application/json;odata=verbose" }, success: function(data) { var values = []; var uniqueValues = []; var option = ""; var valuesArray = data.d.results; $.each(valuesArray, function(i, result) { values.push(result.State.Title); }); $.each(values, function(i, el) { if ($.inArray(el, uniqueValues) === -1) { uniqueValues.push(el); option += ""; } }); $("#form_state").empty(); $("#form_state").append(option); }, error: function(data) { alert(data.responseJSON.error); } }); } function appendCity() { var state = $('#form_state').val(); $.ajax({ url: "../_vti_bin/ListData.svc/CountryStateCity()?$select=City&$filter=State/Title eq '" + state + "'&$expand=State,City&$orderby= City/Title asc", type: "get", headers: { "Accept": "application/json;odata=verbose" }, success: function(data) { var values = []; var uniqueValues = []; var option = ""; var valuesArray = data.d.results; $.each(valuesArray, function(i, result) { values.push(result.City.Title); }); $.each(values, function(i, el) { if ($.inArray(el, uniqueValues) === -1) { uniqueValues.push(el); option += ""; } }); $("#form_city").empty(); $("#form_city").append(option); }, error: function(data) { alert(data.responseJSON.error); } }); } function AddListItem() { var firstName = $("#form_name").val(); var lastName = $("#form_lastname").val(); var email = $("#form_email"); var phone = $('#form_phone'); var country = $('#form_country'); var state = $('#form_state'); var city = $('#form_city'); var address = $('#form_address'); $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "../_vti_bin/ListData.svc/EmployeeDetails()", type: "POST", data: JSON.stringify({ __metadata: { type: "SP.Data.TestListItem" }, FirstName: firstName, Lastname: lastName, Email: email, Phone: phone, Country: country, State: state, City: city, Address: address }), headers: { "Accept": "application/json;odata=verbose", "Content-Type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "X-HTTP-Method": "POST" }, success: function(data, status, xhr) { alert('Request has been submitted successfully'); }, error: function(xhr, status, error) { //console.log(data.responseJSON.error); alert('Error'); } }); }