Display none not working Laravel - php

I have a condition, if active is checked then it'll display a text form.
This is my view form code:
<div class="form-group">
<label>Active:</label>
{{ Form::checkbox('active', 0,null, array('id' =>'active_check','class' => 'active_check')) }}
</div>
<div id ="join_date" class="form-group" style="display: none;">
<label>Join Date:</label>
<div class="input-group">
{!! Form::text('join_date', null, array('class' => 'form-control datetimepicker' )) !!}
</div>
</div>
Here is my jQuery toggle and check condition code so far:
$('#active_check').click(function() {
$("#join_date").toggle(this.checked);
$('#join_date').find('input[type="text"], textarea').val('');
});
var activeCheck = $('checkbox[name="active_check"]');
function checkActive(select) {
if (select.val() == 0) {
$('#join_date').hide();
} else {
$('#join_date').show();
}
}
checkActive(activeCheck);
The issue is when it is first loaded the join_date form doesn't hide. I need to toggle the checkbox to hide it. Any idea?

Try this simple logic:
if($('#active_check').is(':not(":checked")')) {//test if active_check is not checked
$('#join_date').hide();//hide it
}
$('#active_check').click(function() {
$("#join_date").toggle();//toggle it
});

Related

Laravel how to echo selected a value in a dropdown using blade when updating a specific resource

I want to echo the selected value when I edit a specific resource in my table. When I edit the specific resource it should display the current data in my dropdown but in the real scenario it displays the first one on the list which is wrong. So how can I echo the selected value in the options of the dropdown using blade in laravel?
Here is some sample of my code in the view below
<!-- Shows Field -->
<div class="form-group col-sm-6">
{!! Form::label('show_id', 'Shows:') !!}
{!! Form::select('show_id', $shows, $shows, ['class' => 'form-control input-md','required'])!!}
</div>
{{-- {{ $channelshows->channel->name }} --}}
<!-- Client Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('channel_id', 'Channels:') !!}
{!! Form::select('channel_id', $channel, $channel, ['class' => 'form-control input-md','required'])!!}
</div>
<!-- Submit Field -->
<div class="form-group col-sm-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
Cancel
</div>
and here is the code in my controller below.
public function edit($id)
{
$channelshows = $this->channelshowsRepository->findWithoutFail($id);
$shows = Show::pluck('name', 'id');
$channel = Channel::pluck('name', 'id');
if (empty($channelshows)) {
Flash::error('Assigned show not found');
return redirect(route('admin.channelshows.index'));
}
return view('admin-dashboard.channelshows.edit', compact('shows', $shows), compact('channel', $channel))->with('channelshows', $channelshows);
}
Everything here works fine even if I updated the specific resource. I just want to auto populate or select the current value of the resource that I will update because when I edit the specific resource it shows the first one on the list.
Am I going to use an #if statement in blade? But how can I do it using the blade template in my select form. Can somebody help me?
Appreciate if someone can help.
Thanks in advance.
Here is an example:
Open form:
{{ Form::model($service, array('route' => array('services.update', $service->id))) }}
Select form field:
<div class="form-group">
{{ Form::label('Related Agreement') }}
{{ Form::select('agreement', $agreementsList, null, array('class'=>'form-control', 'placeholder'=>'Please select ...')) }}
</div>
In Controller:
$agreementsList = Agreement::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)->pluck('name', 'id');
(Include this when passing data to your view)

Laravel post form error

I'm having a problem for the first time when i submit a form.
When i submit the form it doesn't go to post route and i don't know why.
my post route is that:
Route::post('/app/add-new-db', function()
{
$rules = array(
'name' => 'required|min:4',
'file' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
// get the error messages from the validator
$messages = $validator->messages();
// redirect our user back to the form with the errors from the validator
return Redirect::to('app/add-new-db')
->withErrors($validator);
}
else
{
$name = Input::get('name');
$fname = pathinfo(Input::file('file')->getClientOriginalName(), PATHINFO_FILENAME);
$fext = Input::file('file')->getClientOriginalExtension();
echo $fname.$fext;
//Input::file('file')->move(base_path() . '/public/dbs/', $fname.$fext);
/*
DB::connection('mysql')->insert('insert into databases (name, logotipo) values (?, ?)',
[$name, $fname.$fext]);
*/
//return Redirect::to('app/settings/');
}
});
And my html:
<div class="mdl-cell mdl-cell--12-col content">
{!! Form::open(['url'=>'app/add-new-db', 'files'=>true]) !!}
<div class="mdl-grid no-verticall">
<div class="mdl-cell mdl-cell--12-col">
<h4>Adicionar Base de Dados</h4>
<div class="divider"></div>
</div>
<div class="mdl-cell mdl-cell--6-col">
<div class="form-group">
{!! Form::label('name', 'Nome: ') !!}
{!! Form::text('name', null, ['id'=> 'name', 'class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Logotipo:') !!}
{!! Form::file('image', ['class'=>'form-control']) !!}
#if ($errors->has('image'))
<span class="error">{{ $errors->first('image') }}</span>
#endIf
</div>
<div class="form-group">
{!! Form::submit('Adicionar', ['id'=>'add-new-db', 'class'=>'btn btn-default']) !!}
<p class="text-success"><?php echo Session::get('success'); ?></p>
</div>
</div>
</div>
{!! Form::close() !!}
</div>
I'm loading this from from a jquery get:
function addDB()
{
$( "#settings-new-db" ).click(function(e)
{
$.get('/app/add-new-db', function(response)
{
$('.content-settings').html("");
$('.content-settings').html(response);
componentHandler.upgradeDom();
});
e.preventDefault();
});
}
When i try to submit the form i'm getting 302 found in network console from chrome.
I'm doing forms at this way and it is happens for the first time. Anyone can help me?
Thanks
Fix the name attribute for your image upload field. You refer it as image in the form but you are trying to fetch it as file in your controller.

Laravel (php) save search options

So i am using laravel 5.2 and i am building a search form, there is multiple search queries and options.
I have been wracking my head trying to think of ways to save the users search and auto fill the form when they visit the search page OR even auto search when they visit it.
So far I have been setting the GET's as variables and auto fill in the text fields but that only lasts for the time they are on the page which is not what i am after,
I have thought about saving the gets into the database as separate fields and when they search and pulling them when they are on the page and filling in each field but then how do I auto search?
Or do I save it as 1 string as the get from the URL eg, u=username&p=postcode
and set the route up to check if the user has a search saved and if they visit /search they get redirected to the url+ get options saved in the db. (no clue how to do this either.)
Now the hardest part even with the gets I have fields that are multiple select like genders and sort by. How do you auto fill in those?
Below is my code for just the form
<?php
if(isset($_GET['u'])) { $username = $_GET['u']; } else { $username = ''; }
if(isset($_GET['p'])) { $postcode = $_GET['p']; } else { $postcode = ''; }
if(isset($_GET['o'])) { $orderby = $_GET['o']; } else { $orderby = ''; }
?>
{{ Form::open(['method' => 'GET']) }}
<div id="filter-panel" class="filter-panel collapse in">
<div class="panel panel-default">
<div class="panel-body">
<form class="form-inline" role="form">
<div class="row">
<div class="col-xs-6 col-sm-2">
{{ Form::input('search', 'u', $username, ['class' => 'form-control input-sm', 'placeholder' => 'username...']) }}
</div><!-- form group [search] -->
<div class="col-xs-6 col-sm-2">
{{ Form::input('search', 'p', $postcode, ['class' => 'form-control input-sm', 'placeholder' => 'postcode...']) }}
</div><!-- form group [search] -->
<div class="col-xs-6 col-sm-3">
<select id="attractedToGender" multiple="multiple" name="g[]" class="input-sm" >
<option value="1">Females</option>
<option value="2">Males</option>
<option value="3">Others</option>
</select>
</div><!--END form-group col-xs-6 col-sm-4-->
<div class="col-xs-12 col-sm-3">
<select id="pref-orderby" class="input-sm" name="o[]" multiple="multiple">
<option value="1">Photo Only</option>
<option value="2">User Type</option>
<option value="3">Last Online</option>
</select>
</div> <!-- form group [order by] -->
<div class="col-xs-6 col-sm-2 pull-right text-right">
{{Form::button('<i class="glyphicon glyphicon-search"></i> Search', array('type' => 'submit', 'class' => 'btn btn-primary btn-sm', 'style' => 'margin:0;'))}}
</div><!--END form-group col-xs-6-->
</div>
</form>
</div>
</div>
</div>
{{ Form::close() }}
There are few ways to do that. The best ways are dedicated model and saving data as JSON object.
Model. You can create dedicated table with User has many Searches relationship. And just save object into this table.
JSON. You can serialize object with ->toJson() method and persist in in a users table. Just add json type column:
$table->json('options');
In both cases you're working directly with the object.

how to Select and Clear all check-boxes using Laravel

I have written a code that sends email to selected people using check-box, now I have to add select all and clear all button so that all the check-boxes should get selected/dis-selected.
Here is my code:
My controller:
public function display(){
$data = Input::get('agree');
$count = count ($data);
$this->contact( $data );
return view('clientinfo.display', compact('data','count'));
}
My View:
#extends('app')
#section('content')
<h1>Welcome! Send e-mail to selected people.</h1>
<hr>
{!! Form::open(array('action' => 'ClientsController#display','method' => 'GET'))!!}
#foreach($clients as $client)
{!! Form::checkbox("agree[]", $client->email, null,['id' => $client->email]) !!}
<article>
{{ $client->user_name }}
{{ $client->email }}
</article>
#endforeach
{!! Form::submit('Send Mail',['class' => 'btn btn-primary form-control']) !!}
{!! Form::close() !!}
<br/>
#include('clientinfo.newmember')
#stop
if i add a button in my view naming select-all and clear-all how do i relate them, to select all the members within list?
You could give the checkboxes a class and use jquery.
You also need a checkbox that says "select all" with an ID.
$(function() {
$('#selectAll').click(function() {
if ($(this).prop('checked')) {
$('.your_checkbox_class').prop('checked', true);
} else {
$('.your_checkbox_class').prop('checked', false);
}
});
});
PS: Code is not tested
I have made changes to my code and it worked using JavaScript.
Here is my code:
#extends('app')
#section('content')
<h1>Welcome! Send e-mail to selected people.</h1>
<hr>
{!! Form::open(array('action' => 'ClientsController#display','method' => 'GET','name'=>'f1' , 'id'=>'form_id'))!!}
<br/>
#foreach($clients as $client)
<div class="form-group">
{!! Form::checkbox("agree[]", $client->email, null,['id' => $client->email], ['class' => 'questionCheckBox']), $client->email !!}
<br/>
</div>
#endforeach
<div class="form-group">
{!! Form::submit('Send Mail',['class' => 'btn btn-primary form-control']) !!}
</div>
<div class="form-group">
{!! Form::button('Select All',['class' => 'btn btn-info form-control','onClick'=>'select_all("agree", "1")']) !!}
</div>
<div class="form-group">
{!! Form::button('Clear All',['class' => 'btn btn-danger form-control','onClick'=>'select_all("agree", "0")']) !!}
</div>
{!! Form::close() !!}
#stop
#endsection
And in my master file,I have added a JavaScript:
<script type="text/javascript">
var formblock;
var forminputs;
function prepare() {
formblock= document.getElementById('form_id');
forminputs = formblock.getElementsByTagName('input');
}
function select_all(name, value) {
for (i = 0; i < forminputs.length; i++) {
// regex here to check name attribute
var regex = new RegExp(name, "i");
if (regex.test(forminputs[i].getAttribute('name'))) {
if (value == '1') {
forminputs[i].checked = true;
} else {
forminputs[i].checked = false;
}
}
}
}
if (window.addEventListener) {
window.addEventListener("load", prepare, false);
} else if (window.attachEvent) {
window.attachEvent("onload", prepare)
} else if (document.getElementById) {
window.onload = prepare;
}
</script>
Earlier i was doing this using Jquery, that was not working, then i tried using JavaScript and it worked.
I have tried various ways to add jquery in my code which are given in answers of stackoverflow.com, but nothing worked in laravel. If anyone know how to use jquery in laravel, please leave a comment.

Using a datepicker with Phalcon Volt template engine

I am pretty new to the Phalcon PHP framework and the Volt template engine.
So far I really like it though.
One thing that I can't figure out however, is how to implement a date picker to a date field.
I have a date field defined as below, but rather than having to manually input a date I would like the user to select a date from a date picker.
<?php echo Tag::dateField(array("finishdate", "size" => 8, "maxlength" => 8, "type" => "date")) ?>
I thought that maybe it would automatically get a date picker if it was defined as a date field, or maybe there was an option somewhere, but I have looked all over internet and I can't find anything.
I would be very grateful if someone knew how to solve this?
I am also trying to figure out the Volt textArea and I have problems finding any information regarding this as well. I can't seem to get the text area to show as a text box. There just seems to be very little information from users regarding Phalcon Volt. Is no one using this?
I did it! I used datepicker (https://jqueryui.com/datepicker/). I share with you the code.
Form:
$fecha = new Text('fecha_final');
$fecha->setLabel('Fecha Final');
$fecha->addValidators(array(
new PresenceOf(array(
'message' => 'Por favor pon una fecha límite'
))
));
$this->add($fecha);
Controller:
public function nuevakeywordAction(){
$auth = $this->session->get('auth');
$permiso = $auth['active'];
if($permiso!='A'){return $this->forward('servicios/index');}
$form = new NuevakeywordForm;
if ($this->request->isPost()) {
$trackeable = $this->request->getPost('trackeable', array('string', 'striptags'));
$idcliente = $this->request->getPost('idcliente');
$fecha = $this->request->getPost('fecha');
$keyword = new Keyword();
$keyword->trackeable = $trackeable;
$keyword->cliente_idcliente = $idcliente;
$keyword->fecha_inicial = new Phalcon\Db\RawValue('now()');
$keyword->fecha_final = $fecha;
if ($keyword->save() == false) {
foreach ($keyword->getMessages() as $message) {
$this->flash->error((string) $message);
}
} else {
$this->tag->setDefault('trackeable', '');
$this->tag->setDefault('idcliente', '');
$this->tag->setDefault('fecha', '');
$this->flash->success('Keyword agregada correctamente');
return $this->forward('admin/verkeywords');
}
}
$this->view->form = $form;
}
View:
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<section class="container animated fadeInUp">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div id="login-wrapper">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
Registrar Nueva Keyword
</h3>
</div>
<div class="panel-body">
{{ form('admin/nuevakeyword/', 'id': 'nuevakeywordForm', 'onbeforesubmit': 'return false') }}
<fieldset>
<div class="control-group">
{{ form.label('trackeable', ['class': 'control-label']) }}
<div class="controls">
{{ form.render('trackeable', ['class': 'form-control']) }}
</div>
</div>
<div class="control-group">
{{ form.label('idcliente', ['class': 'control-label']) }}
<div class="controls">
{{ form.render('idcliente', ['class': 'form-control']) }}
</div>
</div>
<div class="control-group">
{{ form.label('fecha_final', ['class': 'control-label']) }}
<div id="datepicker" class="controls">
{{ form.render('fecha_final', ['class': 'form-control']) }}
</div>
</div>
<div class="form-actions">
{{ submit_button('Insertar', 'class': 'btn btn-primary', 'onclick': 'return SignUp.validate();') }}
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
$(function() {
$("#fecha_final").datepicker();
});
</script>
I hope it will help you.
The date field merely uses HTML5's date field. If you want a date picker, you must use a Javascript date picker library.
As for text areas and text fields, you should use the function text_field() to create a text field, and text_area() to create a text area. The list of functions you can use are in the Volt – Using tag helpers documentation page.

Categories