I have a simply HTML form like so:
<form id="new_order" method="post" action="{{ url_for('summary') }}">
<div class="form-group col-sm" id="fruit">
<input type="number" class="form-control" name="fruit_quantity" id="fruit_quantity" required>fruit_quantity<br>
<input type="text" class="form-control" name="fruit_name" id="fruit_name">fruit_name<br>
<button type="submit" class="btn btn-primary" form="new_order" id="new_order_summary">Summarise</button>
( ... ) //more entries possible
</form>
I use Python Flask to retrieve the form data and do stuff with it:
@app.route('/summary', methods=['GET', 'POST'])
def summary():
summary = []
fruit = request.form.get('fruit_name')
summary.append({'fruit_name': fruit})
quantity = request.form.get('fruit_quantity')
summary.append({'fruit_quantity': fruit_quantity})
summary = json.dumps(summary)
return render_template('fruit_summary.html'
, summary= summary
)
The idea was to open a second HTML page to display the summarised fruits using the jQuery plug-in DataTables.
// 2nd HTML file
<div>
<table id="summary_table" class="responsive display">
</table>
</div>
//JS file
$('#new_order').submit( function (data) {
// check that the new page loaded
$('#summary_table').ready( function () {
// this won't show anything, unfortunately..
console.log(data);
$('#summary_table').DataTables({
data: data
, columns: [
{data: 'fruit_name', title: 'fruit_name'},
{data: 'fruit_quantity', title: 'fruit_quantity'}
]
})
})
});
When I now show the variable 'summary' in the HTML file Flask-style ({{ summary }}
), it shows that the data is indeed sent over to the HTML file correctly.
My problem is that I need that information in the JS file, not the HTML file, to make it render nicely using DataTables.
I've tried to get the data with another AJAX call, but that would simply return the dict from Flask without any form data. I believe that I don't really understand how the data flows between files. It works in other examples where I use preventDefault()
on submit, but it should be a separate /summary
HTML file..
The following posts seem related but didn't help me: