It is possible to do disabling and enabling button in Flask? I have POST request in Ajax and send data from JavaScript to Flask and from this data I create in response a marker with popup and there are two buttons one and two. When the user click one he is added to the database. In Flask I check if the user is in the database and when he is in the database this button is disable for him. This two buttons are also to submit the form and I don't know if I can do it because of them.
Code Flask:
@app.route('/mapaa',methods=['GET','POST'])
def mapa():
if request.method == "POST":
if 'join-form' in request.form:
//check if is in the database
elif 'remove-form' in request.form:
//check if he is signed to the event
else:
name_ev = request.form.get('nameevent')
all_data = Event.query.filter_by(name=name_ev).all()
for row in all_data:
date_st_string = str(row.date_start)
date_end_string = str(row.date_end)
dictionary = {'id':row.id,'date_st':date_st_string,'date_end':date_end_string,'type':row.type,'name':row.name,'len_route':row.len_route,'route':row.route}
return jsonify(slownik)
// data to create marker
JavaScript code:
$("#search-button_event").one('click', function(e) { // make ajax request on btn click
e.preventDefault();
$.ajax({
type: "POST",
url: "/mapaa", // url to the function
data: {
nameevent: $("#name_of_event").val(), // value of the form
},
success: function(response) {
id = (response['id']);
data_end = (response['date_end']);
data_st = (response['date_st']);
len_route = (response['len_route']);
name = (response['name']);
let marker_event = L.marker(array[0]).bindPopup()
marker_event._popup.setContent(
'<form method="POST" action="/mapaa"' +
'<p>Name: ' + nazwa + '</p>' +
'<input name="nameOfEvent" type="hidden" value="' + nazwa + '" id="nameOF">' +
'<input id="ev_n" name="idOfEvent" type="hidden" value="' + id + '" id="idOF">' +
'<p>Date start: ' + data_st + '</p>' +
'<p>Date end: ' + data_end + '</p>' +
'<p>Typ: ' + typ + '</p>' +
'<p>Len route: ' + len_drogi + '</p>' +
'<button type="submit" id="one" name="join-form" class="btn btn-warning btn-block">Join to event</button>' +
'<br>' +
'<br>' +
'<button type="submit" id="two" name="remove-form" class="btn btn-danger btn-block">Sign out from the event</button>' +
'</form>')
marker_event.addTo(mymap)
polyline_event = L.polyline(array, {
color: 'red'
})
marker_event.on('click', function() {
polyline_event.addTo(mymap)
})
marker_event.getPopup().on('remove', function() {
polyline_event.remove()
})
mymap.setView(array[0], 14)
document.getElementById("name_of_event").value = "";
},
});
});
I want to do that if the user is in the database he will be have disable button to join.