Materialize select dropdown not passing value to controller in Ruby - 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.

Related

Request values of contoller not getting value of select

This is a noob question, I send a post request to my controller and all it can get is the request of the input. What I want is I can also get the value of $cat array.
Here's my blade:
#if (!$categories->isEmpty())
<form action="{{route('subcategory.store')}}" method="POST">
#csrf
<div class="row">
<div class="input-group mb-3 col-md-4">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Choose Category:</label>
</div>
<select class="custom-select" id="inputGroupSelect01">
#foreach ($categories as $cat)
<option value="{{$cat->id}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
<input type="text" name="subcat" id="subcat" class="form-control col-md-3 ml-5">
<button type="submit" class="btn btn-success btn-lg ml-3">Save Sub Category</button>
</div>
</form>
#endif
And here's my returned value.
{"_token":"XlzwIt2jFqcybqqSjwLLJIAChG5tui7gvHzlaeAq","subcat":"xx"}
You forgot to add name="cat" in select i guess
<select class="custom-select" id="inputGroupSelect01">
simple add name="" attr
<select class="custom-select" id="inputGroupSelect01" name="cat">

Stuck on filling options of HTML select in a modal popup in codeiginiter php

I am trying to put dynamic key and value to the dropdown. which is an HTML select type. The component is part of modal popup. I am getting trouble in passing the array list to the modal popup
<div class="modal-body">
<form method="post" action="<?php echo base_url() ?>admin/addItemToInventory" >
<input type="hidden" name="txtpatient" id="txtpatient">
</div>
<div class="row" >
<div class="col-md-6" >
<div class="form-group" >
<label>Choose Inventroy Level of Item </label>
<select name="selectInventory" id="selectInventory" class="selectpicker" data-style="btn btn-info btn-block" title="Single Select" data-size="7">
<option value="1">Full</option>
<option value="2">Half-Full</option>
<option value="3">Half-Empty</option>
<option value="4">Empty</option>
<option value="5">Emergency</option>
</select>
The array list with necessary data are already avilable as data-id=""
<a data-toggle="modal" data-target="#modalAddInventory" data-patient="$inventory[0]["PatientId"]" data-id="<?php $doctor ?>" style="float:right" class="btn btn-success chk1item" >Add Item</a>
I am looking to replace the hardcoded options with dynamic​ values from database

attributes in form that define in modal does not get dynamic value in laravel :(

I created a table for editing the Movie Object.
One of the table's column is a form that pops up with a modal class (bootstrap)
Here is my code:
<div class="modal-body">
<form method="post" action="{{asset(route('movies.update', $movie->id))}}">
<input type="hidden" name="_token" value="{{csrf_token()}}">
{{method_field('PATCH')}}
<div class="form-group">
<label for="movie-name" class="col-form-label">Name:</label>
<input type="text" class="form-control" name="movie_name" id="movie-name" placeholder="{{$movie->name}}">
</div>
<div class="form-group">
<label for="movie-director" class="col-form-label">Direcor:</label>
<select class="form-control" name="Director">
#foreach($directors as $director)
<option value="{{$director->id}}">{{$director->name}}</option>
#endforeach
</select>
</div>
<button class="form-control btn btn-success" type="submit" class="btn btn-primary">Send message</button>
</form></div>
When I choose one of the edit buttons, the value of the first item (movie) passes to my controller.
I've tested my code and find out it's happening because I have used the modal class.
Do you have any ideas to solve this problem?

Edit db record with modal window

I'm trying to edit some database record based on an ID that I'm saving into a button value.
#foreach ($employment as $empl)
<button data-toggle="modal" data-target="#edit-empl" href="#edit-empl" class="btn btn-default editbtn-modal" value="{{ $empl->id }}" type="button" name="editbtn">Edit</button>
<h3 class="profile-subtitle">{{ $empl->company }}</h3>
<p class="profile-text subtitle-desc">{{ $empl->parseDate($empl->from) }} - {{ $empl->parseDate($empl->to) }}</p>
#endforeach
As you can see here, I have an edit button with an id attached.
When I click edit I open a modal window to edit the fields and later on submit the form.
The thing is, I'm not sure how to get that id from the button into the modal window so I can compare the values and display the correct fields..
<form class="app-form" action="/profile/employment/edit/{id}" method="POST">
{{ csrf_field() }}
<input class="editID" type="hidden" name="editID" value="">
#foreach ($employment as $empl)
#if ($empl->id == buttonidhere)
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="{{ $empl->company }}">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="{{ $empl->from }}">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="{{ $empl->to }}">
</div>
#endif
#endforeach
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
I was able to pass the button value into the modal using javascript.. I put it into a hidden input but that doesn't help me at all because I can't get the input value in order to compare the values..
Solution 1: Using ajax
Step 1: Create a route in laravel which will return a JSON object containing employing data of requested employee.
For e.g,
/profile/employment/data/{empl_id}
Will get you employement data of id empl_id.
Step 2: Change your form as below
<form class="app-form" action="/profile/employment/edit/{id}" method="POST">
<input class="editID" type="hidden" name="editID" value="">
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="">
</div>
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
Step 3: Use javascript(jQuery) to get the data using ajax and load it into the form in modal.
jQuery code:
$(document).on("click", ".editbtn-modal", function() {
var id = $(this).val();
url = "/profile/employment/data/"+id;
$.ajax({
url: url,
method: "get"
}).done(function(response) {
//Setting input values
$("input[name='editID']").val(id);
$("input[name='company']").val(response.company);
$("input[name='to']").val(response.to);
$("input[name='from']").val(response.from);
//Setting submit url
$("modal-form").attr("action","/profile/employment/edit/"+id)
});
});
Solution 2: Using remote modal
Step 1:
Create another blade file for eg. editEmployee.blade.php and add the above form in it.
<form class="app-form" id="modal-form" action="/profile/employment/edit/{{ $empl->id }}" method="POST">
{{ csrf_field() }}
<input class="editID" type="hidden" name="editID" value="{{ $empl->id }}">
<div class="form-group">
<label for="company">Company:</label>
<input type="text" name="company" value="{{ $empl->company }}">
</div>
<div class="form-group">
<label for="month">From:</label>
<input type="date" name="from" value="{{ $empl->from }}">
</div>
<div class="form-group">
<label for="to">To:</label>
<input type="date" name="to" value="{{ $empl->to }}">
</div>
<div class="row">
<div class="col-sm-6">
<input type="submit" class="btn btn-primary profile-form-btn" value="Save Changes">
</div>
</div>
</form>
Step 2: Create a controller which would return the above form as HTML.
Tip: use render() function. example
Step 3: load the form into modal window before showing using javascript(jQuery)
considering your modal id is "emp-modal"
$(document).on("click", ".editbtn-modal", function() {
var id = $(this).val();
url = "/profile/employment/data/"+id;
$('#emp-modal').modal('show').find('.modal-body').load(url);
});
One solution would be to send the details you want the same way you send the id to the modal.
and the proper way to send variables to modal is to include this in the button that opens modal:
data-variablename="{{$your-variable}}"
use this jQuery to get the values of your variables to modal. where edit-empl is the id of your modal and data-target of your button
$('#edit-empl').on('show.bs.modal',function (e) {
var variablename= $(e.relatedTarget).data('variablename');
$(e.currentTarget).find('input[id="yourinputID"]').val(variablename);

ajaxSubmit not working IE9. All other browsers seem fine

I have a simple (naked) MailChimp email signup form that I can't get to work in IE9. Every other browser seems fine. I know this question has been asked before but in various different circumstances. We are using JQuery Form Plugin.
<script type="text/javascript">
$(document).ready(function(){
$("#mc-embedded-subscribe-form").validate({
submitHandler: function(form) {
$(form).ajaxSubmit();
/*DO SOME OTHER STUFF*/
}
});
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="mc_embed_signup">
<form action="//#####.us3.list-manage.com/subscribe/post?u=#############&id=##########" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
<div id="mc_embed_signup_scroll">
<h2>Subscribe to our mailing list</h2>
<div class="indicates-required"><span class="asterisk">*</span> indicates required</div>
<div class="mc-field-group">
<label for="mce-EMAIL">Email Address <span class="asterisk">*</span>
</label>
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
</div>
<div class="mc-field-group">
<label for="mce-FNAME">First Name <span class="asterisk">*</span>
</label>
<input type="text" value="" name="FNAME" class="required" id="mce-FNAME">
</div>
<div class="mc-field-group">
<label for="mce-MMERGE10">State </label>
<select name="MMERGE10" class="" id="mce-MMERGE10">
<option value=""></option>
<option value="NSW">NSW</option>
<option value="VIC">VIC</option>
<option value="QLD">QLD</option>
<option value="WA">WA</option>
<option value="ACT">ACT</option>
<option value="SA">SA</option>
<option value="NT">NT</option>
<option value="TAS">TAS</option>
</select>
</div>
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div> <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
<div style="position: absolute; left: -5000px;"><input type="text" name="b_8bd18de15624fc1fb89b76d51_29d17890f0" tabindex="-1" value=""></div>
<div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
</div>
</form>
</div>

Resources