I'm looking to improve my jQuery code.
The piece of code is submitting data to another PHP page for processing.
At the moment I'm taking data submitted from a form, and taking data from a custom data attribute from the page.
Here's the code
// Add new comment to nit on comment.php page
$('#new_comment_form').bind('submit', function(e) {
// Prevent default
e.preventDefault();
var commentNitId = $('#left_content').attr('data-nit-id');
var commentData = 'nit_id=' + commentNitId + '&new_comment_text=' + $('textarea.new_comment').val();
$.ajax({
type: "POST",
url: "ajax/addComment.php",
data: commentData,
dataType: "json",
success: function(comment_response) {
// Code upon success
},
error: function() {
// Error
alert("Error adding comment");
}
});
});
I'm just wondering if there is a better ("neater") way to serialize the data ready to submit to the form?
Kind Regards,
Luke