How to display stored value of datetimepicker to blade in Laravel - laravel

Do you know guys how display the date value from database to datetimepicker field?
the example value from database is 2022-10-31
<div class="input-group date" id="allDay" data-target-input="nearest">
<input type="text" id="all-value" name="all_day"
class="form-control datetimepicker-input" data-target="#allDay"
value="" />
<div class="input-group-append" data-target="#allDay" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
js
$('#allDay,#startLongDay,#endLongDay').datetimepicker({
format: 'L',
minDate: new Date()
});

If you're using the jQuery DateTime Picker, You can retrieve the value from the database and then pass it to the view through your controller. After that, you can set it as the value attribute.
Example:
<input type="text" id="all-value" name="all_day"
class="form-control datetimepicker-input" data-target="#allDay"
value="{{$date}}" />
Or else you can do it in JS Way:
Setting with viewDate option
<script>
var d = "{{$date}}"
$('#allDay,#startLongDay,#endLongDay').datetimepicker('viewDate', d);
</script>
Setting with date option
<script>
var d = "{{$date}}"
$('#allDay,#startLongDay,#endLongDay').datetimepicker('date', d);
</script>
Please note that in all of the above steps you need to format the date correctly.

Related

Laravel, can't set old value to bootstrap datetimepicker, when validate

I'm using Laravel 8.5 (current version)
Therefore I'm new to laravel, I'm trying to learn with simple examples.
I have some blade view, with code part bellow:
<div class="form-group">
<input type="hidden" id="hdnLocale" value="ru">
<input type="hidden" name="hdndefDate" id="hdndefDate" value="2021-01-17">
#if (old('hdndefDate'))
<input type="hidden" name="hdndefDate" value="{{ old('hdndefDate') }}">
#endif
<label for="picker">Birthday</label>
<div class="input-group">
<div class="input-group-prepend">
<button type="button" class="input-group-text" id="toggle">
<i class="fa fa-calendar-alt"></i>
</button>
</div>
<input type="text" id="picker" name="picker" value="{{old('picker')}}" class="form-control" readonly>
</div>
</div>
As you see I'm using two hidden inputs, with a variable value from the controller, that must be set from javaScript (In a simple I use exact value).
Here is my script:
<script>
var locale = $('#hdnLocale').val()
jQuery.datetimepicker.setLocale(locale)
jQuery.datetimepicker.setDateFormatter('moment')
var yourDate = $('#hdndefDate').val();
$('#picker').val(yourDate);
$('#picker').datetimepicker({
timepicker: false,
datepicker: true,
format: 'YYYY-MM-DD',
defaultDate: yourDate,
weeks: false,
hours12: false,
step: 15,
yearStart: 2015,
yearEnd: 2022,
dayOfWeekStart: 1
});
$('#toggle').on('click', function () {
$('#picker').datetimepicker('toggle');
})
</script>
I have some other inputs also, that I've validating via the controller,
when I change the value of datepicker, post form and validation fails, other fields set with old values, except datepicker.
Where I have messed something? I wanna set the old value to datepicker, when validation fails.
As I discovered later, the old value is set and immediately refreshed with default value '2021-01-17'
Seems like you're getting two inputs with name hdndefDate when old values from this input exists.
And in your JS you're getting the value from the input #hdndefDate, that holds the default value.
Change your #if to something like this:
#if (old('hdndefDate'))
<input type="hidden" name="hdndefDate" id="hdndefDate" value="{{ old('hdndefDate') }}">
#else
<input type="hidden" name="hdndefDate" id="hdndefDate" value="2021-01-17">
#endif
Or even better:
<input type="hidden" name="hdndefDate" id="hdndefDate" value="{{ old('hdndefDate') ?? 2021-01-17}}">

Slider Checkbox on modal passing Null instead of 0 or 1 to my DB when checked or unchecked

I´m trying to make an admin panel where the rights of the user are turned off and on with a switch. I can see that my data is beeing passed to the modal, but th rights on the DB are not beeing updated.
My view looks like this:
<script>
$('#edit').on('show.bs.modal', function(event) {
var button = $(event.relatedTarget);
var id = button.data('id'); // Extract info from data-* attributes
var Admin = button.data('Admin');
var modal = $(this);
$('#edit_form').attr('action', '{{URL::to('/')}}/AdminPanel/'+id);
modal.find('#is-Admin').val(Admin);
});
</script>
and my modal looks like this:
<div class="form-group row">
<label for="Admin" class="col-md-4 col-form-label text-md-right">{{__('Admin?') }}</label>
<div class="col-xs-8 col-sm-7 col-md-6">
<label class="switch">
<input id='is-Admin' name='Admin' type="checkbox">
<span class="slider round"></span>
</div>
</div>
I found a workaround, giving my slider an initial value of 0 (no rights) and defining the value when checked (1), I can pass the right values to the DB.
<input name='Admin' type='hidden' value='0'>
<input id='is-Admin' name="Admin" type="checkbox" value='1'>

How to get inputs before submiting and put it on textArea LARAVEL

my input for example :
<div class="form-group">
<label for="name">New Name Offers :</label>
<input name="newNameOffers" class="form-control" id="newNameOffers">
</div>
<div class="form-group">
<label for="">From Email :</label>
<input name="fromEmail" placeholder="Email" class="form-control" id="fromEmail">
</div>
How can i put it on text area ....
That my textArea :
<textarea class="form-control" name="HEADER" id="HEADER" rows="11">i need to dispay my values here <textarea>
(i need to put the values not the name of variable)
I have tried to make a simple form for you. You can modify it as you wish. For this, you need to take help of javascript or other similar front end technologies. However, if your requirement is just to take the value to back end then you can get both value $request->newNameOffers and $request->fromEmail then you can concatenate and store in your desired way.
function myFunction() {
var val1 = document.getElementById("newNameOffers").value;
var val2 = document.getElementById("fromEmail").value;
var res = val1 +' '+ val2;
document.getElementById("HEADER").innerHTML = res;
}
<!DOCTYPE html>
<html>
<body>
newNameOffers:<br>
<input name="newNameOffers" onChange="myFunction()" class="form-control" id="newNameOffers">
<br>
fromEmail:<br>
<input name="fromEmail" placeholder="Email" class="form-control" onChange="myFunction()" id="fromEmail">
<p>Click the button to alert the contents of the text area.</p>
<!-- <button type="button" onclick="myFunction()">Try it</button> -->
<br>
<textarea id="HEADER"></textarea>
<script>
</script>
</body>
</html>
Why do you want to put it using Laravel, You can do it in the front-end(JavaScript, JQuery, Vue.js, React) itself.

Coeigniter Url : separating variables with slashes

I am using codeigniter for developing my website.When submitting form Iam variables separated with ? and &. I want variables separated with slashes like below.
Now getting like this
http://example.com/controller/method?id=22&sd=31
I want an url like below
http://example.com/controller/method/22/31
Html Code is given below
<form action="<?php echo base_url();?>controller/method" method="get">
<div class="form-group">
<select id="id" name="id" class="selectcl form-control" data-live-search="true" title="Pick Your place" >
<option value="22" >22</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control" name="sd" id="sd" placeholder="Enter details" title="please choose">//getting values here by autocomplete
</div>
<div class="form-group">
<button type="submit" class="btn find" value="submit" ></i>Start </button>
</div>
</form>
Tried URI Routing and below
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
Please help me to find a solution.
change your form method get to post
<form id="form" action="<?php echo base_url('controller/method');?>" method="post">
add following jquery in the page.
$(function(){
$('button[type="submit"]').on('click', function(e){
e.preventDefault();
var id = $.trim($('#id').val());
var sd = $.trim($('#sd').val());
if(id.length > 0 && sd.length > 0){
var url_string = new URL($('#form').attr('action'));
window.location.href = url_string.href.concat(((url_string.href.endsWith('/')) ? '' : '/'),id,'/',encodeURIComponent(sd));
}
});
});
If you want to pass the value through URL, Try the below link:
Send form input value to URL

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.

Resources