Form sending GET request to Flask when POST is expected - html

I have a form in my Flask template.
<form id="answer_form" action=http://127.0.0.1:5000/answer_doubt method="POST">
Answer:<textarea rows='10' cols="100" form="answer_form" wrap="soft" name="answer"></textarea>
<input type="hidden" value="{{ entry.content.id }}" name="id">
<button type="submit">Submit</button>
</form>
The method of the form is clearly declared POST but it is sending GET.
Therefore, I am unable to use request.form.
What should I do to solve this error. Am I making, like, a really obvious mistake?
Thanks in advance!

Remove the full url from your action.
<form id="answer_form" action="/answer_doubt" method="POST">
Answer:<textarea rows='10' cols="100" form="answer_form" wrap="soft" name="answer"></textarea>
<input type="hidden" value="{{ entry.content.id }}" name="id">
<button type="submit">Submit</button>
</form>

Related

What does <form action = ""> mean ? Many videos I saw assign the action attribute an empty string and I have no idea what it means

<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input class="button" type="submit" value="Submit">
</form>
What will happen after I click the Submit button?
Syntax
<form action="URL">
the URL shows Where to send the form-data when the form is submitted.
Example
On submit, send the form-data to a file named "process_input.php" (to process the input):
<form action="/process_input.php" method="get">
<input type="text" id="name" name="name">
</form>
read more: https://www.w3schools.com/tags/att_form_action.asp

Django how to give a user input for writing email text

I have a template for sending custom emails:
The template is as below:
<p> Type here your email: </p>
<input id="your_name" type="text" name="your_name" value="{{ mail_content }}">
{% csrf_token %}
<input type="submit" value="Send the Request " name="_add_peer_confirm">
<input type="submit" value="Go back to Standard Email " name="_">
<input type=button value="Go back" onClick="javascript:history.go(-1);">
</div>
How can I make it tidy and gives the user a longer/elongated input box where he can write his mail?
any idea?
If you are looking for a larger input box, then you could use textarea
<textarea id="your_name" type="text" name="your_name">{{ mail_content }}</textarea>

form post action going to wrong URL

I have another personal website I want to post my form to, see code
<form name="requestPrint" action="www.mysite1.com/set_var.cgi"
method="POST">
<input name="value" type="hidden" size="8" value="1">
<input type="hidden" name="page" value="mypage.html"/>
<input type="hidden" name="index" value="94"/>
<input type="submit" value ="Print">
</form>
However when the form posts it goes to www.mysite2.com/www.mysite1.com/set_var.cgi
Is there a way to point it to just www.mysite1.com/set_var.cgi?
You should try the following:
action="http://www.mysite1.com/set_var.cgi"

How to Submit Multiple Values in a single HTML Form?

So I have a HTML form:
<html>
<body>
<script>history.pushState('', '', '/')</script>
<form action="http://myserver.com" method="POST">
<input type="hidden" name="Id" value="83" />
<input type="hidden" name="url" value="http://example.com/" />
<input type="submit" value="Submit request" />
</form>
<script>
document.forms[0].submit();
</script>
</body>
</html>
As you can see this is submitting the action for <input type="hidden" name="Id" value="83" /> meaning it's submitted for the attribute associated with ID number 83, I'm wanting the action to be submitted for multiple ID values, i.e. 1 - 100. Is this possible? If so how can it be done?
I assume you want to do something like this
<html>
<body>
<script>history.pushState('', '', '/')</script>
<form action="http://myserver.com" method="POST">
<input type="hidden" name="Id[]" value="83" />
<input type="hidden" name="Id[]" value="85" />
<!-- you can add as many as input here for id if you want -->
<input type="hidden" name="url" value="http://example.com/" />
<input type="submit" value="Submit request" />
</form>
<script>
document.forms[0].submit();
</script>
</body>
</html>
After this form is posted, on the server side you can get $_POST['id'] as an array and playing around with it.
Add [] to input name:
<input type="hidden" name="ID[1]" value="83" />
<input type="hidden" name="ID[100]" value="100" />
then the in php
print_r($_POST['ID']); //print out the data array
Or use just one input with comma separated values?
<input type="hidden" name=Id value="1, 2, 3,.., 100" />
PHP:
$ids = explode(" ", $_POST['ID']);
Old post, but like to add a Flask solution.
Flask can handle 'name' tags inside the form fields.
<div class="modal-body">
<p>Do you want to add {{ jar.jar_name }} Jar to Map</p>
<div class="col">
<form action="{{ url_for('hornet._jar_on_map')}}">
<input type="hidden" value="{{jar.jar_name}}" name="jar_name"/>
<select class="form-select form-select-lg mb-3" aria-label=".form-select-lg" name="map_name">
{% for map in maps %}
<option value='{{map.map_name}}'>{{ map.map_name }}</option>
{% endfor %}
</select>
<button type="submit" class="btn btn-secondary">Yes</button>
</form>
</div>
The submit button returns to a Flask route '_jar_on_map'. This calls a function to add an item on a map. It is the request.args that shall read your name tags of the different values. Then you can handle those tags inside your function. I made a dict again.
#hornet_bp.route("/add_jar_on_map", methods=["POST", "GET"])
def _jar_on_map():
returneddata = {}
print(request.args)
returneddata["jar_name"] = request.args.get('jar_name')
returneddata["map_name"] = request.args.get('map_name')
print(f"-------------------{returneddata['jar_name']}")
print(f"-------------------{returneddata['map_name']}")
jar = Hornet.find_one_by_name(jar_name=returneddata["jar_name"])
if jar:
update = Hornet.bind_to_map(bind_jar_to_map=returneddata)
if update:
flash(f"Adding Map {returneddata['map_name']} for item {returneddata['jar_name']} OK")
else:
flash(f"Adding Map {returneddata['map_name']} for item {returneddata['jar_name']} FAILED - Does it exist?")
return redirect(url_for(".table_jars"))
By doing document.forms[0].submit(); you are submitting all the input values in that form and values will be saved as Id=83&url=http://example.com/
If you want to submit several forms then you could use a for loop
x = document.forms.length //your desired number or number of forms
for(i = 0; i<x; i++){
document.forms[i].submit();
}

Passing multiple values in JSP url

I am trying to pass multiple values in a JSP page url to another one.
The code is:
<form name="QuantityForm" onsubmit="return quantityValidation()" action='<%="basket.jsp?addItem="+product.PID%>' method="post">
Quantity <input type="text" name="quantity" size="5">
<input class="button button2" type="submit" value="Add to basket" />
</form>
How do i pass the value of quantity(the field of name="quantity") in the same URL? i know it's something like "Search.jsp?item=<%=search%>&item2=value2&item3=value3.." but i can't seem to structure it properly. Thanks
Do not put your ? and parameters in the action url. Put your parameters in input tags:
<form name="QuantityForm" onsubmit="return quantityValidation()" action='basket.jsp' method="post">
<input type="hidden" name="addItem" value="%product.PID%">
Quantity <input type="text" name="quantity" size="5">
<input class="button button2" type="submit" value="Add to basket" />
</form>
you're using method="post" which doesn't allow parameters to be passed into the url. change it to method="get" if you want the parameters to be passed along with the url.