How to pass data to JS from a submit button in a form using Sightly
I have a simple form with a submit button and previously I was using JSP page to write a script tag and execute a JS function. How to convert this to a Sightly page and execute this script? I need to pass data only on click of the submit button and also retrieve data.
Below is the code,
<script>
$(document).ready(function() {
$('body').hide().fadeIn(5000);
$('#submit').click(function() {
var failure = function(err) {
alert("Unable to retrive data "+err);
};
//Get the user-defined values to persist in the database
var myFirst= $('#first').val() ;
var myLast= $('#last').val() ;
var myDescription= $('#description').val() ;
var myAddress= $('#address').val() ;
var url = location.pathname.replace(".html", "/_jcr_content.persist.json") + "?first="+ myFirst +"&last="+myLast +"&desc="+myDescription +"&phone="+myAddress;
$.ajax(url, {
dataType: "text",
success: function(rawData, status, xhr) {
var data;
try {
data = $.parseJSON(rawData);
//Set the fields in the forum
$('#custId').val(data.pk);
} catch(err) {
failure(err);
}
},
error: function(xhr, status, err) {
failure(err);
}
});
});
}); // end ready
</script>