3

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.

  • 2
    Incidentally, it looks like your code might generate duplicate element IDs. Note that [IDs](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) "must be unique in the whole document". – showdev Aug 13 '19 at 16:13
  • 2
    Possible duplicate of [How to prevent page from reloading after form submit](https://stackoverflow.com/questions/45634088/how-to-prevent-page-from-reloading-after-form-submit-jquery). Also see [Prevent page from refreshing after an ajax call](https://stackoverflow.com/questions/40362821/prevent-page-from-refreshing-after-an-ajax-call) and [Stop form refreshing page on submit](https://stackoverflow.com/questions/19454310/stop-form-refreshing-page-on-submit). – showdev Aug 13 '19 at 16:16

4 Answers4

3

You can use:

event.preventDefault()

after

$('#like').keypress(function(event) {

to stop reloading the page.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Suraj Singh
  • 142
  • 4
  • The page still reloads :/ – user53267891023 Aug 13 '19 at 16:59
  • You can use class instead of id because id is unique for all the elements of DOM. and replace the line " – Suraj Singh Aug 14 '19 at 07:05
1

Please change your code like this.

<form method="post" id="liketest" class="redirect" onSubmit="return false;">

Then, you must have to send form data with ajax. It's the best way to send a post request without page reload.

1

try this

event.preventDefault();

after this

$('#like').keypress(function(event) {
Ahsan Abrar
  • 139
  • 3
  • 13
0

You cannot change form's default behaviour so just add onsubmit="return false":

<form onsubmit="return false"></form>

return false will prevent form submit

Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30