how to deal with fields outside of form tag - forms

Here is my code.
In this code, as you can see, there is no form tag wrapping around fields.
but somehow it can process fields. from my understanding, I always thought that fields are needed to be wrapped around form tag. How come this works?
Would anyone can explain this to me please? Thank you.
<form role="form" method="post">
<input type='hidden' name='bno' value="${boardVO.bno}">
</form>
<div class="box-body">
<div class="form-group">
<label for="exampleInputEmail1">Title</label> <input type="text"
name='title' class="form-control" value="${boardVO.title}"
readonly="readonly">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Content</label>
<textarea class="form-control" name="content" rows="3"
readonly="readonly">${boardVO.content}</textarea>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Writer</label> <input type="text"
name="writer" class="form-control" value="${boardVO.writer}"
readonly="readonly">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-warning">Modify</button>
<button type="submit" class="btn btn-danger">REMOVE</button>
<button type="submit" class="btn btn-primary">LIST ALL</button>
</div>
<script>
$(document).ready(function(){
var formObj = $("form[role='form']");
console.log(formObj);
$(".btn-warning").on("click", function(){
formObj.attr("action", "/board/modify");
formObj.attr("method", "get");
formObj.submit();
});
$(".btn-danger").on("click", function(){
formObj.attr("action", "/board/remove");
formObj.submit();
});
$(".btn-primary").on("click", function(){
self.location = "/board/listAll";
});
});

Related

Html Form inside another form not redirecting to the correct URL

I've the following form on JSP:
<form class="form-horizontal" style="margin-bottom: 24px">
<fieldset id="myFieldset" disabled>
<div class="form-group">
<label class="control-label col-sm-3" style="text-align: left">Created By</label>
<div class="col-sm-4">
<input class="form-control" type="text" name="created_by" value="${inputConfiguration.getCreatedBy()}" readonly/>
</div>
</div>
.
.
. few more divs of form-group
.
.
</fieldset>
<div class="form-group row">
<div class="col-sm-offset-4">
<form action="${pageContext.request.contextPath}/Configuration/EditConfiguration" method="GET">
<input type="hidden" name="marketplace" value="${param.marketplace}">
<input type="hidden" name="program" value="${param.program}">
<button class="btn btn-primary"> form_Edit </button>
</form>
</div>
<div class="col-sm-1">
<form action="${pageContext.request.contextPath}/Configuration/CopyConfiguration" method="GET">
<input type="hidden" name="marketplace" value="${param.marketplace}">
<input type="hidden" name="program" value="${param.program}">
<button class="btn btn-primary"> form_Copy to New </button>
</form>
</div>
</div>
</form>
However, the forms that are within the outer-form are not redirecting to the correct path on clicking their respective buttons. NOTE: the hidden input fields are just to pass them as url parameters to the paths mentioned in the action and nothing else. Can anyone suggest as to how to pass these as url params and how to make the nested forms work?

How to set a Razor Html.BeginForm within another Html.BeginForm

Description
I want to have two buttons to edit and delete an identity role. The edit button works but if I want to delete a role the site is reloading but nothing happens.
The post function will not be called. Is there another way to have a post form in another post form?
Code
<div class="row mb-3">
<div class="col-12">
<div class="card">
<div class="card-body">
<h4 class="card-title">Rollennamen bearbeiten</h4>
#using(Html.BeginForm("EditRole", "Administration", FormMethod.Post)) {
<div class="form-group row">
<label asp-for="EditRole.RoleName" class="col-sm-12 col-md-3"></label>
<div class="col-sm-12 col-md-9">
<input asp-for="EditRole.Id" type="hidden" value="#roles.Id" />
<input asp-for="EditRole.RoleName" class="form-control" value="#roles.Name" />
<span asp-validation-for="EditRole.RoleName" class="text-danger"></span>
</div>
</div>
<div class="d-flex flex-row-reverse">
<button type="submit" class="btn btn-success">Rolle bearbeiten</button>
#using(Html.BeginForm("DeleteRole", "Administration", FormMethod.Post)) {
<input name="id" type="hidden" value="#roles.Id" />
<button type="submit" class="btn btn-danger mr-2">Rolle löschen</button>
}
</div>
}
</div>
</div>
</div>
</div>
You could handle multiple submit in one form use asp-action like
#using (Html.BeginForm(FormMethod.Post))
{
<div class="d-flex flex-row-reverse">
<input type="submit" value="Save" class="btn btn-success" asp-controller="Administration" asp-action="EditRole" />
<input type="submit" value="Delete" class="btn btn-danger" asp-controller="Administration" asp-action="DeleteRole" />
</div>
}

How to access to RESTController through a controller?

I am using Spring.
The method in my RESTController is this,
http://localhost:8080/delivery-api/training/submit. It is able to save hard code values into database.
This is part of my html form, it can be open up using this URL: http://localhost:8080/delivery-web/training/apply.
<form th:object="${applyForm}" class="form-horizontal" role="form" method="post" action="http://localhost:8080/delivery-api/training/submit" >
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1" > Country </label>
<div class="col-sm-9">
<input type="text" id="form-field-1" placeholder="Country"
class="col-xs-10 col-sm-7" th:field="*{country}" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1"> Fee </label>
<div class="col-sm-9">
<input type="text" id="form-field-1" th:field="*{Fee}" class="col-xs-10 col-sm-7" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1"> Start Date </label>
<div class="col-sm-9">
<input type="date" id="form-field-1" th:field="*{startDate}"
class="col-xs-10 col-sm-7" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right"
for="form-field-1">End Date </label>
<div class="col-sm-9">
<input type="date" id="form-field-1" th:field="*{endDate}"
class="col-xs-10 col-sm-7" />
</div>
</div>
<br> <br>
<button class="btn btn-info" type="submit">
<i class="ace-icon fa fa-check bigger-110"></i> Submit
</button>
</div>
</div>
</form>
When I click on submit, it can be directed to http://localhost:8080/delivery-api/training/submit and insert the hard coded values into database, but it did not direct to my application-form.html page after that.
My controller
#PostMapping(value = "/apply")
public String apply(#Valid #ModelAttribute("applyForm") DeliveryApplicationForm applyForm, BindingResult errors, Model model) {
model.addAttribute("applyForm", applyForm);
return VIEW_PATH + "application-form";
}
I would like to render html page and also pass data from my form to RESTController method through a controller after I click on the submit button in my html page.
May I know how I can do it?

Angular 4 form validation with input fields and checkboxes

I have this form right here:
<form (ngSubmit)="onSubmit()" #testForm="ngForm">
<div class="form-group col">
<label for="a">a</label>
<select class="form-control" id="a" [(ngModel)]="form.a" name="a" required>
<option value="x">x</option>
<option value="y">y</option>
</select>
</div>
<div class="form-group">
<label for="b">b</label>
<input class="form-control" id="b" [(ngModel)]="form.b" name="b" required />
</div>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input">c
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input">d
</label>
</div>
<div class="col text-right">
<button type="submit" class="btn btn-success" [disabled]="!testForm.form.valid">Submit</button>
</div>
</form>
With this component-code behind:
export class FormComponent {
form = new Form();
submitted = false;
onSubmit() {
this.submitted = true;
}
}
What I want to achieve is that my button only gets enabled, when both the dropdown and the input field are filled in, which works with the required attribute, AND both checkboxes are checked, which doesn't work.
My question: Is there something like required for Checkboxes, or are there other ways to solve this problem?
Try this way:
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" [checked]="ch2" (change)="ch2= !ch2" class="form-check-input">c
</label>
</div>
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" [checked]="ch1" (change)="ch1= !ch1" class="form-check-input">d
</label>
</div>
<div class="col text-right">
<button type="submit" class="btn btn-success" [disabled]="!testForm.form.valid || !ch1 || !ch2">Submit</button>
</div>

Laravel. Button in FORM tag doesn't work like the others

The buttons inside my panel-body doesn't work. This is the code:
<div class="panel-body">
<table border="3">
<!-- <div id="tray"></div> -->
#forelse($carts as $cart)
<div class="row">
<div class="col-md-1">{{$cart->id}}</div>
<div class="col-md-3">{{$cart->name}}</div>
<div class="col-md-4">
<div class="row clearfix">
<div class="col-md-1">
<form action="/restaurant/minus" method="POST">
<input type="hidden" name="trayid" id="trayid" value="{{$cart->id}}">
<input type="hidden" name="trayprice" id="trayprice" value="{{$cart->price}}">
<button type="submit" class="btn btn-xs btn-primary" style="border-radius: 50%;" id="{{$menu->id}}"><i class="fa fa-minus" aria-hidden="true"></i></button>
</form>
</div>
<div class="col-md-4">
<input type="number" min="1" value="{{$cart->quantity}}" style="width:40px;">
</div>
<div class="col-md-1">
<form action="/restaurant/plus" method="post">
<input type="hidden" name="trayid" id="trayid" value="{{$cart->id}}">
<input type="hidden" name="trayprice" id="trayprice" value="{{$cart->price}}">
<button type="submit" class="btn btn-xs btn-primary" style="border-radius: 50%"><i class="fa fa-plus" aria-hidden="true"></i></button>
</form>
</div>
</div>
</div>
<div class="col-md-2">
<input type="hidden" name="trayprice" value="{{$cart->price}}">
</div>
<div class="col-md-1">
<form action="/restaurant/delete" method="post">
<input type="text" name="deleteid" id="deleteid" value="{{$cart->id}}">
<button type="submit" class="btn btn-xs btn-danger" style="border-radius: 50%;"><i class="fa fa-trash" aria-hidden="true"></i></button>
</form>
</div>
</div>
#empty
#endforelse
</table>
</div>
Meanwhile, the button in my panel-header works, and it just clears the body.
<div class="panel-heading">
<div class="row clearix">
<form class="col-md-12" method="POST" action="/restaurant/cleartray">
{{ csrf_field() }}
<p class="pull-left">Tray</p>
<button type="submit" class="btn btn-danger btn-xs pull-right"><i class="fa fa-eraser" aria-hidden="true"></i> Clear</button>
</form>
</div>
</div>
Is there anything wrong with the structure that made it impossible for the button to work?
P.S: It doesn't even gets to the route target. It doesn't refresh or something like that. It's like the button itself is unresponsive.
EDIT: I found out that the buttons in the form doesn't work once it's inside the forelse loop.
I think you can change your route action like:
<form action="/restaurant/plus" method="post">
TO
<form action="{{ url('restaurant/plus') }}" method="post">
Hope this work for you!
I kinda got the answer by literally debugging it, haha.
It turns out that having a form tag inside the table tag isn't going to go well. So I took out the table tag. (Didn't remember why I had it in the first place)
Aaaaand voila! It worked!