I'm trying to use ajax jquery to stop my page from reloading whenever the button is pressed, but the page is still reloading after I implemented it.
This is just a test system for "liking" a post.
//foreach loop that echos out row
foreach($dbData as $post){
<p id="likecount" class = "likecount"> </p>
<p>
<form method="post" id="liketest" class="redirect" >
<button type="submit" id="like" name="like" value= "'.$post["id"].'">
<i class="fa fa-heart"></i>
</button>
</form>
</p>
}
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#like').focus();
$('#like').keypress(function(event) {
var key = (event.keyCode ? event.keyCode : event.which);
if (key == 13) {
var info = $('#like').val();
$.ajax({
method: "POST",
url: "index.php",
data: {like: info},
success: function(status) {
$('#likecount').append(status);
$('#like').val('');
}
});
};
});
});
</script>
<?php
if (isset($_POST['like'])) {
echo '<h1>'.$_POST['like'];
}
?>
I expect it to not reload the page but the actual output is the page still reloading once the button is clicked.