changing a forms action attribute dynamically in a go template - go

Currently the forms action attribute is hardcoded to the id=2
How would I dynamically set it to the ID chosen by the user when pressing submit.
<p>Select a group</p>
<form method="POST" action="/artists/id?id=2">
<label for="selection">Artists:</label>
<select id="selection" name="selection">
<option value=1>Queen</option>
<option value=2>Pink Floyd</option>
<option value=3>Scorpions</option>
</select>
<input type = "submit" value ="submit" />
</form>
Go template:
<p>Select a group</p>
<form method="POST" action="/artists/id?id=2">
<label for="selection">Artists:</label>
<select id="selection" name="selection">
{{range .}}
<option value={{.Id}}>{{.Name}}</option>
{{end}}
</select>
<input type = "submit" value ="submit" />
</form>

Used form method="GET" and made the changes suggested by #cerise

Related

th:field syntax inside th:each is incorrect

I am using spring boot and thymeleaf combination for my project.
Following is the code snippet for Get controller -
#GetMapping("/split")
public String VerticalSplit(Model model) {
MessageContent messageContent0 = new MessageContent();
MessageContent messageContent1 = new MessageContent();
List<MessageContent> messageContentList = new ArrayList<MessageContent>();
messageContentList.add(0, messageContent0);
messageContentList.add(1, messageContent1);
DisplayMessage displayMessage = new DisplayMessage();
model.addAttribute("displayList", displayService.getAllDisplays());
model.addAttribute("groupList", groupService.getAllGroups());
model.addAttribute("messageContentList", messageContentList);
model.addAttribute("displayMessage", displayMessage);
return "combo_vertical_split";
}
And respective html code is as follows -
<html>
<head></head>
<body>
<div class="col-12">
<label class="form-label"> Name of Message </label>
<input type="text" name="name" class="form-control" placeholder="Enter Name Of Message" required="required" th:value="${displayMessage.name}" />
</div>
<hr />
<div th:each="messageContent, iStat : ${messageContentList}">
<h6 align="center" th:text="'Section '+${iStat.count}"></h6>
<div class="form-group">
<label class="form-label"> Select the content type </label>
<select name="messageContentType" id="messageContent" class="form-control" th:onchange="ShowHideTextMediaDiv();" th:field="${messageContent.type}"> <option class="form-select" th:value="Text">Text</option> <option class="form-select" th:value="Image">Image</option> <option class="form-select" th:value="Video">Video</option> </select>
<br />
</div>
</div>
</body>
</html>
When I am trying to load the page, i am getting error as follows -
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'messageContent' available as request attribute
The point which I am not getting is that even though I have declared the messageContent in controller, why am i getting the above mentioned error.
I tried multiple syntax of
th:field="${messageContent.type}"
as follows -
th:field="*{messageContent.type}" //its silly as i have not declared the form object but still i tried
th:field="${messageContentList[__${iStat.index}__].type}"
and few other combinations as well, but nothing worked and almost everytime i got the same error.
You're getting the Neither Bindingnor plain target... error because you have used the th:field in your HTML, but you haven't defined an object from which to bind to it.
You can add your input elements in a form element and add a th:object attribute bound to your model's messageContent object.
You can do something similar to this:
<form th:object="${messageContent}">
<div class="form-group">
<label class="form-label"> Select the content type </label>
<select name="messageContentType" id="messageContent" class="form-control" th:onchange="ShowHideTextMediaDiv();" th:field="*{type}">
<option class="form-select" th:value="Text">Text</option>
<option class="form-select" th:value="Image">Image</option>
</select>
<br />
</div>
</form>

Materialize select dropdown not passing value to controller in Ruby

I have the following code in my view:
<div class="input-field col s6">
<form method="POST" action="/results">
<input type="text" id="track" class="validate" name="inicio">
<label class="active" for="track">Nro Inicial de Tracking:</label>
</div>
<div class="input-field col s6">
<select name="sucu">
<option value="" disabled selected></option>
<option value="5472">Clorinda</option>
<option value="5266">Formosa</option>
</select>
<label>Seleccionar Sucursal</label>
</div>
<button style="text-align:center;"class="btn waves-effect waves-light" type="submit" name="action">Submit
<i class="material-icons right">send</i>
</button>
</form>
And in my controller I'm grabbing the tracking number and sucu value like this:
inicio = params[:inicio].to_i
#sucursal = params[:sucu].to_i
Now, for some reason, the controller is getting the inicio param, which is a number I input, but is not getting the value of the dropdown, instead of getting one of the two values 5266 or 5472, I receive a 0.
Any idea why?
You are right, the error is with Materialize, which seems to ignore the select value. So, in order to get the value, you could add a hidden input that will act as a placeholder for the value that is selected, and then update its value when your select box changes.
<form method="POST" action="/results">
<div class="input-field col s6">
<input type="text" id="track" class="validate" name="inicio">
<label class="active" for="track">Nro Inicial de Tracking:</label>
</div>
<div class="input-field col s6">
<label>Seleccionar Sucursal</label>
<select id="sucu_select">
<option value="" disabled selected></option>
<option value="5472">Clorinda</option>
<option value="5266">Formosa</option>
</select>
<input type="hidden" name="sucu" id="sucu" />
</div>
<button style="text-align:center;"class="btn waves-effect waves-light" type="submit" name="action">Submit
<i class="material-icons right">send</i>
</button>
</form>
<script type="text/javascript">
$(document).ready(function() {
$('select').material_select();
$('#sucu_select').on('change', function() {
$('#sucu').val($('#sucu_select').val());
});
});
</script>
I added an id (id="sucu_select") to the select box and created the hidden input that will store (and post) the sucu value:
<input type="hidden" name="sucu" id="sucu" />
Then i added jQuery to update the hidden value:
$('#sucu_select').on('change', function() {
$('#sucu').val($('#sucu_select').val());
});
May not fix the problem itself, but now you can get the selected value through params.

Form fields in acordina are not geting validated

I am using validationEngine for validating form fields. I have tab and inside the tab, I have two accordions. Each accordion has 4 fields. When I submit, only first accordion form fields that are opened are getting validated. Is there any way I can force to validate all the form fields and open all accordions to show the required field message? Thank you!
<div id="tabs-1">
<div id="accordion">
<h3>Select Agent</h3> <!-- First accordion title) -->
<fieldset>
<div>
<label >Agent Name or ID</label>
<input name="insuredFirsName" title="type "Agent name or id "" class="validate[required] text-input" type="text" >
</div>
<label>Age</label>
<input type="text" name="age" value="${ajaxForm.age}" maxlength="3" size="3" class="validate[required] text-input" >
</fieldset>
<h3>Application Details</h3> <!-- Second accordion title) -->
<fieldset>
<div>
<label>Contract State</label>
<select name="State" id="State" class="validate[required]">
<option value="">Choose a State</option>
<option value="IL">Illinois</option>
<option value="MN">Minnesota</option>
<option value="WI">Wisconsin</option>
</select>
</div>
<div>
<label>Application Number :</label>
<input value="" class="validate[required] text-input" type="text" name="req" id="req" />
</div>
<div>
<label>Application Number :</label>
<input value="" class="validate[required] text-input" type="text" name="req1" id="req1" />
</div>
</fieldset>
</div> <!-- Accordion close -->
<html:button property="button1" value ="Submit"></html:button>
</div> <!-- Tab-1 close -->
maybe a little late but I had this same problem, i searched on the internet and found a solution, but this will work for you:
jQuery("#digitale-aanvraag").validationEngine({
validateNonVisibleFields: true,
});
Your fields in an accordion are set to hidden so as default the validator would skip them.
Be aware that if you use this in a wizard, it won't work, because those fields are also hidden...
Anyway, GL!

Assign Values to Drop-Down Menus in PayPal Third-Party Carts

I am trying to implement a PayPal cart on a third-party site. Customers are first asked to select a product ("Product") from a drop-down menu and then, optionally, to enter a specific amount ("Additional Costs") into a text field. Both values should finally be sent to PayPal upon submitting.
While everything works fine with two text fields, where amount_1 and amount_2 are clearly defined, I struggle with assigning different values (/amounts, say 10.00 and 5.00) to the options of the drop down and to make the value of the selected option my value amount_1. Here is the code I figured out so far:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="whatever#gmail.com">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="item_name_1" value="Product">
<input type="hidden" name="on0_1" value="Product">
<select name="os0_1">
<option value="product1">Product 1</option>
<option value="product2">Product 2</option>
</select>
<input type="hidden" name="item_name_2" value="Additional Costs">
<input type="text" name="amount_2" value="0.00">GBP
<br>
<br>
<input type="hidden" name="currency_code" value="GBP">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
option_select and option_amount do not seem to work with this kind of form. Any help would be greatly appreciated!
I'm assuming this is the select box that is used to select the product amount_1:
<select name="os0_1">
<option value="product1">Product 1</option>
<option value="product2">Product 2</option>
</select>
What you need to do is this:
<input type="hidden" name="item_name_1" value="Product" id="item_name_1">
<select name="amount_1" onchange="changeProductName()" id="amount_1">
<option value="5.00">Product that is a fiver</option>
<option value="10.00">Product that is a tenner</option>
</select>
<script>
function changeProductName() {
var e = document.getElementById('amount_1');
var h = document.getElementById('item_name_1');
h.value = e.options[e.selectedIndex].text;
}
</script>
That will change the item_name_1 to the friendly value of the price option.

Paypal form submission - mobile

Im using below code to submit values in to Paypal and its working.
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick" />
<h2 style="font-family:Segoe UI;">Your Email :</h2>
<input type="text" name="business" value="youremailaddress#yourdomain.com" style="width:1000px; height:50px; margin-left:30px;"/>
<br />
<h2 style="font-family:Segoe UI;">Driveway Size :</h2>
<input type="hidden" name="item_name" value="" />
<select id="item_price" name="amount" style="width:1000px; height:50px; margin-left:30px;">
<option value="325">Single $325.00</option>
<option value="375">Double $375.00</option>
<option value="400">2.5 $400.00</option>
<option value="425">Triple $425.00</option>
<option value="450">3.5 $450.00</option>
<option value="475">Quad $475.00</option>
</select>
<br />
<input type="submit" value="Buy!" style="margin-top:30px"/>
</form>
But this is desktop version. is there anyway that I can do same thing for mobiles?
Yes you can. You could for example use System.Net.WebClient to make the post towards the URL you have in the action field of the form.
Or, you can have the above HTML code embedded inside a web-browser control in your application.

Resources