Submitting form when there are 2 forms - html

How do I send the contents of the form with out sending the contents of my other form on the same page? For example
<form class="form" method="get" action="page.php">
<input type="text" value="hi" name="forminput1">
<input type="submit" value="send">
</form>
<form class="form" method="get" action="page.php">
<input type="text" value="byebye" name="forminput2">
<input type="submit" value="send">
</form>
page.php:
if (isset($_GET['forminput1'])) {
//some code
}
if (isset($_GET['forminput2'])) {
//some code
}
Whenever I submit form #2, I end up submitting form #1.

You have not given any of your inputs a name attribute. Without a name="somename" attribute the browser will not pass anything back on the GET or POST.
If you add a name attribute like this
<form class="form" action="page.php">
<input type="text" value="hi" name="data">
<input type="submit" value="send" name="send">
</form>
<form class="form" action="page.php">
<input type="text" value="byebye" name="data">
<input type="submit" value="send" name="send">
</form>
It will suddenly start to work as you expect.
If you want to make both forms unique you can add a different name to the submit buttons.
<form class="form" action="page.php">
<input type="text" value="hi" name="data">
<input type="submit" value="send" name="send_form1">
</form>
<form class="form" action="page.php">
<input type="text" value="byebye" name="data">
<input type="submit" value="send" name="send_form2">
</form>
and then in your PHP, you will be able to differentiate between which form (button) is being submitted like this
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['send_form1'])) {
// User sent form1
}
if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['send_form2'])) {
// User sent form2
}

I think you are using the same action....Forms are independent

Related

Redirect to URL following form input in form submit

I have this HTML code:
<form class="w3-center w3-padding-16" method="get" action="" >
<input type="search" name="q" placeholder="Buscar en {{categoria}}" autofocus required>
<input type="submit">
</form>
My goal is to send the input text to URL. For example if I write "admin" on the textbox the page redirects to "https://example.com/admin"
Add onsubmit to the form, and a JS script. Like this:
function redirect(){
window.location.href="http://www.example.com/" + document.getElementById("urlInput").value;
}
<form id="my-form" onsubmit="redirect(); return false" class="w3-center w3-padding-16" method="get" action="" >
<input id="urlInput" type="search" name="q" placeholder="Buscar en {{categoria}}" autofocus required>
<input type="submit">
</form>

How do I add a submit button, and specify that the form should go to "/action_page.php"?

Add a submit button, and specify that the form should go to "/action_page.php".
<form ____________="/action_page.php">
Name: <input type="text" name="name">
<_________________________>
Try this:
<form action="/action_page.php">
Name: <input type="text" name="name">
<input type="submit" value="Send Request">
</form>
You must use:
<form method="get" action="/action_page.php">
<input type="submit" value="Submit"/>
</form>
And learn this https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form

Forms user entry to append form action

I'm trying to append the form action with what the user puts in the text box and having some problems, could someone help me out of this jam?
<form action="http://wfbscd13.cadence.com/cgi-bin/motd.cgi?msg=1&cmd=replace&text=" method="get">
<label for="mestext1"></label>
<input type="text" size="100" maxlength="80">
<input type="submit" name="button1" id="button1" value="Replace">
</form>
If, for some reason, you need your action to be hardcoded:
<form id="myForm" action="http://wfbscd13.cadence.com/cgi-bin/motd.cgi?msg=1&cmd=replace&text=" method="get">
<label for="mestext1"></label>
<input type="text" id="mestext1" size="100" maxlength="80">
<input type="button" name="button1" id="button1" value="Replace" onclick="submitForm();">
</form>
<script type="text/javascript">
function submitForm()
{
var myForm = document.getElementById("myForm");
myForm.action = myForm.action + document.getElementById("mestext1").value;
myForm.submit();
}
</script>
This is not the right way to do it though. You should be adding inputs named msg and cmd and hide them if needed. Then your code will look like this:
<form id="myForm" action="http://wfbscd13.cadence.com/cgi-bin/motd.cgi" method="get">
<label for="mestext1"></label>
<input type="hidden" name="msg" value="1">
<input type="hidden" name="cmd" value="replace">
<input type="text" name="mestext1" id="mestext1" size="100" maxlength="80">
<input type="submit" name="button1" id="button1" value="Replace">
</form>

how to expand the rows of an input field while keeping it part of the form

I know there quite a few people that say to get a multi-line input you need to use a textarea, but I can't because then it wouldn't be part of the form. Here is my code.
<form action="form.php" method="POST">
<input name="field1" type="text" value="type here" />
<input type="submit" name="submit" value="enter">
</form>
You can't. The only HTML form element that's designed to be multi-line is <textarea>
<form action="form.php" method="POST" id="myForm">
Name: <input type="text" name="usrname" />
<input type="submit" name="submit" value="enter">
</form>
<textarea name="comment" form="myForm"></textarea>
The texarea is outside the form, but by adding the ID of the <form> it's still a part of the form.
Is there a reason you can't use:
<form action="form.php" method="post">
<textarea name="field1"></textarea>
<input type="submit" name="submit" value="enter">
</form>

how to submit a hidden form HTML JSP

I want to update a field in my html form separately from the rest. I know we can't have embedded forms in html so how can I make this work?
<form name="LabelForm" method='POST' action="lab/CA/ALL/createLabel.do">
<input type="hidden" name="lab_no" value="<%=lab_no%>">
<input type="hidden" name="aNum" value="<%=aNum%>">
<input type="hidden" name="label" value="<%=label%>">
<td><input type="submit" value="Create" /></td>
</form>
In the above code, the submit button is outside the main table which is part of another form called ackform. I want to put the submit button in the main table(so everything's neat and orderly) but make it part of LabelForm. The value that is entered by the user is "label" which I want to submit with the LabelForm.
Here's my guess:
<form name="TDISLabelForm" method='POST' action="lab/CA/ALL/createLabelTDIS.do">
<input type="hidden" name="lab_no" value="<%=lab_no%>">
<input type="hidden" name="accessionNum" value="<%=accessionNum%>">
<input type="hidden" id="label" name="label" value="<%=label%>">
<td><input type="submit" value="Create" /> <input type="button" onclick="form2.submit()" value="save in the hidden form">
</td>
</form>
<form name="form2" target="fr1" action="....your post code..." method="post">
<input type="hidden" id="label" name="label" value="<%=label%>">
</form>
<iframe style="height:1px;width:1px;border:none:" id="fr1"></iframe>