I'm making a flask application. The user submits a form, which is sent to flask. Something is done, then I want to return the results by appending <li> in a <ul>.
@app.route('/')
def index():
return render_template("index.html")
@app.route('/', methods=['POST'])
def search_form():
print(request.form)
x = request.form['id']
a = Vbulletin(x)
def result_gen():
return a.reg_ver()
result_gen()
temp = []
for s in result_gen():
text = s
print(text)
temp.append(text)
print(temp)
return render_template("index.html", temp=temp)
This isn't the complete jquery that appends items from the list. But when i define the variable thats the unordered list it changes my post request.
$(document).ready(function(){
$("#submit").click(function(e){
e.preventDefault();
$.ajax({type: "POST",
url: "/",
data: ({ id: $("#searchinput").val()},
const resultList = document.getElementById("resultList")
//let results = {{ temp|tojson}};
success:function(){
alert("POST was sent.");
}
})
});
});
Without defined 'resultlist', it sends the data in my post request as this:
ImmutableMultiDict([('id', 'monkey')])
But when I define 'resultlist' it sends the data as this:
ImmutableMultiDict([('searchinput', 'monkey')])
So instead of sending "id" its the id of the input on the form. How can I fix this? I'll also include the html.
<!DOCTYPE html>
<html>
<head>
<title>UserFind Home</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}">
<script type="text/javascript" src="{{ url_for('static', filename='forumsearch.js') }}"></script>
</head>
<body>
<nav>
<ul id="navlist">
<h1>Userfind</h1>
<li><a class="btn" href="#home">Home</a></li>
<li><a class="btn" href="#contact">Contact</a></li>
<li><a class="btn" href="#about">About</a></li>
<form method="POST" name="searchbar">
<ul id="navsearch">
<li class="search">
<input type="text" id="searchinput" name="searchinput" placeholder="Search for User here. Must be atleast 5 characters long.">
</li>
<li><button type="submit" id="submit" class="btn-default">Submit</button></li>
</ul>
</form>
</ul>
</nav>
<ul id="resultList"></ul>
<div class="footer">
<p>©2019 Userfind</p>
</div>
</body>
</html>
const resultList = document.getElementById("resultList")would result in javascript not parsing with a syntax error, and therefore that ajax would never run because it wouldn't actually be there - Jaromanda X