I'm working on a reporting page using php Laravel framework. I select the options from the comboboxes, these values are sent to the controller to replace them in some queries used to display the reports.
When I click the submit button, the page refreshes and the name shown is the last element of the dropdown.
My Controller:
$negocioDive = DB::table('ma_leads_reporte')
->selectRaw('NEGOCIO, year(FEC_CREACION) as AÑO')
->groupBy('NEGOCIO', 'AÑO')
->get()->toArray();
$selectFecha = DB::table('param_fecha')
->selectRaw('mesNum, mesNom')
->groupBy('mesNum', 'mesNom')
->get()->toArray();
$año = $request ->input('año');
$mes = $request -> input('mes');
$negocio = $request -> input('negocio');
//Solicitud de Cotización versus mes anterior
$mesAnt = DB::table('ma_leads_reporte')
->selectRaw('count(*) as C, day(FEC_CREACION) AS DIA, monthname(FEC_CREACION) AS MES')
->whereMonth('date_identified', '=', $mes-1)
->whereYear('date_identified', '=', $año)
->groupBy('MES', 'DIA')
->get()->toArray();
$vscont = array_column($mesAnt, 'C');
$antMes = array_column($mesAnt, 'MES');
$dia = array_column($mesAnt, 'DIA');
My View:
<div class="container">
<div class="row justify-content-center">
<div class="content mt-3">
<div class="animated fadeIn">
<div class="col-lg-8">
<div class="card">
<div class="card-header">
<form action="{{route('reportes')}}" method="GET" id="fecha">
{{Form::label('', 'Año')}}
<select id="año" name="año">
#foreach($negocioDive as
$negocioD)
<option value="{{$negocioD->AÑO}}"
selected="selected">{{$negocioD->AÑO}}</option>
#endforeach
</select>
{{Form::label('', 'Mes')}}
<select id="mes" name="mes">
#foreach($selectFecha as $fecha)
<option value="{{$fecha->mesNum}}" name="{{$fecha->mesNom}}">{{$fecha->mesNom}}</option>
#endforeach
</select>
{{Form::label('', 'Negocio')}}
<select id="negocio" name="negocio" >
#foreach($negocioDive as $negocioD)
<option value="{{$negocioD->NEGOCIO}}" selected="selected">{{$negocioD->NEGOCIO}}</option>
#endforeach
</select>
<button type="submit"class="btn btn-default" id="filter">Filtrar
<i class="fa fa-filter"></i>
</button>
</form>
<?php if(isset($_GET['año']) && isset($_GET['mes']) && isset($_GET['negocio'])){
echo "<h4> Año: $año | Mes: {$fecha->mesNom} | Negocio: $negocio </h4>";
}?><!--
<p id="mesJS"></p> -->
</div>
</div>
</div>
After receiving the request variables, it should show the correct values instead of the last index of the dropdown.
What can I do?
dropdown options:
After submit:
The reason it is showing the last index of the dropdown is that you are looping on your various items and creating your options within each select box with 'selected' overwritten every time all the way to the last one in the loop.
To fix this, you'll need to add some type of conditional on the option box to tell it if the loop item is the correct selected one. If the new model has the key within this loop, then, and only then, make it selected. Something like this (You'll need to make this code your own):
#foreach($negocioDive as $id=>name)
<option value="{{$id}}" {{$theThingYouAreSelecting->id === $id?"selected":''}}>$name</option>
#endforeach
NOTE - I'm assuming you are sending a key->value array to the foreach as this is what the option expects - the key would be the id and the value would be the name for the item.
Once you get this working, I strongly recommend looking at LaravelCollective HTML - it binds the model to the form and automates a huge chunk of the conditionals.
Related
I am a beginner in Laravel. I want to implement a feature that:
I want to use Laravel 8 and PHP:
Select a part of the data in a table (via a select)
how to get the id selected in the select :
-> get the identifier selected in the option tag in order to use it in an sql query that combines selection and insertion
Copy the data and insert it in another table (via an add button)
how to recover the id present in the url of the page
Display the data inserted in a table
no problem
<select name="id_segment" class="form-control">
<?php
$req1 = DB::select("SELECT id_segment, nom_segment FROM `segements` ");
foreach ($req1 as $re1 => $r1) {
?>
<option name="id_segment" value="{{ $r1->id_segment }}"> {{ $r1->id_segment }}
{{ $r1->nom_segment }}</option>
<?php
} ?>
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary" id="AjoutSegment">Ajouter</button>
<a class="btn btn-primary" href="{{ route('groupecampagnes.index') }}"> Back</a>
</div>
I have already tried with $_POST and $_GET while making a var_dump, but I have seen that some people say that it does not work, me it gave me the display of an error with the id that I seek to recover precisely, whereas it should just display it to me on the page to see if I recover it well and truly. The var_dump is not present in the code because I removed it in the meantime. After I tried various solutions found on the net, which have me display as error for some of them that I had to do with a std class object.
Assuming your code is in a file called page and stored in the views directory (resources/views/page.blade.php), you could go to your controller and pass the needed data to your view like this:
$segments = DB::select("SELECT id_segment, nom_segment FROM `segements` ")->get();
return view('page', compact('segments'))
Then you can use it in your view:
<select name="id_segment" class="form-control">
#foreach ($segments as $r1)
<option name="id_segment" value="{{ $r1->id_segment }}"> {{ $r1->id_segment }} {{ $r1->nom_segment }}</option>
#endforeach
</select>
I am trying to get two values. For one, it is working but for another one it is not. Here is my code:
<div class="product-price">
<div class="lefts-column">
<span>₹</span>
<span id="priceval"> {{$weights[0]->price}} </span>
<input type="hidden" class="priceval1" value="{{$weights[0]->id}}" id="pro_ids">
</div>
<div class="choose-weight">
<!-- original reads <select onchange="yes(this.value,this.data)"> -->
<select onchange="yes(this.value,this.name)">
#foreach($weights as $weight)
<option id="red" name="{{$weight->id}}" data-id="{{$weight->id}}"
class="weight" value="{{$weight->price}}">
{{$weight->weight}}
</option>
#endforeach
</select>
</div>
</div>
<!--jquery function-->
<script>
function yes(pid , uid)
{
$('#priceval').html(pid);
alert(uid);
//$('#pro_ids').val(uid);
}
</script>
Now I use alert to check if the value is retrieve or not (alert shows nothing then).
this.value is working and this.name is not. Is there any way to resolve it?
How to get data by selecting the drop-down in CodeIgniter.
Below is my code it does not work. The below code is not working properly when I selecting an option in the drop down and when I submit page appearing empty. when click please help me to get data. I am doing it in CodeIgniter. I want to get data from the database . I think the code is perfect am new to CodeIgniter the below code is not working properly the page appearing empty when click please help me to get data. I am doing it in CodeIgniter. I want to get data from the database . I think the code is perfect am new to CodeIgniter.
view:
<?php echo form_open('super_admin/believers_controller/view_believerr/' , array('class' => 'form-horizontal validatable', 'enctype' => 'multipart/form-data') ); ?>
<div class="form-group">
<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> District Id</label>
<div class="col-sm-9">
<select name="report_type" >
<option >Select Id</option>
<option value="believers">Believers</option>
<option value="staff">staff</option>
<option value="staff">committe members</option>
</select>
</div>
</div>
<div class="clearfix form-actions">
<div class="col-md-offset-3 col-md-9">
<input type="submit" name="Submit" class="btn btn-info" >
controller:
<?php
function view_believerr()
{
$report_type = $this->input->get('report_type');
if($report_type=='believers'){
$this->load->model('believers/believers_model');
$page_data['h']=$this->believers_model->view_believer();
$page_data['page_name'] = 'believers/view_believer';
$this->load->view('index', $page_data);
}
}
?>
model:
<?php
function view_believer()
{
$query = $this->db->get('tbl_believers');
return $query;
}
?>
I want to get data from database . I think the code is perfect am new to codeigniter the below code is not working properly the page appearing empty when click please help me to get data. I am doing it in codeigniter. I want to get data from database . I think the code is perfect am new to codeignite
Use $this->input->post('report_type');
I am making a loan system for books and I have a profile page that displays the loans you have taken out, underneath each one is a button that takes you to a form where you fill out the condition of the book, under that is another button that will send the data to a returns table in my database, I want to delete the loan in the loan table because if not then the loan keeps displaying in the profile page and the user can return the same book as many times as they want. any help would be greatly appreciated.
thanks.
profile.blade.php
<?php
use App\Requests;
use App\Loan;
$request = Requests::where('userid', auth()->user()->userid)->get();
?>
#extends('layouts.app')
#section('content')
<div class="container">
<div class="panel panel-default">
<div class="panel-heading"><h1>Profile: {{ Auth::user()->f_name }}</h1></div>
<div class="panel-body">
<div class="container">
<h3>Requests</h3>
#foreach ($request as $request)
<ul>
<h4>Title: {{$request->r_title}}</h4>
<h4>Author: {{$request->r_author}}</h4>
<h4>Year: {{$request->r_year}}</h4>
<h4>Condition: {{$request->r_condition}}</h4>
<br/>
</ul>
#endforeach
</div>
<div class="container">
<h3>Loans</h3>
#foreach ($loan as $loan)
<ul>
<h4>Title: {{$loan->title}}</h4>
<h4>Start Date: {{$loan->startdate}}</h4>
<h4>Due Date: {{$loan->duedate}}</h4>
<br/>
<form action="{{url('returnform/'.$loan->loanid)}}" method="GET">
<input type="submit" name="returnbtn" value="returnform">
</form>
</ul>
#endforeach
</div>
</div>
</div>
</div>
#endsection
returnform.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="panel panel-default">
<div class="panel-heading"><h1>Return Book</h1></div>
<div class="panel-body">
<form action="{{url('returns')}}" method="POST">
{{ csrf_field() }}
<div>
<input type="hidden" name="lid" value="{{$loan->loanid}}" id="lid">
<input type="hidden" name="re_bookid" value="{{$loan->bookid}}" id="re_bookid">
<input type="hidden" name="re_title" value="{{$loan->title}}" id="re_title">
<input type="hidden" name="re_ddate" value="{{$loan->duedate}}" id="re_ddate">
</div>
<div>
<label for="title"><h4>Enter the books condition: </h4></label>
<select name ="re_condition" value="{{old('condition')}}">
<option value="mint">Mint</option>
<option value="good">Good</option>
<option value="fair">Fair</option>
<option value="poor">Poor</option>
</select>
</div>
<input type="submit" name="btn" value="returns">
</form>
</div>
</div>
</div>
#endsection
BookController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Book;
use App\Returns;
use App\Requests;
use App\Loan;
use Carbon\Carbon;
class BookController extends Controller
{
......
function returnform($loanid)
{
$loan = Loan::find($loanid);
return view('book/returnform',['loan' => $loan]);
}
function returns(Request $request, $loanid)
{
$return = new Returns();
$return->userid = auth()->user()->userid;
$return->f_name = auth()->user()->f_name;
$return->l_name = auth()->user()->l_name;
$return->bookid = $request->re_bookid;
$return->title=$request->re_title;
$return->condition=$request->re_condition;
$return->returndate= Carbon::now();
$return->duedate=$request->re_ddate;
$return->save();
return redirect('profile');
}
routes
....
Route::get('returnform/{loanid}', 'BookController#returnform');
Route::post('returns', 'BookController#returns');
To delete a row in a table use the following code
function deletereturns(Request $request, $loanid) {
$return = Return::find($loanid);
$return->delete();
// return to some place
}
Add a route and its done.
But i suggest you to create a new column in the table with boolean and set default false.
Each time a user tries to delete a loan just change false to true.
When you list the loan use a where conditions to exclude all the deleted ones.
This helps you to know who all deleted the loans, which may help in any way
The $loanid parameter won't work in your method since you don't pass any value to it. You are however setting the loan id in a hidden form field. So do this to delete the loan using the loan id.
Loan::destroy($request->lid);
You should also add some check when a user returns the book. Check if the loan with the loan id exists and perform a check to see if the logged in user and the loaned user match. Because with your code right now, any user can return a book loaned by any other user.
I'm currently new on Laravel and trying to develop my first project. I have this MethodNotAllowedHttpException in RouteCollection.php line 218 error during my development for inserting data into database. I have searched both Google & Stackoverflow for solutions but non are related to my current problem and some of them way too complex for this simple problem (I think so...).
I have my form in my checklist page:-
<form action="{{url('addchecklist')}}" method="POST">
{{ csrf_field() }}
<div class="row">
<div class="col-sm-12">
<div class="text-left">
<input type="hidden" name="schmFK" value="{{$id}}">
<div class="col-sm-6">
<h4>
<label>Section</label>
<select class="selectpicker form-control" data-live-search="true" name="sctionPK">
<option selected>Select the Section</option>
#foreach ($sction as $key=>$slct1)
<option value="{{$slct1->ssctionPK}}">{{strtoupper($slct1->ssctionName)}}</option>
#endforeach
</select>
</h4>
</div>
<div class="col-sm-2">
<button type="button" data-toggle="modal" data-target=".bs-example-modal-lg" class="btn btn-primary btn-sm" style="margin-top:33px; padding-top:7px; padding-bottom:7px;">Add Section</button>
</div>
<div class="col-sm-4">
<h4>
<label>Severity</label>
<select class="selectpicker form-control" name="svrityPK">
<option selected>Select the Severity</option>
#foreach ($svrity as $key=>$slct2)
<option value="{{$slct2->severityPK}}">{{strtoupper($slct2->severityName)}}</option>
#endforeach
</select>
</h4>
</div>
<div class="col-sm-12">
<h4>
<label>Question</label>
<input class="form-control" type="text" placeholder="Question" name="question">
</h4>
</div>
<div class="col-sm-6">
#include('widgets.button', array('class'=>'primary btnaddstd', 'size'=>'lg', 'type'=>'submit', 'value'=>'Add Checklist'))
</div>
</div>
</div>
</div>
</form>
Then I have this route for inserting data from the form into database:-
Route::post('/addchecklist', function (Request $request){
// Create instance to store record
$scheme = new App\checklists;
$scheme->schmFK = $request->schmFK;
$scheme->schSectionFK = $request->sctionPK;
$scheme->severityFK = $request->svrityPK;
$scheme->clQuestion = $request->question;
$scheme->save(); // save the input
// Sort all records descending to retrieve the newest added record
$input = App\checklists::orderBy('cklistPK','desc')->first();
// Set search field variable default value of null
$src = isset($src) ? $src : null;
// Get Checklist reference from cklists_stddetails with the designated ID
$chkstd = App\Cklists_stddetail::where('cklistFK', $input->cklistPK)
->join('stddetails', 'stdDtlFK', '=', 'stddetails.sdtlPK')
->get();
// Get the newest stored record
$chcklst = App\checklists::where('cklistPK', $input->cklistPK)->firstOrFail();
// Get all data from table 'stddetails'
$stddetail = App\stddetails::all();
// Get all data from table 'standards'
$stndrd = App\standard::all();
// Get all data from table 'sections'
$sction = App\Section::all();
// Redirect to 'addref.blade' page with the newest added record
return redirect('addref/'.$input->cklistPK)
->with('src', $src)
->with('chkstd', $chkstd)
->with('id',$input->cklistPK)
->with('schmid', $request->schmFK)
->with('chcklst', $chcklst)
->with('stddetail', $stddetail)
->with('stndrd', $stndrd)
->with('sction', $sction);
});
My scenario is this, I have a form for user to input data in it. Then when the data is saved, they will be redirected to the page of that data to do something there. The data is successfully saved in the database but the redirection to the designated page (addref.blade) with the newest record ID return error:-
But the URL goes where I wanted it to go (means the URL is right):-
As you can see, the usual solution from the net that I found are:-
Make sure both method from routes and the form is the same, and mine it is:-
method="POST"
Route::post
Make sure the URL routes can recognize the form's action URL, and mine it is:-
<form action="{{url('addchecklist')}}" method="POST">
Route::post('/addchecklist', function (Request $request)
Include CSRF token field in the form, and mine it have been included:-
<form action="{{url('addchecklist')}}" method="POST">
{{ csrf_field() }}
I have tried those simple solution provided on the net and nothing is helpful enough. I'm still wondering what else I have missed and hoped that anyone here can assist on solving my issue.
I think the error is that you have a redirect which you have not registered in your routes or web.php file.
Sample redirect:
Route::post('/addchecklist', function (Request $request){
//some post process here...
return redirect('addref/'.$input->cklistPK)
->with('src', $src)
->with('chkstd', $chkstd)
->with('id',$input->cklistPK)
->with('schmid', $request->schmFK)
->with('chcklst', $chcklst)
->with('stddetail', $stddetail)
->with('stndrd', $stndrd)
->with('sction', $sction);
});
Route::get('addref/{id}', function(Request $request){
//show the blade.php with data
});
Can you please write :
url('/addchecklist')
instead of :
url('addchecklist')
and then print_r('in');
and die; and check what you get.
Route::post('/addchecklist', function (Request $request){
print_r('in');
die;
});