Reputation: 103
I am trying to get the data that I have stored inside of a script tag in my html file to my python file so that I can use it in a python function, does anyone have any idea how I could achieve this? Here's my html code:
<textarea id="rendezook" class="rendezook"></textarea>
<button onclick="get_text();">Valider</button>
<script>
function get_text() {
var x = document.getElementById("rendezook")
location.href = "/validate_comment?rendezook=" + encodeURIComponent(x.value);
console.log(x.value)
}
</script>
And then this is my python file:
@app.route('/validate_comment')
def validate_comment(item_id=None):
print("test")
print(request.args.get("rendezook"))
print("test2")
return redirect(url_for('msr_edit', item_id=72))
test and test2 are not getting printed so it is not entering the function.
Upvotes: 0
Views: 87
Reputation: 168863
The easiest way is to just use a form that's submitted via a GET request:
<form action="/validate_comment">
<textarea name="rendezook" class="rendezook"></textarea>
<button type="submit">Valider</button>
</form>
This will result in an URL like /validate_comment?rendezook=some_value_here
, so you can access it via request.args
(as it looks like you're using Flask):
@app.route('/validate_comment')
def validate_comment():
print("yeet", request.args.get("rendezook"))
return redirect(url_for('msr_edit', item_id=72))
Based on comments, if you're unable to use a form for some reason, then:
<textarea id="rendezook" class="rendezook"></textarea>
<button onclick="test()">Valider</button>
<script>
function test() {
var x = document.getElementById("rendezook");
location.href = "/validate_comment?rendezook=" + encodeURIComponent(x.value);
}
</script>
Here's a minimal example that provably works :)
from flask import request, Flask
app = Flask(__name__)
@app.route("/validate_comment")
def validate_comment(item_id=None):
x = request.args.get("rendezook")
return f"you said {x}"
@app.route("/")
def index():
return """
<textarea id="rendezook" class="rendezook"></textarea>
<button onclick="get_text();">Valider</button>
<script>
function get_text() {
var x = document.getElementById("rendezook")
location.href = "/validate_comment?rendezook=" + encodeURIComponent(x.value);
console.log(x.value)
}
</script>
"""
if __name__ == "__main__":
app.run()
Upvotes: 1