One servlet One JSP 2 forms [duplicate] - forms

This question already has answers here:
How to transfer data from JSP to servlet when submitting HTML form
(4 answers)
Closed 4 years ago.
One JSP, which contains two forms
<div class="modal" id="modalDialog">
<input method="post" action="newsPage">
<input type="hidden" name="modalForm" value="modalFormPush"/>
<input type= "text" name="title">
<textarea cols="45" maxlength="100" onkeyup="countf()" id="text" name="content"></textarea>
<input type="submit" value="Save" name="modalForm">
</form>
</div>
<div class="modal" id="newsModalDialog">
<form method="post" action="newsPage">
<input type="hidden" name="modalForm" value="modalFormNews"/>
<input type= "text" name="title">
<textarea cols="45" maxlength="100" onkeyup="countf2()" id="news_text" name="content"></textarea>
<input type="submit" value="Save" name="modalForm">
</form>
The goal is to send data from this forms to one servlet, which inserts its to database.
From one form data values must be inserted to one table-database, from second form - to second table-database, accordingly.
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
String modalForm = request.getParameter("modalForm");
if ("modalFormPush".equals(modalForm)) {
Pushdata pushdata = new Pushdata();
pushdata.setTitle(request.getParameter("title"));
pushdata.setContent(request.getParameter("content"));
pushModifier.savePushdata(pushdata);
}
else
if ("modalFormNews".equals(modalForm)) {
Newsdata newsdata = new Newsdata();
newsdata.setTitle_news(request.getParameter("title_news"));
newsdata.setContent_news(request.getParameter("content_news"));
newsModifier.saveNewsdata(newsdata);
}
}
But when I try to send data from one of this forms (for example "newsModalDialog"), joint table is created. This table contains fields from two tables. And this new table is empty.
Thus, values are not inserted to database, through servlet.
Thanks in advance!

I have found a mistake.
Not
<input method="post" action="newsPage">
But
<form method="post" action="newsPage">

Related

Form not submitting with query parameters set with "asp-route" despite being in action

UPDATE: per this response this is behavior of HTML5? I tried setting the parameters in hidden inputs and that works. Doesn't make much sense to me, but what do I know.
I have my form for a "next page" button set up like this:
<form id="next" method="get"
asp-controller="Search"
asp-action="Cities"
asp-route-sortOrder="#ViewData["CurrentSort"]"
asp-route-currentFilter="#ViewData["CurrentFilter"]"
asp-route-pageNumber="#(Model.PageIndex + 1)">
<input form="next" type="submit" class="page-btn" disabled="#nextDisabled" value="Next" />
</form>
When I inspect the page, the form has the correct action url (example):
/Search/Cities?currentFilter=Test&pageNumber=2
But the request actually being made is to
/Search/Cities?
But when it hits the controller, all of the parameters are null. Here is the top of my controller:
[Route("Search/Cities")]
public ActionResult Cities(string SearchString, string sortOrder, string currentFilter, int? pageNumber)
I'm not sure what I'm missing here.
you have 3 choices. This code was tested
first the most popular, you have to use post
<form id="next" method="post"
asp-controller="home"
asp-action="test"
asp-route-sortOrder="CurrentSort"
asp-route-currentFilter="CurrentFilter"
asp-route-pageNumber="1">
<label for="searchString">Search</label>
<input type="text" id="searchString" name="searchString"><br><br>
<input form="next" type="submit" class="page-btn" value="Next" />
</form>
in this case searchingString will be sent in a request body, all another params in a query string
second
<a href="/Home/Test?sortOrder=CurrentSort&currentFilter=CurrentFilter&pageNumber=2">
<button class="page-btn">Next</button>
</a>
the second option will generate get request if it is so important for you, but you will not be able post search string except using ajax.
third, you can use get, but route params should be in the inputs, probably hidden, search string will have a visible input
<form id="next" method="get"
asp-controller="home"
asp-action="test">
<input type="hidden" id="sortOrder" name="sortOrder" value="CurrentSort" />
<input type="hidden" id="currentFilter" name="currentFilter" value="CurrentFilter" />
<input type="hidden" id="pageNumber" name="pageNumber" value="23" />
<label for="searchString">Search</label>
<input type="text" id="searchString" name="searchString"><br><br>
<input form="next" type="submit" class="page-btn" value="Next" />
</form>
in this case nothing will be inside of the body, everything in a query string including searchString.

How to get the post vars from a form via user function

On a Typo3 website a form is integrated. The action should be routed to a typoscript user function.
This is what I tried so far:
The fluid form code (excerpt):
<form action="{f:cObject(typoscriptObjectPath: 'lib.mynlreg')}" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
The typoscript lib:
lib.mynlreg = USER_INT
lib.mynlreg {
userFunc = Vendor\Extension\myClass->myFunction
}
And the class:
class myClass {
public function myFunction($content, $conf) {
$arguments = $this->request->getArguments();
$formEmail = $arguments['email'];
return '<div>' . $formEmail . '</div>';
}
}
I expect to get the content of the form field "email", but after submitting the page throws an error. The question is, how do I get the post vars into the user function? Thank you for any help!
$this->request is not available in a userFunc. As gautamsinh mori says, you should use \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('email');, however I'm not sure you understand what the f:cObject ViewHelper does.
With this code, your HTML before submitting the form will be:
<form action="<div></div>" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
Your HTML after submitting will be:
<form action="<div>filledInEmail</div>" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
I'd recommend making an extension for this, but if you really want/need to do it like this, I think what you're looking for is something like:
<f:cObject typoscriptObjectPath="lib.mynlreg" />
<form action="{uri.page(addQueryString: 1)}" method="post">
<input type="text" name="email" placeholder="Ihre E-Mail-Adresse">
<input type="submit" name="send" value="Jetzt registrieren" class="submit" />
</form>
This will create the form with action to the current page (including any query string). You then have to change the userFunc to return an empty string if the form hasn't been submitted. Something like:
class myClass {
public function myFunction($content, $conf) {
$formEmail = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('email');
if (empty($formEmail)) {
return '';
}
return '<div>' . $formEmail . '</div>';
}
}

Nested Angular Form Groups - Form must reflect HTML structure

Say I have the following formGroup structure:
userGroup = {
name,
surname,
address: {
firstLine,
secondLine
}
}
This forces me to write HTML similar to the following:
<form [formGroup]="userGroup">
<input formControlName="name">
<input formControlName="surname">
<div formGroupName="address">
<input formControlName="firstLine">
<input formControlName="secondLine">
</div>
</form>
Let's say, just for the sake of the example, that you are constrained to write HTML that looks like this:
<form [formGroup]="userGroup">
<input formControlName="name">
<input formControlName="surname">
<div formGroupName="address">
<input formControlName="firstLine">
</div>
<hr>
<div formGroupName="someOtherGroup">
<input id="problemSecondLine" formControlName="???.secondLine">
</div>
</form>
Is there a way, to force the field with id=problemSecondLine to be under userGroup -> address -> secondLine, even though, visually, I have no option but to place it under this particular DIV?
I guess I can manually update via ngModel - but I'm trying to find the cleanest way possible.
You can use formControl directive instead of formControlName
<input id="problemSecondLine" [formControl]="userGroup.get('address.secondLine')">
Plunker Example

serialize array returning an empty array

I'm trying to grab some form data in my function but for some reason my fom returns an empty array when i try and run the function .serializeArray() on it. I've checked that to make sure it input element has a name I just can't figure out why this is happening
this is my form
<form id="uploadForm" name="form" action="{{nodeSocketUrl}}/upload?tenant=qa&envelope=true" method="POST" enctype="multipart/form-data" class="forms" style="left:15px">
<fieldset class="units-row">
<h3>Upload A Presentation</h3>
<label for="presentationFileUpload">Select a .ppt, .pptx, .pdf</label>
<input type="file" id="presentationFileUpload" name="presentationFile" onchange="angular.element(this).scope().onFileSelect(this)"/>
<hr>
<input type="submit" id="fileUploadBtn" class="btn btn-primary disabled" value="Upload" ng-click="uploadFile($event)" name="upload">
</fieldset>
</form>
This is my function which I'm hopping to get the form data from
$scope.uploadFile = function(e) {
e.preventDefault();
//$scope.form.preventDefault();
var dCheck = $('input[type="file"]').val();
console.log(e.currentTarget);
var form = $('#uploadForm').serializeArray();
console.log(form);
}
form returns an empty array any help with this would be appreacited
Why not think in pure angular way?
use can use
<input type="file" ng-model="filePath">
And next, get the file name in $scope.filePath

How to pass undefined number of inputs to a form spring mvc

I'm creating an form which will contain a sort number of hidden inputs and I would like to pass it to my spring MVC server.
The form created will be something like that (not always I will have 3 input types.. I can have more or less than it, the user will set up this value):
<form id="submitCoordenadas" method="post" action="adicionaCorredores">
<button type="submit" id="saveCoordenadas" class="btn" value="Save">Enviar</button>
<input type="hidden" name="corredor" id="linha1" val="[Linha] Inicio: [1;1] Fim: [104;114]">
<input type="hidden" name="corredor" id="linha2" val="[Linha] Inicio: [113;1] Fim: [1;144]">
<input type="hidden" name="corredor" id="linha3" val="[Linha] Inicio: [113;1] Fim: [1;144]">
</form>
What I have tried so far is to create an object which has an List of Strings:
public class Corredores {
List<String> corredor;
........ (getters and setters)
}
and my Spring MVC server:
#RequestMapping("adicionaCorredores")
public String adicionaCorredores(Corredores valores) {
System.out.println("Valores: " + valores.getCorredor().get(0));
return "";
}
I'm not receiving anything on "valores" parameter.
How can I do it? How can I have an undetermined number of inputs in a form and receive it in my Spring MVC server?
I solved my problem..
First of all, I was committing a mistake in my form. Instead of inserting the attribute "value" to it, I was using "val". Now my form looks like this:
<form id="submitCoordenadas" method="post" action="adicionaCorredores">
<button type="submit" id="saveCoordenadas" class="btn" value="Save">Enviar</button>
<input type="hidden" name="corredor" id="linha1" val="[Linha] Inicio: [1;1] Fim: [104;114]">
<input type="hidden" name="corredor" id="linha2" val="[Linha] Inicio: [113;1] Fim: [1;144]">
<input type="hidden" name="corredor" id="linha3" val="[Linha] Inicio: [113;1] Fim: [1;144]">
</form>
Another thing I've done is change my controller to this:
#RequestMapping("adicionaCorredores")
public String adicionaCorredores(#RequestParam("corredor") String[] corredor) {
for(int i=0; i < corredor.length; i++) {
System.out.println("Valores: " + corredor[i]);
}
return "";
}
Instead of receiving an Object "Corredores", I'm just receiving an String array. Note that the name of this String array should be the same of the name given in the form
Hope it helps someone else!