Home > Mobile >  The python and sqlite is returning the right data but HTML is not giving it right
The python and sqlite is returning the right data but HTML is not giving it right

Time:12-23

Taking something from a database if the method is get it just returns everything but if its post it returns the searched thing, when its get it works fine but when its post it gives the right value but html is not "deciphering" it?

HTML

{% for account in accounts %}
                <tr>
                    <th scope="col">{{ account["email"] }}</th>
                    <th scope="col">{{ account["username"] }}</th>
                    <th scope="col">{{ account["password"] }}</th>
                    <th scope="col">{{ account["website"] }}</th>
                    <th></th>
                </tr>
            {% endfor %}

Ajax

$(document).ready(function () {
        $("#search").on("input", function(e) {
            $(".table-body").empty();

            $.ajax ({
                method: "GET",
                url: "/manager",
                data: {search:$("#search").val()},
            })
        })
    })

PYTHON

@app.route("/manager", methods=["GET", "POST"])
def manager():
    loggedUser = session["loggedUser"][0]["username"]
    if request.method == "GET":
        accounts = db.execute("SELECT * FROM manager WHERE loggedUser = ?", loggedUser)
    else:
        form = request.form.get("search")
        accounts = db.execute("SELECT * FROM manager WHERE loggedUser = ? AND website LIKE ?", loggedUser, form   "%")

    print(accounts)
    return render_template("manager.html", accounts=accounts)

It gives the right results but it doesnt give it in the html unless its the get method

Output when POST

[{'id': 1, 'loggedUser': 'Hyde', 'username': None, 'email': None, 'password': '123', 'website': 'HydPass'}]

Structure

CREATE TABLE manager (id INTEGER, loggedUser TEXT NOT NULL, username, email, password TEXT NOT NULL, website TEXT NOT NULL, PRIMARY KEY (id));

CodePudding user response:

Turns out I had to update it through AJAX

Ajax

    $(document).ready(function () {
        $("#search").on("input", function(e) {
            $(".table-body").empty();

            $.ajax ({
                method: "POST",
                url: "/manager",
                data: {search:$("#search").val()},
                success: function(data) {
                    // Clear the table
                    $(".table-body").empty();

                    // Iterate over the returned data and add a row for each account
                    for (var i = 0; i < data.length; i  ) {
                        var account = data[i];
                        var row = '<tr><th scope="col">'   account.email   '</th><th scope="col">'   account.username   '</th><th scope="col">'   account.password   '</th><th scope="col">'   account.website   '</th><th><input type="button"  value="Edit Account" name="edit"><input type="button"  value="Remove Account" name="remove"></th></tr>';
                        console.log(row)
                        $(".table-body").append(row);
                    }
                }
            })
        })
    })
  • Related