How can I make an html button that passes a parameter? - html

I want a html button with parameter functionality.
new/?sorting=desc is the url that it should link to. But when I try, the parameter is not passed. How should it be done? I tried both the methods below but none worked.
<FORM METHOD="GET" ACTION="./?sorting=desc">
<INPUT TYPE="submit" VALUE="Äldst först">
</FORM>
I want buttons that behave like these links:
Äldst först
Nyast först

If you want something to act as a link, then you should use a link.
That said:
When you submit a GET form, the query string is removed from the action and a new one is generated from the data in the form.
You need to store the data in hidden inputs.
<form action="/social/tracking/new/">
<input type="hidden"
name="sorting"
value="desc">
<input type="submit"
value="Nyast först">
</form>
<form action="/social/tracking/new/">
<input type="hidden"
name="sorting"
value="asc">
<input type="submit"
value="Sort the other way">
</form>

If you are using jQuery, you can use the code below.
This will fill a hidden input with the correct value when you click on one of the submit buttons.
<form method="get" action="./">
<input type="hidden" name="sorting" id="sorting" value="" />
<input type="submit" value="Äldst först" id="sort_desc" />
<input type="submit" value="Nyast först" id="sort_asc" />
</form>
<script>
$('#sort_desc').click(function(){
$('#sorting').val('desc');
});
$('#sort_asc').click(function(){
$('#sorting').val('asc');
});
</script>

I think its not possible. A form is used to send data (mostly) via POST or GET. Your goal is to open a specific URL. I would create a standard and would style it like a button. Whats the reason you want to use a button?

Related

HTML 1 input text for different forms/methods

I am wanting to send POST/GET requests to one of my .jsps. I thought about using 2 buttons but only 1 input text as the action for the 2 buttons is actually the same and only the mehod differs. Right now I only have 1 <form> with the input text as well as the button.
<form action="systemDetail.jsp" method="GET">
<input type="text" id="get" name="paramName" /> <input type="submit"
value="Submit GET" />
</form>
Preferably I would now like to have a second button which uses POST method for the exact same call. I am fairly new to this subject all together.
My questions:
Is what I want even possible or should I use 2 input boxes as well as 2 buttons?
How can I realize it if its possible?
Please note that I do not want to use PHP, JavaScript or anything else. Only HTML and JSP/JSTL are okay
you can use this method for submitting in both methods
<form action="systemDetail.jsp" method="get">
<input type="text" id="get" name="paramName" />
<button type="submit">Submit</button>
<button type="submit" formmethod="post">Submit using POST</button>
</form>

Two forms on one page causes wrong validation

I have one page with two separate forms. Most input fields are required. When I click the submit button on the second form, it asks me to fill out the required fields in the first form.
How do I make sure that it only validates the form in which I clicked the submit button?
<form method="post" action="index.php" name="orderQuick" id="orderQuick">
<input type="text" name="street" id="street" required>
<button type="submit" name="submitBtn" id="submitBtn">Submit</button>
</form>
<form method="post" action="index.php" name="order" id="order">
<input type="text" name="street2" id="street2" required>
<button type="submit" name="submitBtn2" id="submitBtn2">Submit</button>
</form>
I think it is because you have not closed <button> tag so it is considering both input type in one form. Close <button> tag.I think it will solve your issue.
I think you will need to use the jqueryvalidation plugin: (http://jqueryvalidation.org/valid/)
Using this plugin, on the click of the respective submit buttons, you just need to call the method:
$("#orderQuick").valid();
and
$("#order").valid();
This should solve your issue.

How to build a custom URL from a HTML form?

I have a HTML form like:
<form name="input" action="" method="post">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
I need to build a URL when the submit link is clicked and send the user to it, using the username from the form in the correct position in the URL.
URL:
http://example.com/htmlchat/init.html?init_room=1&init_user=USERNAME_FROM_FORM_HERE
Can someone help (an example would be great)?
Don't use POST when you want the data to appear in the URL
Do put all the data you want in your URL in the form
Get the names of your fields right
Put the base URI in the action
such:
<form action="init.html">
<input type="hidden" name="init_room" value="1">
<label>
Username:
<input name="init_user">
</label>
<input type="submit" value="Submit">
</form>
This is possible with either Javascript or a server side language such as PHP. With Javascript you want to update the action attribute.
You could use jQuery to do this.

how to pass textbox value to next jsp page by clicking on a different form button without using javascript

here is my code
<body>
<input type=text name=name>
<form action=new3.jsp method=post name="f1">
<input type=text name=name>
<input type=submit value=next>
</form>
<form action=new1.jsp method=post>
<input type=submit value=back>
</form>
</body>
as you can see there are two forms here with two different submit buttons named "next" and "back". The text box is in the form where "next" button is. My question is here how to send the value of this text box to "new1.jsp" clicking "back button" without using javascript. Thanks in advance...
Don't post form to JSPs. Post the to servlets instead, and let the server analyze the parameters and forward to the appropriate JSP depending on which button has been clicked:
<form action="/controllerServlet" method="post" name="f1">
<input type="text" name="name"/>
<input type="submit" name="destination" value="next"/>
<input type="submit" name="destination" value="back"/>
</form>
Using the above form (which BTW, is valid HTML), the servlet can use the value of the "destination" parameter. If it's "next", it should forward to new3.jsp. If it's back, it should forward to "new1.jsp". And if, for example, you want to redisplay the same page because the user didn't enter a valid value in the text field, it can do so.
That's the well-known MVC pattern. You should use it.

Html input value

A simple html question i would like to make form that shows a different value to the one that it posts.
<form action="" method="post">
<input type="submit" name="action" value="Buy"/>
</form>
for example shows a button with Buy written on it and Buy is also posted. I would like it to show Buy, but I would like it submit a differnet value.
In theory you can:
<button type="submit" name="action" value="a differnet value">Buy</button>
In practise, we have to operate on a WWW that includes Internet Explorer.
You would probably be better off encoding the data into the name attribute and checking that.
probably the easiest way to do it is to attach an event to the form submission to change the value for that input button. You could probably also put an onclick for the submit button itself.
<form action="" method="post">
<input type="submit" name="action" value="Buy" onclick="this.value='some value';"/>
</form>
The button within form is only for checking if the form is submitted or not. You don't need to get the value of the button. Simply write html code:
<form action="smth.php" method="post">
<input type="submit" name="action" value="Buy"/>
</form>
And smth.php will look like that
if(isset($_GET['action']))
{
...
do smth
...
}