Skip to content

Instantly share code, notes, and snippets.

@zainafzal88
Created October 7, 2020 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zainafzal88/ec569124b369b9331811dddd9712473a to your computer and use it in GitHub Desktop.
Save zainafzal88/ec569124b369b9331811dddd9712473a to your computer and use it in GitHub Desktop.
<script>
$(function(){
let allComments = document.getElementById("all-comments");
let $name = $("#name");
let $comment = $("#comment");
$("#submit").on('click', function(){
//validation to prevent html tags being inputted input box
var reg =/<(.|\n)*?>/g;
if (reg.test($('#comment').val()) == true) {
var errorText ='HTML Tags Not Allowed';
alert(errorText);
}
else
{
var item =
{
username: $name.val(),
comment: $comment.val(),
postId: window.location.pathname,
};
$.ajax({
type: "POST",
url: "[your-api-url-here]]",
data: JSON.stringify(item),
contentType: "application/json",
success : function(){
alert("Comment submitted successfully")
location.reload();
},
error : function(err){
alert("error in post" + JSON.stringify(err))
}
});
}
$name.val("");
$comment.val("");
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment