How to call url which depends on input in Thymeleaf? - html

I have a form in Thymeleaf in which there is a drop down list and a button. I want to call an URL when the button is clicked which depends on the value of drop down selected. From the dropdown, serviceId is selected and then the URL also uses serviceId. How do I do this?
<form action="#" th:action="#{/heart2heart/format/{serviceId}}" method="get" role="form">
<div class="form-group">
<select th:field="*{serviceId}" class="form-control">
<option th:each="service : ${services}"
th:value="${service.id}"
th:text="${service.description}">Customer Service</option>
</select>
</div>
<div class="form-group">
<button type="button" name="addRow" th:text="#{button.download}"
class="btn btn-primary btn-md">Download</button>
</div>
</form>

This is a combination of javascript/jquery and integrating it into your form.
So first you need to set some id's:
<select id="someidyougaveit" th:field="*{serviceId}" class="form-control">
//code
</select>
<form id="yourform" action="#" th:action="#{/heart2heart/format/{serviceId}}" method="get" role="form">
// code
</form>
Then using Javascript change the action after obtaining the value:
var frm = document.getElementById('yourform');
if(frm) {
frm.action = 'yoururl'+$("#someidyougaveit").val();
}

Related

How can I pass a select HTML element as a parameter in a onSubmit form call?

I am trying to pass the html elements of a form through the submit function as parameters. I can get correctly the nameInput element with flag #nameInput, but the select element (#skillSelect) is throwing this error:
- error TS2339: Property 'skillSelect' does not exist on type 'MemberFilterComponent'.
Here is my form template. How can I pass the select element to component as I did with the input text?:
<form
[formGroup]="filterMemberForm"
(ngSubmit)="onSubmit(nameInput, skillSelect)"
>
<div class="form-row">
<div class="col-md-3">
<label class="font-weight-bold"
>Name
<input
ngDefaultControl
type="text"
class="form-control"
label="'Name'"
formControlName="name"
placeholder=" Ex: Maria Novillo"
required
id="name"
#nameInput
(change)="mapChipValue(nameInput)"
/></label>
</div>
<div class="col-md-3" *ngIf="skills.length !== 0">
<label class="font-weight-bold">Skills:</label>
<select
id="skillId"
class="form-control"
formControlName="skillId"
#skillSelect
(change)="mapChipValue(skillSelect)"
>
<option value="">-- Select skills --</option>
<option *ngFor="let skill of skills" [value]="skill.idSkill">
{{ skill.skill }}
</option>
</select>
</div>
<div class="form-row">
<div class="col-md-3 mt-5">
<button type="submit" class="btn btn-primary">Apply</button>
</div>
</div>
</form>
In the html file:
<select class='select-option'
#mySelect
(change)='onOptionsSelected(mySelect.value)'>
<option class='option'
*ngFor='let option of dropDownData'
[value]="option.seo_val">{{option.text_val}}</option>
</select>
In the ts file:
onOptionsSelected(value:string){
console.log("the selected value is " + value);
}
why you need pass the "html element"? in filterMemberForm.value you has an object with the values. Really your form should be
<form
[formGroup]="filterMemberForm"
(ngSubmit)="onSubmit(filterMemberForm)"
>
onSubmit(form:FromGroup)
{
if (form.valid)
console.log(form.value)
else
form.markAllAsTouched()
}
if you need the selectOption name always can makes
onSubmit(form:FromGroup)
{
if (form.valid)
{
//if is not multiple select
const selectedItem=this.skills.find(x=>x.idSkill==form.value.skillId)
console.log(form.value,selectedItem)
//if is multiple select
const selectedItems=this.skills.filter(
x=>form.value.skillId.indexOf(x.idSkill)>=0)
console.log(form.value,selectedItems)
}
else
form.markAllAsTouched()
}

Angular 2 - Dropdown validation issue

i have tried with many ways:
Validation for select field angular 2
How to apply required validation to dropdown in angular 2
This help me to add validation in dropdown. but i have a issue that validation run properly but when i click on submit button it submit the form if dropdown value is valid or not. i do not want to submit my form if the value of dropdown is "select".
this is my HTML code:
<form name="form" (ngSubmit)="f.form.valid && SaveSymbol()" #f="ngForm" novalidate>
<div class="form-group" [ngClass]="{ 'has-error': f.submitted && !DisplayGroupID.valid && !manageSymbolViewModel.DisplayGroupID }">
<label for="DisplayGroupID">Display Group</label>
<select class="form-control" name="manageSymbolViewModel.DisplayGroupID" #DisplayGroupID id="manageSymbolViewModel.DisplayGroupID" [(ngModel)]="manageSymbolViewModel.DisplayGroupID" required>
<option value="0" selected disabled>Select</option>
<option *ngFor="let group of result.DisplayGroups" value={{group.DisplayGroupId}}>
{{group.DisplayGroup}}
</option>
</select>
<div *ngIf="f.submitted && !DisplayGroupID.valid && !manageSymbolViewModel.DisplayGroupID" class="help-block">Display Group is required</div>
</div>
<div class="form-group">
<button [disabled]="loading" type="submit" class="btn btn primary">Save</button>
<a [routerLink]="['/login']" class="btn btn-link">Cancel</a>
</div>
</form>
This is component code:
SaveSymbol() {
this.manageSymbolService.Save(this.manageSymbolViewModel).subscribe(data => {
debugger;
},
error => {
// this.alertService.error(error);
// this.loading = false;
});
}
}
The first and selected option of your select element has a value 0. So it basically it already has a value set to it on page load.
Try to change it to: <option value="" selected disabled>Select</option> and see if that fixes your problem.
Try this
<button [disabled]="loading || !DisplayGroupID.valid || !manageSymbolViewModel.DisplayGroupID" type="submit" class="btn btn primary">Save</button>
This will prevent the form from submitting while there is nothing selected in the dropdown or when the dropdown model is invalid.
please change your form as given below and also pass your status of form through submit function if required
<form #f="ngForm" (submit)="f.valid && SaveSymbol(f.value, f.valid)" novalidate>
<div class="form-group">
<label>Display Group</label>
<select ngControl="DisplayGroupID" #DisplayGroupID="ngForm" [(ngModel)]="manageSymbolViewModel.DisplayGroupID" required>
<option value='' disabled>Select</option>
<option *ngFor="let group of DisplayGroups" value={{group.DisplayGroupId}}>{{group.DisplayGroup}}</option>
</select>
<small [hidden]="DisplayGroupID.valid || (DisplayGroupID.pristine && !submitted)" class="text-danger">
Displaygroup is required
</small>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
and initialise the model as
this.manageSymbolViewModel = {
DisplayGroupID: ''
};
refer this plunkr http://plnkr.co/edit/sFTM22xbZSXBLZcv90o2
If I'm reading your question right...
Your first value will be 0. So simply set your [disabled] attribute accordingly.
Example: [disabled]="loading || DisplayGroupId.value == 0"
Your button will not be enabled while the first option is selected. Simple solution.

I'm trying to append an option value to a form action

I'm trying to append an option value to a form action, using method get. Submit sends the form request but isn't attaching the select option to the action?
So when the form is submitted it should use >
https://url.here.com?domain= users search here + selected option?
Thanks in advance.
<form action="https://url.here.com?domain=" method="get">
<div class="just-arrived-search">
www.<input type="search" name="domain" placeholder="Search" />
<select>
<option value="">All</option>
<option value=".bike">.bike</option>
<option value=".clothing">.clothing</option>
<option value=".guru">.guru</option>
<option value=".holding">.holdings</option>
<option value=".plumbing">.plumbing</option>
<option value=".singles">.singles</option>
<option value=".venture">.ventures</option>
</select>
<button class="btn-new" type="submit">Register Domain</button>
</div>
</form>
UPDATE: Since you edited your initial post to more clearly explain what you are attempting to achieve, here is a simple solution to your issue.
Use JavaScript to append the selected value to the domain before it submits. Change the button to have an onclick attribute as oppose to making it submit the form.
Add this JavaScript to your head section (or wherever you want, but convention is typically the HEAD section or bottom of the body):
<script type="text/javascript">
function submitForm() {
var ext = document.getElementById('ext');
var selected_opt = ext.options[ext.selectedIndex].value;
// Add own code to handle "All" here
var domain = document.getElementById('domain');
// Append selected option
domain.value = domain.value + selected_opt;
// Submit form
document.getElementById('myForm').submit();
}
</script>
And this is the updated HTML code to go along with it:
<form action="https://url.here.com" method="get" id="myForm">
<div class="just-arrived-search">
www.<input type="search" id="domain" name="domain" placeholder="Search" />
<select id="ext">
<option>All</option>
<option>.bike</option>
<option>.clothing</option>
<option>.guru</option>
<option>.holdings</option>
<option>.plumbing</option>
<option>.singles</option>
<option>.ventures</option>
</select>
<button class="btn-new" onclick="submitForm();">Register Domain</button>
</div>
</form>

html input action through button

I am using the following form to take input and then set an action
<select name="page_option">
<option value="go1">GO1</option>
<option value="go2">Go 2</option>
</select>
<input type="submit" value="Change home.php" />
Instead of this I want to have two buttons side by side,one button has text GO1 while other has GO2 and when user click GO1 and GO2 it will serve the same purpose as this above form
Also when Go1 button is clicked the page will display below the buttons "GO1 is on" and when Go2 is clicked it will display below "GO2 is on"
Any clue ?
Try the following..
<form action='action.php' method='post'>
<input type="submit" name='go1' value="go1">
<input type="submit" name='go2' value="go2">
in action.php
if(isset($_REQUEST['go1']))
{
$_SESSION['msg'] = 'Go1 button is on'
}
if(isset($_REQUEST['go2']))
{
$_SESSION['msg'] = 'Go2 button is on'
}
print_r($_SESSION['msg']);
Hope this will help you. You must start session at the beginning of action.php file
Thanks
You can have two submit buttons, as follows:
<input type="submit" value="go1">
<input type="submit" value="go2">
You can check serverside which button was clicked, and lead the user to the appropriate page.
<form action="form_action.asp" method="get">
<select name="page_option">
<option value="go1">GO1</option>
<option value="go2">Go 2</option>
</select>
<input type="submit" value="Submit" />
</form>
action="form_action.asp" method="get"
then use java script
<input type="submit" onclick="function1()" value="go1">
<input type="submit" onclick="function2()" value="go2">
funcation f1(){}
funcation f2(){}

shoppingcart page html forms design

while designing the template for a shoppingcart view,I used a loop to create a list of items in the cart to be displayed .I also want to provide remove buttons to enable the user to remove each item from the cart.The html came out to be like this
here in the below html code,in /books/removefromcart/91 , 91 is the shoppingcart id.
The cartitemid represents the item's id.
Is this bad style to create one form for each list item?Which is the correct way?
<form action="/books/removefromcart/91" method="POST" >
<li>1. mastering java 1 copies
<input type="hidden" name="cartitemId" value="94"/>
<input type="submit" value="remove"/>
</li>
</form>
<form action="/books/removefromcart/91" method="POST" >
<li>2. mastering python 2 copies
<input type="hidden" name="cartitemId" value="93"/>
<input type="submit" value="remove"/>
</li>
</form>
<form action="/books/removefromcart/91" method="POST" >
<li>3. mastering ruby 1 copies
<input type="hidden" name="cartitemId" value="92"/>
<input type="submit" value="remove"/>
</li>
</form>
..
<!--a form for adding item to cart -->
<form action="/books/addtocart/43" method="POST">
<label for="quantity">Number of Copies:</label>
<select id="quantity" name="quantity">
<option selected="" value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
...
</select>
<input type="hidden" name="cartId" value="91"/>
<input type="submit" value="AddToCart" />
</form>
On each button of update/remove call one javascript function and pass that product value to that function...and then you can process on it...
e.g: ---
<input type="button" OnSubmit="remove('91');" value="Remove" />
function remove(id)
{
$.ajax({
url: 'yourpage.aspx,
type: 'POST',
data: 'prodid='+id,
success: function(response) {
/*your code for success goes here */
}
});
}