vendredi 16 mars 2018

Jquery Flask capturing checkbox value

I am trying to capture the data from checkbox by assigning value in jquery and passing it to backend each time search is run by the user. It is not working for me, tried several ways.

Use case: As a user I would like to search and be able to check for various search options.

template:

$('a#process_input').bind('click', function () {
                $.getJSON('/background_process', {
                    checkbox1: $('input[name="checkbox1"]').val(),
                    input_search: $('input[name="input_search"]').val(),
                },
                    function (data) {
                    ul.empty();
                    $.each(data.result, function (index, element) {
...
... <some irrelevant code...>

        <input type=text size=40 name=input_search>
        <a href=# id=process_input>
            <button class='btn btn-default'>Submit</button>
        </a>
    </form>
    <form method="POST" action="/">
        <p>Please select allergy options below:</p>
        <input type="checkbox"  name="checkbox1"/>Allergy 1

backend

    @app.route('/background_process', methods=['GET', 'POST'])

    def background_process():
try:
    checkbox1 = request.args.get("something")

    search_word = request.args.get('input_search', 0, type=str)

    if search_word:

        return jsonify(result=yummly_search(search_word, checkbox1))
    else:
        return jsonify(result='Try again.')
except Exception as e:
    return str(e)




Aucun commentaire:

Enregistrer un commentaire