1

I need help on how to refresh data when I click a button. What's it's doing is duplicating every time I click the button. Please help.

Here's my jquery code

$(document).ready(function() {
$('#button1').click(function(event) {
    $.ajax({
        url : 'http://localhost/api/eric_api.php?q=series',
        type : 'GET',
        dataType : 'json',
        success : function(data) {
            var table = $("#list");
            $.each(data, function(idx, elem) {
                table.append("<tr><td>" + elem.user + "</td></tr>");
            });

        },
        error : function() {
            alert('There was an error');
        }
    });
});
 });

And my html code is

<?php include 'eric_api.php'; ?>

<!DOCTYPE html>


<html>
<head>
    <title></title>
    <script src="js/jquery.js"></script>
    <script src="js/api_calls.js"></script>
    <link rel="stylesheet" href="css/normalizer.css" />
    <link rel="stylesheet" href="style.css" />
</head>

<body>
    <div>
        <table id="list"></table>
    </div>
    <div>
        <button id="button1">Get Data</button>
    </div>
</body>

I'm getting the right data returned I just want to refresh data and duplicated values cascading down my page. Thanks.

3 Answers 3

1

Try this..You should clear the old data to append new data

 $('#button1').click(function(event) {
    $("#list").empty();//Clear old data before ajax
    $.ajax({
        url : 'http://localhost/api/eric_api.php?q=series',
        type : 'GET',
        dataType : 'json',
        success : function(data) {
            var table = $("#list");
            $.each(data, function(idx, elem) {
                table.append("<tr><td>" + elem.user + "</td></tr>");
            });

        },
        error : function() {
            alert('There was an error');
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for you reply it still isn't returning all the values that it should. It only returns one row when it should be two.
whenever you click button it will call and clear the old data and fetch new data from db
1

it's just a guess, but ofcourse you have to remove the old entrys before like :

       table.empty();
       $.each(data, function(idx, elem) {
            table.append("<tr><td>" + elem.user + "</td></tr>");
        });

3 Comments

Thanks for the quick reply I tried that but it's not returning all my values from database.
Sridhar and demouser are both correct. Thanks I can't put checks on both so I'll go with first reply. Thank you both.
sorry it's wrong to empty the table inside the loop, i updated answer
0

Try this

 success : function(data) {
    var table = $("#list");
    table.html(''); // empty table on success
    $.each(data, function(idx, elem) {
       table.append("<tr><td>" + elem.user + "</td></tr>");
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.