Post a Multi-Dimensional Array from HTML Form with dynamic fields - html

I have a form that is little tricky and I'm hoping to submit it with multi-dimensional array. The form is divided as follows.
The first input is lets say a meal name input like breakfast, lunch or dinner. After it enters it. and click on the button add detail.
A new input field will appear called meal-content. After adding it he clicks the button attached to content. A new input will appear called calories. So I Want to submit it like this.
Meal Name->Content->Calories.
There are button like add meal which will add another meal field.
Add Meal Content which will add another meal content for same meal.
So when I submit it the posted data is like this.
Breakfast->Egg->200
->Juice->150
Lunch->Pasta->250
->Spaghetti->190
The problem here is I cannot use index like 0 for names like
Meal[0][Calories][Content]
Because fields are generated at runtime so i cannot know which meal has how many contents and how many meals are there.
<form action="{{route('add-diet')}}" class="diet-program form-horizontal bordered-row" method="post">
{{csrf_field()}}
<div class="tab-content">
<h3 class="content-box-header bg-default">Diet Program Form</h3>
<div class="form-group first-group">
<label class="col-sm-3 control-label">Program Name</label>
<div class="col-sm-6">
<input class="form-control" name="name" placeholder="Program Name... (Optional)" type="text" value="">
</div>
</div>
<div class="form-group meal_number">
<label class="col-sm-3 control-label">Meal Number</label>
<div class="col-sm-6">
<div class="input-group">
<input class="form-control meal_number_input" name="[number][]" placeholder="Meal Number..." type="text" value=""> <span class="input-group-btn"><button class="btn btn-blue-alt meal_detail" disabled type="button"><span class="input-group-btn"><span class="input-group-btn">Add Details</span></span></button></span>
</div>
<div aria-hidden="true" class="modal fade meal_details_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button aria-hidden="true" class="close" data-dismiss="modal" type="button">×</button>
<h4 class="modal-title">Meal Content</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="meal_container">
<div class="meal_content-div col-md-12 col-sm-12 col-xs-12">
<div class="form-group input-group meal_name-div">
<input class="form-control meal_name_input" name="content[]" placeholder="Meal Content..." type="text" value=""> <span class="input-group-btn"><button class="btn btn-blue-alt meal_name_detail" disabled type="button"><span class="input-group-btn"><span>Add Details</span> <i class="glyph-icon icon-plus"></i></span></button></span>
</div>
<div class="form-group meal_name_detail-div">
<div class="col-md-10 col-md-offset-1 form-group calories-div">
<input class="form-control calories" name="calories[]" placeholder="Calories..." type="number" value="">
</div>
<div class="col-md-10 col-md-offset-1 form-group time_taken-div">
<input class="form-control time_take_input" name="taketime[]" placeholder="Time to take... (Optional)" readonly type="text">
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-6">
<button class="btn btn-alt btn-hover btn-primary add_meal_name" type="button"><span>Add Content</span><i class="glyph-icon icon-arrow-up"></i></button>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-default" data-dismiss="modal" type="button">Done</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-6 col-md-offset-3">
<button class="btn btn-alt btn-hover btn-primary add_meal_number" type="button"><span>Add Meal</span> <i class="glyph-icon icon-arrow-up"></i></button>
</div>
</div>
</div>
<div class="form-group submit-btn-div">
<button class="btn ra-100 btn-default" type="submit">Submit</button>
</div>
</form>
My Javascript
/***
* Cloned Div for future duplication
*/
var cloned_meal_number = $('.meal_number').clone();
var meal_content = $('.meal_content-div').clone();
/***
* Enable Meal Number or Meal Name If Entered
*/
$('.tab-content').on('input','.meal_number_input, .meal_name_input',function () {
if($(this).val()) {
$(this).siblings('.input-group-btn').children().prop('disabled',false);
}
else{
$(this).siblings('.input-group-btn').children().prop('disabled',true);
}
});
/***
* Meal Detail Modal Load
*/
$('.tab-content').on('click','.meal_detail',function () {
var meal_number_val = $(this).parent().siblings('.meal_number_input').val();
$(this).closest('.input-group').siblings('.modal').find('.modal-title').html(meal_number_val);
$(this).closest('.input-group').siblings('.meal_details_modal').modal({
keyboard: 'false',
backdrop: 'static'
})
});
/***
* Modal Hide
*/
$('.tab-content').on('click','.modal_close, .close',function () {
$(this).closest('.meal_details_modal').modal('hide');
});
/***
* Meal Detail Toggle Menu
*/
$('.tab-content').on('click','.meal_name_detail', function () {
$(this).find('i').toggleClass('icon-plus icon-minus');
$(this).closest('.meal_name-div').siblings('.meal_name_detail-div').slideToggle();
});
/***
* Add Meal Number
*/
$('.tab-content').on('click','.add_meal_number',function () {
cloned_meal_number.clone().insertAfter('.meal_number:last');
});
/***
* Add Day Button
*/
$('.tab-content').on('click','.add_meal_name',function () {
meal_content.clone().appendTo($('.meal_container'));
});

you can use foreach loop within a foreach loop to run all over your array. I would've used an array of objects for this task since its far more easier to parse than multidimensional arrays, they are also very flexible and will save you precious time, one dimension also, consider that.
as for code example:
foreach($mainArray as $childArray=>$someArrayProp){ }

You can create a JSON with all the data you need like this:
[{
"meal": "breakfast",
"content": [{
"name": "egg",
"calories": "200"
}, {
"name": "juice",
"calories": "150"
}
]
}, {
"meal": "lunch",
"content": [{
"name": "pasta",
"calories": "250"
}, {
"name": "spaghetti",
"calories": "190"
}
]
}
]
but I'm not sure if this is what you need, tell me if not!
Michele

Related

Get data from database based on search by name(Input field) in Laravel

I have an issue with Laravel. I have a page where two fields are given; one is a select option in which two parameters are given (name, PRN). Below this, I have an input field and submit button.
When the user writes the name in the input field and searches. It should get data from that user on the next page in table format.
receipt.blade.php
<body>
<div class="form_design">
<div class="container">
<div class="row">
<div class="col-md-10 mx-auto">
<h1 class="mt-0 text-center mb-4 text-white">Get Receipt</h1>
<p style="text-align: left; margin-bottom: 0px !important;">
Home </p>
<div class="boxed_shadow">
<h5><span class="question-label active">Search your transactions for Receipt</span></h4>
<form action="{{url('/')}}" id="submit_form" method="POST" >
#csrf
<div class="alert alert-danger" id="RegisterAlert" role="alert" style="display:none">Record not found.</div>
<div class="form-group">
<select class="form-control" id="tran_type">
<option value="pg_res_txn_id">PRN</option>
</select>
</div><!-- /input-group -->
<div class="form-group">
<label><span class="star_vld">*</span>Search Based On PRN</label>
<input type="text" class="form-control" id="MainContent_TextBoxEmpId">
</div>
<div class="form-group text-center">
<input type="button" class="btn btn-sm px-5" id="MainContent_ButtonSearch" value="Search" style="background-color: #c2d3c8;font-size: 14px;font-weight: 600;">
</form>
<form id="receipt_list" method="POST" style="display:none" >
<table id="rec_list" border='1' class="table">
</table>
</form>
</div>
</div>
</div>
</div>
</div>
ReceiptController.php
class ReceiptController extends Controller
{
public function index()
{
return view('receipt');
}
}
Please give me the solution for this. If anyone wants more data, I can share it with you want to get data based on search and display it on the next page. Data should be from the database; my table name is 'form2s'.
I didn't get the result. I tried but did not get a result.
I have been working on Laravel for the last three months.

Cannot read property 'setValue' of undefined | Angular

I am having an issue with my HTML component, for some reason the my method is displaying an error:
Cannot read property 'setValue' of undefined when I click the edit button, however everything seems to be ok.
This is my HTML
<div *ngFor="let item of currentUser.myExperiences" class="mt-3">
<div>
<h6>{{item.companyName}} <span *ngIf="item.currentlyWorking" class="badge badge-warning">Currently
working</span>
<span class="float-right">
<a class="text-warning cursor" (click)="openEditModal(item)"><i class="fas fa-pencil-alt"></i></a>
</span>
</h6>
<p class="text-muted p-text-sm">{{item.functions}}</p>
<p class="text-muted p-text-sm" *ngIf="item.references">
<strong>References:</strong>
{{item.references}}
</p>
<hr>
</div>
</div>
<form role="form" [formGroup]="updateForm">
<div class="form-group">
<label for="functions">Funtions</label>
<input type="text" class="form-control" id="functions" name="functions" formControlName="functions" aria-describedby="emailHelp">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
This is my TS component:
public openEditModal(myExperience: IMyExperience): void {
console.log(myExperience);
// Set values to controls
console.log(myExperience.id);
this.experienceId.setValue(myExperience.id);
this.companyName.setValue(myExperience.companyName);
this.functions.setValue(myExperience.functions);
this.workedYears.setValue(myExperience.workedYears);
this.references.setValue(myExperience.references);
this.currentlyWorking.setValue(myExperience.currentlyWorking);
this.updateForm.setValue({
experienceId: this.experienceId.value,
companyName: this.companyName.value,
functions: this.functions.value,
workedYears: this.workedYears.value,
references: this.references.value,
currentlyWorking: this.currentlyWorking.value
});
// Open the modal
this.modalRef = this.modalService.show(
this.editModal,
Object.assign({}, { class: 'gray modal-lg' })
);}
I tried displaying in console the object, but it works perfectly, however I am not sure why this is happening.
I use resolvers to pass information from the route to the component properly.
Thanks in advance.
Try to use this.updateForm.patchValue instead of this.updateForm.setValue.

Get values of dynamic input fields

I have this HTML template:
<div class="container">
<div class="row">
<div class="col-md-12">
<div data-role="dynamic-fields">
<div class="form-inline">
<div class="form-group">
<label class="sr-only" for="field-name">Link </label>
<input type="text" class="form-control" id="field-name" placeholder="Link"
[(ngModel)]="HrefLink" name="HrefLink">
</div>
<span>-</span>
<div class="form-group">
<label class="sr-only" for="field-value">Label</label>
<input type="text" class="form-control" id="Label" placeholder="Label"
[(ngModel)]="HrefLabel" name="HrefLabel">
</div>
<button class="btn btn-danger" data-role="remove">
<span class="glyphicon glyphicon-remove"></span>
</button>
<button class="btn btn-primary" data-role="add">
<span class="glyphicon glyphicon-plus"></span>
</button>
</div> <!-- /div.form-inline -->
</div> <!-- /div[data-role="dynamic-fields"] -->
</div> <!-- /div.col-md-12 -->
</div> <!-- /div.row -->
</div>
In this simple form, I can add one or more inputs. This is the live example: https://bootsnipp.com/snippets/VPRlZ
My question is: How can I get the values of all added fields inside my angular component ? I can add the directive ngModel like above in every input but then all directives will have the same name and I'll not get all values?
Unfortunately, I can't place the ngModel in a higher level in the div of form-group ...
Please tell me if my question is clear or you need more information.
Why not you base the quantity of input in an array of Objects(InputModel it is a custom interface or class) and finally use *ngfor to show them.
InputModel
{
input_value: string,
index_of: number,
isDelete: boolean, //if false is "+" if true is "x"
}
Component.ts
inputValues: InputModel[] = [];
count: number = 0;
OnInit{
this.inputValues.push({
input_value:"",
index_of: this.count,
isDelete: true
});
}
addMore(){
this.inputValues.push({
input_value:"",
index_of: this.count++,
isDelete: false
});
}
Html Template
<div *ngFor="let input of inputValues">
<input [(ngModel)]="input.input_value" />
<!-- TODO button with 'x' or '+' depends of input.isDelete -->
</div>

Simple html/haml form submit button - Post doesn't work

I have a form from a model where I am trying to update multiple records with one submit. I couldn't get it working
Here is the haml code with slight ruby in it.
.container#questions
.col-md-8
- #questions.each do |q|
- ans = #user.answers.where(question_id:q.id).first.try(:content)
.edit-input
%input.form-control.edit-answer{data: {question_id: q.id, url: api_v1_answers_path(), user_id: #user.id}, placeholder: "#{q.description}", value: "#{ans}"}
%button#update-answers{:type =>'submit', :class=> 'btn btn-primary'} Update
Here is the html that gets generated
<div class="container" id="questions">
<div class="col-md-8">
<div class="info">
<div class="question"> Question1 </div>
<div class="edit-input">
<input class="form-control edit-answer" data-question-id="2" data-url="/api/v1/answers" data-user-id="46" placeholder="" value="example1">
</div>
</div>
<div class="info">
<div class="question"> Question2 </div>
<div class="edit-input">
<input class="form-control edit-answer" data-question-id="3" data-url="/api/v1/answers" data-user-id="46" placeholder="" value="example2">
</div>
</div>
<button class="btn btn-primary" id="update-answers" type="submit">Update</button>
</div>
</div>
When I click on update button, I don't see any action or post happening

How can I auto-size a div for certain elements, but let others overlap it?

I have a website with a modal dialog box containing an input form. The text box at the bottom of the form is set up to use an autocomplete list to suggest options as you type into it. This is provided by a typeahead javascript module.
By default, the typeahead list falls underneath the modal footer:
So I edited the css for the containing dev to set the 'overflow' to 'visible'. This allowed the typeahead to overlap the footer, but the div shrunk, because it no longer had to contain all of the content:
The only way I could think to fix this was to make the height of the div constant, but now I have the problem that the error boxes can push the content into the footer:
My question is: Is there a way that I can set up the div so that it automatically resizes to fit its usual content (the input boxes and the validation boxes), but allows the suggestion list to overlap the footer?
I have attached the HTML here:
<div class="modal-body" #*Note! This height value will need to be updated if the contents change, it is here to allow the Typeahead list to
overlap the rest of the div*# style="height: 345px; overflow: visible">
<!-- Container for top half of objection form -->
<div class="span4 offset2">
#Html.Partial("OfferDisplay",Model.OfferDisplayModel)
</div>
<div class="span8">
<form id="rejectionForm" class="form-horizontal" method="POST" data-ajax-pubsubupdate="RenderPresentOfferOptionsForm">
<input type="hidden" value="#Model.NegotiationSessionId" name="NegotiationSessionId"/>
<hr />
<div class="control-group">
#Html.LabelFor(m => m.RejectionReasonId, new { #class = "control-label" })
<div class="controls">
#Html.DropDownListFor(m => m.RejectionReasonId, Model.RejectionReasons, new { #class = "input-large" })
<button type="button" class="btn pull-right btn-validation" tabindex="-1" disabled="disabled"><i class="icon-ok"></i><i class="icon-warning-sign"></i></button>
<div class="help-block">
<p>#Html.ValidationMessageFor(m => m.RejectionReasonId)</p>
</div>
</div>
</div>
<div class="control-group">
#Html.LabelFor(m => m.TargetPrice, new { #class = "control-label" })
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-gbp"></i></span>#Html.TextBoxFor(m => m.TargetPrice, new { #class = "input-medium", style = "width:162px", #data_val_keypressfilter_regex = UIValidationRegularExpressions.NumberKeyPressFilterRegex })
</div>
<button type="button" class="btn pull-right btn-validation" tabindex="-1" disabled="disabled"><i class="icon-ok"></i><i class="icon-warning-sign"></i></button>
</div>
</div>
<div class="control-group">
#Html.LabelFor(m => m.CompetitorNameId, new { #class = "control-label" })
<div class="controls">
<div class="input-prepend">
<input id="competitorName" type="text" data-provide="typeahead" class="input-large" style="width: 198px"/>
</div>
<button type="button" class="btn pull-right btn-validation" tabindex="-1" disabled="disabled"><i class="icon-ok"></i><i class="icon-warning-sign"></i></button>
</div>
</div>
<!-- the real submit button has to be hidden because the button we want to click is outside this div, and the form cannot span both divs -->
<input id="RejectionFormSubmitButton" type="submit" style="display: none"/>
<input id="competitorIdField" name="CompetitorNameId" type="hidden"/>
<input id="showNextOfferInput" type="hidden" value="true" name="ShowNextOffer"/>
</form>
</div>
</div>
<div class="modal-footer">
<form method="POST" data-ajax-pubsubupdate="RenderOverrideConfirmationForm">
<input type="hidden" value="#Model.NegotiationSessionId" name="NegotiationSessionId" />
<input type="hidden" value="#Model.SchemeId" name="SchemeId" />
<button class="btn btn-warning pull-left" type="submit"><i class="icon-warning-sign"></i> #Html.DisplayNameFor(m => m.OverrideButton)</button>
<input id="cancelButton" class="btn" type="button" value="#Html.DisplayNameFor(m => m.CancelButton)"/>
#if (Model.OfferDisplayModel.Offer.IsFinalOffer)
{
<input id="submitAndCloseButton" class="btn btn-primary" type="button" value="#Html.DisplayNameFor(m => m.SubmitAndCloseButton)"/>
}
else
{
<input id="rejectAndShowNextOffer" class="btn btn-primary" type="button" value="#Html.DisplayNameFor(m => m.SubmitButton)"/>
}
</form>
You should try to use the "z-index" css property.