我的按钮只能单击一次。我希望能够重复许多搜索

时间:2019-08-29 02:35:04

标签: javascript html flask

我的问题与大多数人相反。我希望能够多次搜索用户。例如,如果我查找Ray,则会检索ray的数据。然后,如果我查找bob,我想检索bob的数据。

但是,我的代码可以从第一次搜索中检索数据。然后出现在您单击的按钮中,但是什么也没有发生。

我没有做太多尝试,因为我不知道从哪里开始。

<div align="center">

</div>
    <!--
    <div class = "box" style = "width:100%; text-align:center;">

        <form method = "POST">
            <input style = "float:left;"type="search" class="form-control" type="text" name = "search" placeholder="Enter First and Last name to begin" aria-label="Search" > </input>
            <button style = "float:right;"type="submit" class="btn btn-success" id ="search" onclick="fetchlist();return false;">Search</button>
        </form>
    --> 

        <div style="text-align: center;">
            {% from "_formhelpers.html" import render_field %}
        <form method="POST"  action="/">
            <dl style="display: inline-block; text:white;" >{{render_field(form.search)}}           </dl>
            <button id="searchbutton" type="submit" style="display: inline-block;"  class="btn btn-outline-success my-2 my-sm-0" onclick="fetchlist(); return false;">Search</button>   
            <br>
            {% if error %}
            <p class = "error"><strong>Error:</strong>{{error}}</p>
        {% endif %}
        </form>


    </div>

{% if data %}
    <div  style="text-align:center;">   
        <table id="list" style="display:none;" class = "table table-hover" >
            <thead>
                <th scope="col">First</th>
                <th scope="col">Last</th>
                <th scope="col">Rating</th>
                <th scope="col">Review</th>
             </thead>
             <tbody>
                {% for row in data %}
                    <tr>
                        {% for d in row %}
                            <td>{{ d }}</td>
                        {% endfor %}
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
{% endif %}
</body>

<script>
function fetchlist() {
    if (document.getElementById('searchbutton').onclick) {
        document.getElementById('list').style.display = 'inline';
    }
    else document.getElementById('list').style.display = 'inline';

}   
</script>
{% endblock %}
@app.route('/', methods=['GET', 'POST'])
def homepage():
    try:
        form=SearchForm(request.form)
        global d1
        d1 =""
        if request.method == "POST":
            s1 = form.search.data
            a = s1.split(" ",1)
            firstname, lastname = (a[0], a[1])
            c,conn = connection()
            qry = "SELECT FirstName, LastName FROM posts WHERE FirstName LIKE (%s) AND LastName like (%s)"
            c.execute(qry, ((thwart(firstname)), (thwart(lastname))))
            d1 = c.fetchall() 
            c.close()
            conn.close()
            print(d1)
        else: print('error')
        return render_template("index.html", data=d1, form = form)
    except Exception as e:
        return(str(e))  

无错误消息。我希望执行多次搜索。

0 个答案:

没有答案