This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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