is there a way to have a nested form?
like this
<form action="" method="post">
<input type="text".... />
<input type="text".... />
<form action="some action">
<input type="submit" .... />
</form>
</form>
if not, how would I go around getting the same function without nested forms
No you can't have nested forms but there is a simple solution to your problem.
<form action="" method="post">
<input type="submit" name="action" value="action1" />
<input type="submit" name="action" value="action2" />
</form>
If you use PHP this code will handle your form submissions:
//if user clicks on the first submit button, action1 is triggered
if (isset($_POST['action']) && $_POST['action'] == 'action1') {
// do something
}
//if user clicks on the second submit button, action2 is triggered
if (isset($_POST['action']) && $_POST['action'] == 'action2') {
// do something else
}
ps: As I see you work with C# and .NET, this would be translated into:
public class ExampleController : Controller
{
....
[HttpPost]
public ActionResult Index(string action)
{
if (!string.IsNullOrEmpty(action) == "action1") {
// Do something
}
if (!string.IsNullOrEmpty(action) == "action2") {
// Do something
}
}
...
}
Related
I want to make a survey based on forms, in which I try to get the new (next) form after a submit on the first form like below.
<form id="pass" action="form.html" method="get">
---some code for users to select answers--
<button type="submit" form="nextpass" value="next">
</form>
<form id="nextpass" action="form.html" method="get">
Both forms are displayed on the same page, but when I press the next button, I get the error "cannot get /".
Simplest way is to give each submit button a unique name. Then check if it was submitted, and process accordingly.
<form id="form_1" action="" method="post">
<input type="submit" name="submit_1" value="Submit" />
</form>
<form id="form_2" action="" method="post">
<input type="submit" name="submit_2" value="Submit" />
</form>
<?php
if( $_POST['submit_1'] ) {
// Do stuff
//hide form 1
echo "<style>#form_1{display:none}</style>";
}
else if( $_POST['submit_2'] ) {
// Do stuff
}
?>
I've some form like this:
<form action="#" method="post">
<input type="text" name="website" />
</form>
How I can find if user enter website with http:// or without? I want to show it in:
<a href="value from input">
Any suggestions how to validate this form and send it to database as right link like: http://website...?
You may use javascript to validate input.
function validate()
{
var input = document.getElementsByName("website")[0].value;
if(input.startsWith("http://")) {
alert("valid input");
return true;
}
alert("invalid input");
return false;
}
<form action="#" method="post" onsubmit="return validate()">
<input type="text" name="website" />
<input type="submit" />
</form>
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 few <div> inside a forms, each thing inside the div contains a specific form.
When a user presses the submit button, I want to execute a different action based on
<form method="get" action="addprogramtodb.jsp">
<select name="cid" style="display: none;">
<option>1</option>
<option>2</option>
</select>
<div id="1">
</div>
<div id="2">
</div>
<div id="3">
</div>
<input type="submit"/>
</form>
When the user presses the submit button I want the program to execute different queries based on what div it is in.... based on the div id, or somehow..
Give the submit button a name and value the usual way.
<input type="submit" name="action" value="action1">
...
<input type="submit" name="action" value="action2">
...
<input type="submit" name="action" value="action3">
The pressed button is namely available as request parameter as well.
String action = request.getParameter("action");
if ("action1".equals(action)) {
// action1 button is pressed.
} else ("action2".equals(action)) {
// action2 button is pressed.
} else ("action3".equals(action)) {
// action3 button is pressed.
}
You can if necessary give them a different name instead and then nullcheck each request parameter.
<input type="submit" name="action1" value="This is more i18n friendly">
...
<input type="submit" name="action2" value="Blah">
...
<input type="submit" name="action3" value="More blah">
with
if (request.getParameter("action1") != null) {
// action1 button is pressed.
} else (request.getParameter("action2") != null) {
// action2 button is pressed.
} else (request.getParameter("action3") != null) {
// action3 button is pressed.
}
Or, if they are actually all in their own <form>, then you can also pass a hidden input along.
<form>
<input type="hidden" name="action" value="action1">
...
</form>
<form>
<input type="hidden" name="action" value="action2">
...
</form>
<form>
<input type="hidden" name="action" value="action3">
...
</form>
with the same server-side handling as in 1st example.