<form name="input" action="/test/compare method="get">
<input type="checkbox" name="id" value="val1">val1<br/>
<input type="checkbox" name="id" value="val2">val2<br/>
<input type="submit" value="Submit">
</form>
I want a query string that looks like this:
/test/compare?id=val1,val2
Is this possible?
Not exactly as comma separated values, but you can get an array of values of all selected checkboxes as follows.
<form name="input" action="/test/compare" method="get">
<input type="checkbox" name="id[]" value="val1">val1<br/>
<input type="checkbox" name="id[]" value="val2">val2<br/>
<input type="submit" value="Submit">
</form>
PHP
// here you'l get array of id in $_GET and process according to your need
$ids=$_GET['id'];
$str=implode(",",$ids); //array to comma separated values
echo $str; //output will be checked checkbox values--> val1,val2
EDIT:
ANOTHER SOLUTION USING JAVASCRIPT
<form name="input" action="/test/compare" method="get">
<input type="checkbox" name="id" value="val1">val1<br/>
<input type="checkbox" name="id" value="val2">val2<br/>
<input type="submit" value="Submit">
</form>
<!-- load jQuery -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"> </script>
<script type="text/javascript">
$(document).ready(function(){
$("form[name=input]").submit(function(){ //bind onSubmit handler on page ready
//get all field values
var query="id="; //write here parameter name
$("form[name=input] [name=id]:checked").each(function(i){ //it will check for all elements inside form with name "id"
query+=$(this).val()+",";
});
query=query.slice(0,-1); //remove last extra comma
//redirect url by passing "query" as GET parameter
window.location=$(this).attr("action")+"?"+query;
return false;
});
});
</script>
You can make name="id[]" to make the form parameter an array, but aside from that I don't believe it's possible to do what you are asking. You should have unique values as form fields unless they are an array.
Related
Making a little search engine. The idea is to take input from the user and then based on that, make a redirection to the search page.
The following code:
<form action ="/search.html">
<label for="form-search"></label>
<input type="text" id="form-search" placeholder="TYPE HERE!"><hr>
<input type="submit" name="query" value="Search!">
</form>
Always redirects to the following page regardless of what the input user has given:
/search.html?query=++Search%21++
While (for the input "Suppose This Was Entered") it should go to:
/search.html?query=Suppose++This++Was++Entered
Any help will be appreciated.
The var name used in the query string of the url is the name attribute of the form fields so you need add a name atribute to your text field instead to the submit input.
<form action ="/search.html">
<label for="query"></label>
<input type="text" id="query" name="query" placeholder="TYPE HERE!"><hr>
<input type="submit" value="Search!">
</form>
The id and the name in the text field not necessary has to be the same
You can create a function that gets called when the form submits via the form's onsubmit attribute. From within the funciton you can manipulate your URL generation like below:
Note: return false; is to prevent submitting the form since the return value of the function is passed to the form's onsubmit.
function submitFunction() {
let searchText = document.getElementById("form-search").value.trim();
let form = document.getElementById('myForm');
if(searchText.length > 0) {
document.getElementById("s").value = searchText;
form.action = "/search.html";
form.submit();
} else {
return false;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Title of Your page</title>
</head>
<body>
<form id="myForm" method="get" onsubmit="return submitFunction();">
<label for="form-search"></label>
<input type="text" id="form-search" placeholder="TYPE HERE!" value="" >
<input type="hidden" id="s" name="query" value="" />
<hr>
<input type="submit" value="Search!">
</form>
</body>
</html>
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();
}
I want to pass the input field to the form action part covering field; so it looks like /{user}/some_integer_in_field if you know what I mean.
<form action="<c:url value='${user}/?field'/>"
method="post" >
<input type="hidden" name="_method" value="DELETE">
<input type="text" name="field" id="field"/>
<input type="submit" value="DELETE">
</form>
can someone help? is this possible?
I see you've only tagged HTML. What other languages are you using? I recommend you change the desired action via JavaScript, based on the user variable.
HTML
<form>
<input type="hidden" name="_method" value="DELETE">
<input type="text" name="field" id="field"/>
<input type="submit" value="DELETE" id="submitBtn">
</form>
JavaScript
<script>
var sub = document.getElementById('submitBtn');
// When sub is clicked, compare user var
sub.addEventListener('click', function(event){
if(user == 1){
// Perform post action 1
}else if(user == 2){
// Perform post action 2
}
// etc...
});
</script>
You'll have to change the user variable as the field changes as well.
how can I replace this code, by only one Form please.
<form method="post" action="<c:url value="/deconnexion" />"><input type="submit" value="Déconnexion" class="deco" /></form>
<form method="post" action="<c:url value="/accueil" />"><input type="submit" value="Retour" class="deco" /></form>
What you want to do is impossible, each form will submit it's information to a single location, which is your action property in your form.
What you can do, alternatively, is manage two different cases in the same form. Your form would look like that :
<form method="post" action="<c:url value="/redirection" />">
<input id="deco" type="submit" value="Déconnexion" class="deco" />
<input id="retour" type="submit" value="Retour" class="deco" />
<input id="type" name="type" type="hidden" value="" />
</form>
You would need Javascript (Jquery) code to assign the type of redirection in the hidden field before the form is submitted :
<script type="text/javascript">
$('#deco').click(function(event)
{
$('#type').val("Déconnexion");
});
$('#retour').click(function(event)
{
$('#type').val("Retour");
});
</script>
Then, in the PHP, you would redirect to the page or function you wish with this condition :
if ($_POST["type"] == "Déconnexion")
// Do something
else if ($_POST["type"] == "Retour"
// Do something else
I have a form that can be called from any page. It opens on top of the current page with z-index. I cannot use php in this case. The form is send to a cgi to process the values. It is necessary to put something in "redirect" if I leave it blank it don't works. In redirect is there a way to put the value to the current page whatever it is?
<Form id="formulari" method="POST" action="http://cgi.domain.com/FormMail.pl">
<p>
<input type="hidden" name="recipient" value="info#domain">
<input type="hidden" name="subject" value="IB4 correu">
<input type="hidden" name="redirect" value="TheSamePageWeAre">
</p>
<form>
<input type="text" name="name" value="name"/>
<input type="submit" value="Send"/>
</form>
Put this javascript at the end of the page.
<script type='text/javascript'>
document.getElementsByName('redirect')[0].value = window.location.pathname;
</script>
Set an id on the hidden field, for example id="redirect", and use this Javascript:
<script>
window.onload = function (){
document.getElementById('redirect').value = location
}
</script>