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

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!

Related

Salesforce Web-to-Lead form collecting UTM data after browsing multiple pages

I have a salesforce web-to-lead form that is set up to collect utm data, and it does.... if I dont leave the page.
Currently, I am not using sf web to lead form. If the user comes to site from an ad, the utm parameters are stored in a cookie and used if the user completes a form. It works perfectly.
I now am required to use sf web to lead forms. If I land directly on the page and never leave, the utm parameters in url are successfully collected in the form. If I leave page and return to form page, I can see the utm parameters stored in the cookie, but the form does not collect.
Please send help!!!!! I need to be able to navigate away from page and use stored cookie to populate the utm hidden form fields.
<form id="salesforceForm" method="POST" action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8">
<input name="oid" type="hidden" value="mySFID#">
<input name="retURL" type="hidden" value="myredirectlink.com">
<label for="first_name">First Name*</label> <input id="first_name" maxlength="40" name="first_name" required="" size="20" type="text">
<label for="last_name">Last Name*</label> <input id="last_name" maxlength="80" name="last_name" required="" size="20" type="text">
<label for="email">Email*</label> <input id="email" maxlength="80" name="email" required="" size="20" type="text">
<label for="company">Company*</label> <input id="company" maxlength="40" name="company" required="" size="20" type="text"> <label for="phone">Phone*</label> <input id="phone" maxlength="40" name="phone" required="" size="20" type="text">
<input id="utm_source" name="00N50000003KWmr" type="hidden" value="">
<input id="utm_medium" name="00N50000003KWn6" type="hidden" value="">
<input id="utm_campaign" name="00N50000003KWnB" type="hidden" value="">
<input id="utm_term" name="00N50000003KWnG" type="hidden" value="">
<input id="utm_content" name="00N50000003KWnL" type="hidden" value="">
<input name="btnSubmit" type="submit">
</form>
<script type="text/javascript">
function parseGET(param) {
var searchStr = document.location.search;
try {
var match = searchStr.match('[?&]' + param + '=([^&]+)');
if (match) {
var result = match[1];
result = result.replace(/\+/g, '%20');
result = decodeURIComponent(result);
return result;
} else {
return '';
}
} catch (e) {
return '';
}
}
document.getElementById('utm_source').value = parseGET('utm_source');
document.getElementById('utm_medium').value = parseGET('utm_medium');
document.getElementById('utm_campaign').value = parseGET('utm_campaign');
document.getElementById('utm_term').value = parseGET('utm_term');
document.getElementById('utm_content').value = parseGET('utm_content');
</script>
There's nothing here that actually sets the cookie, right? Or reads from it.
I've never actively used Google Tag Manager and you're saying something sets the cookie already...
My gut feel you need something like if(parseGET('utm_source') == ""), then use functions from https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
This helps?
let utm_source = parseGET('utm_source');
if(!utm_source){
utm_source = document.cookie
.split('; ')
.find(row => row.startsWith('utm_source='))
.split('=')[1];
}
document.getElementById('utm_source').value = utm_source;
?
Untested, you'll have to experiment and put right names of cookies.

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>';
}
}

One servlet One JSP 2 forms [duplicate]

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">

How to pass data from a form in a view to another controller with C# MVC?

If I have a form that needs to use a textbox input like as below:
#{
if(IsPost){
username = Request.Form["username"]
}
}
<form action="Home/Index" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
The controller is something like below,
public class HomeController : Controller
{
public ActionResult Index (string username) {
if (string username != string.Empty)
{
Console.WriteLine("Your username is " + username);
}
return View();
}
}
Seems like the data is not being passed from the post method. When I hit submit the URL that it requests is Home/Home/Index, which is not were the controller(HomeController) action is located, it should be Home/Index, and use the HomeController right?
What if I need to pass this data to a different controller that has an Index for the action, like UserController?
In this instance, you're missing a slash!
<form action="/Home/Index" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
But when using asp.net mvc, you should use Url.Content with the home directory character to ensure that if your site is deployed in a sub-directory of the site, the correct root will be found. So use:
<form action="#Url.Content("~/Home/Index")" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>

JSP - check form submission

Let say I have a simple form with no required fields:
<form action="index.jsp" method="post">
<input type="text" name="firstName" />
<input type="text" name="lastName" />
<input type="text" name="email" />
<input type="submit" value="submit" />
</form>
I want to check if the form was submitted by checking the submit parameter (because it's always present). In PHP I can do a simple
if ( $_POST['submit'] )
but the request.getParameter("submit") doesn't seem to work.
So what's the best way to check if a form was submitted?
You need to give the input element a name. It's the element's name which get sent as request parameter name.
<input type="submit" name="submit" value="submit" />
Then you can check it as follows:
if (request.getParameter("submit") != null) {
// ...
}
You perhaps also want to check if "POST".equalsIgnoreCase(request.getMethod()) is also true.
if ("POST".equalsIgnoreCase(request.getMethod()) && request.getParameter("submit") != null) {
// ...
}
Better, however, would be to use a servlet and do the job in doPost() method.
You can try this way:-
if ("POST".equalsIgnoreCase(request.getMethod())) {
// Form was submitted.
} else {
// It may be a GET request.
}