Submitting individual entries from the same page -> with multiple forms - php

I have the following form:
#foreach($pleyers as $player)
<form action="/saveplayer" method="POST">
<div class="row">
<div class="col-6">
<p>{{$player['name']}}</p>
</div>
<div class="col-6">
<label>Score</label>
<input type="number" name="{{$player['id']}}"/>
</div>
</div>
</form>
#endforeach
MY FUNCTION
function savePlayer(Request $request){
}
How do I save the player scores individually, if the foreach generates 3 forms

Related

Why is my data not being submitted into the database

I tried to create adding/creating a new menu and saved it in the database. However, when I clicked the button, my system didn't show any error but the data is not saved in the database.
adminAddMenu.blade.php
<form>
#csrf
<div class="form-group row">
<label for="categorycode" class="col-sm-3 col-form-label">Category Code</label>
<div class="col-md-4">
<input name="category_code" value="{{old('category_code')}}" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="menutitle" class="col-sm-3 col-form-label">Menu Title</label>
<div class="col-md-4">
<input name="menu_title" value="{{old('menu_title')}}" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="menuprice" class="col-sm-3 col-form-label">Menu Price</label>
<div class="col-md-4">
<textarea name = "menu_price" value="{{old('menu_price')}}" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<div class="offset-sm-3 col-sm-9">
<button type="submit" class="btn btn-primary" href="">Submit</button>
</div>
</div>
</div>
</form>
AdminMenuController.php
public function store(Request $request)
{
$menu = new \App\Menu;
$menu->category_code = $request->category_code;
$menu->menu_title = $request->menu_title;
$menu->menu_price = $request->menu_price;
$menu->save();
Session::flash('flash_message', 'Menu is successful! added');
return redirect()->back();
}
web.php
Route::resource('/menus', 'AdminMenuController');
You don't give the form action method where your data will submit. Like you want to send your form data to store method in your controller. So you have to write follow:
<form method="POST" action="{{ route('your route name') }}">
I think you miss the action attribute of your form. Please add action to your form
<form action="{{ YOUR_ROUTER }}">
...
Please add action on form tag.
Like as-
<form action={{ route("your route write here") }} method="post">
I think now it's work's.

laravel foreach input radiobutton displayed incorrectly

I'm trying to display the options for the questions. i have the problem that when im doing it this way:
#section('content')
<div class="card">
<div class="card-header">Quiz: {{$category->name}}:</div>
<div class="card-body">
<form action="#" method="post" class="form-group">
#csrf
#foreach($questions as $key => $question)
<div class="form-group">
<label for="question">Question {{1+$key}}:</label>
<p class="card-text">{{$question->question_text}}</p>
#foreach($question->option_text as $key => $option)
<input type="radio">{{$option}}
#endforeach
</div>
#endforeach
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
i can check all radio buttons at the same time but if i put there a name for the radiobutton i can check only one of the options for the whole questions.. i think there's something strange with the foreach loop..
Info: the table "questions" has the following rows:
id, category_id, question_text, correct_answer, option_text (this is a json-field that is casted to an array)
Code from Controller:
public function store(Request $request, Category $category){
if($request->categoryTest == $category->name){
$questions = $category->question()->inRandomOrder()->get();
return view('user.test', compact('questions', 'category'));
}
}
do you have an idea how to fix this? thank you!
Try this:
#section('content')
<div class="card">
<div class="card-header">Quiz: {{$category->name}}:</div>
<div class="card-body">
<form action="#" method="post" class="form-group">
#csrf
#foreach($questions as $key => $question)
#php
$q = 1+$key
#endphp
<div class="form-group">
<label for="question">Question {{1+$key}}:</label>
<p class="card-text">{{$question->question_text}}</p>
#foreach($question->option_text as $key => $option)
<input name="radio-{{$q}}" type="radio">{{$option}}
#endforeach
</div>
#endforeach
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
This seems to be a normal behavior of the radio buttons, only one is market at time. Have you tried to use checkbox instead?
Also, if you want to send it to backend, you'll need to set a name attribute. And here is the magic: instead of using a singular name, put it as array in order to get an array on your controller.
<input type="checkbox" name="question[]" class="btn btn-primary">

How to edit one column in a row of the database in laravel

How to edit one column in a row of the database in laravel
I can't update one column of row has multiple columns by laravel
My edit :
public function edit($id)
{
$addremark=bookappoitment::findOrFail($id);
return view('admin.ManageTime.addremarks', compact('addremark'));
}
My update:
public function update(Request $request, $id)
{
$request->validate([
'Remarks'=>'required'
]);
$data=bookappoitment::find($id);
$data->Remarks = $request->get('Remarks');
$data->save();
return view('/home');
}
link to update:
Update
form:
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Remarks :</label>
<textarea class="form-control" rows="10" name="Remarks" placeholder="Remarks">{{ $addremark->Remarks }}</textarea>
</div>
<a class="btn btn-success btn-mini deleteRecord " href="{{route('BookAppoint.update',$addremark->id)}}">Update</a>
</div>
</div>
</div>
You can update 1 (or more, just add to the array) column through the DB class.
DB::table('yourTable')->where('id', $id)->update(['remarks' => $request->input('Remarks')]);
You can also do it for a model like so:
bookappoitment::where('id', $id)->update(['remarks' => $request->input('Remarks')]);
It's in the Laravel documentation here.
<form class="" action="index.html" method="{{ route('BookAppoint.update', $addremark->id) }}">
#method('PATCH')
#csrf
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Remarks">Remarks :</label>
<textarea name="remarks" rows="10" placeholder="Remarks">{{$addremarks->Remarks}}</textarea>
</div>
</div>
<input type="submit" name="submit" value="Update">
</div>
</div>
</form>

How to view data based on date range using laravel and mysql

Route:
Route::post('dategraph','Chatbot\TrackerController#dategraph');
Controller:
public function dategraph(Request $request)
{
$dategraph = DiraStatistics::all()->whereBetween('date_access', [$from, $to])->get();
$dates = $dategraph('date_access');
return view('AltHr.Chatbot.graph', compact('dates'));
}
View:
<form id="form-project" role="form" action="{{action('AltHr\Chatbot\TrackerController#dategraph')}}" autocomplete="off" method="POST">
{{csrf_field()}}
<!-- <canvas id="myChart" width="150" height="50"></canvas> -->
<div class="form-group-attached">
<div class="row">
<div class="col-lg-6">
<div class="form-group form-group-default required" >
<label>From</label>
<input type="date" class="form-control" name="from" required>
</div>
</div>
<div class="col-lg-6">
<div class="form-group form-group-default required" >
<label>To</label>
<input type="date" class="form-control" name="to">
</div>
</div>
</div>
</div>
<button class="btn alt-btn-black btn-xs alt-btn pull-right" type="submit">Next</button>
</form>
Hi guys, so im trying to view the data from the selected dates as the code ive written. But im getting an error. Did i write it correctly? or am i missing something?
You do not have a $from variable.
You need to pull out posted variables from the request.
The method get() will return a Collection of objects. You can, for example, turn it to a flat array by plucking the column and turning it toArray()
$dategraph = DiraStatistics::whereBetween(
'date_access',
[
$request->get('from'),
$request->get('to')
]
)->get();
$dates = $dategraph->pluck('date_access')->toArray();

Codeigniter : Form became weird when using select option (form_dropdown) to edit my data

It's work fine when I am inputing data using select option on CI but when I try to edit or update my data again my form became weird like this (See picture edit form)
Here my controller :
public function updateobat ($kode_obat){
if($_POST==null){
$this->load->model('a_model');
$data['hasil'] = $this->a_model->select2($kode_obat);
$data['title'] = "Data Stok Obat | Praktik Dokter Umum";
$data['tb_jenisobat'] = $this->a_model->ambil_jenisobat();
$this->template->admin('admin/edit-obat',$data);
}else{
$this->load->model('a_model');
$this->a_model->update_obat($kode_obat);
$data['title'] = "Data Stok Obat | Praktik Dokter Umum";
$data['obat'] = $this->a_model->ambil_obat();
$this->template->admin('admin/data-obat',$data);
}
}
Model
public function update_obat($kode_obat){
$nama_obat = $this->input->post('nama_obat');
$kode_jenis_obat = $this->input->post('kode_jenis_obat');
$stok= $this->input->post('stok');
$data = array('nama_obat' => $nama_obat,
'kode_jenis_obat' => $kode_jenis_obat,
'stok' => $stok,
);
$this->db->where('kode_obat',$kode_obat);
$this->db->update('tb_obat',$data);
}
public function select2($kode_obat){
return $this->db->get_where('tb_obat', array('kode_obat' => $kode_obat))->row();
}
View
<!-- Main content -->
<section class="content">
<div class="row">
<!-- left column -->
<div class="col-md-8">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Silahkan melakukan edit stok obat</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form role="form" action="<?php echo base_url().'index.php/a_controller/updateobat/'.$hasil->kode_obat; ?>" method="post">
<div class="box-body">
<div class="form-group">
<label for="exampleInputNama">Nama Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-medkit"></i></span>
<input type="text" class="form-control" name="nama_obat" id="exampleInputNama" placeholder="Nama Obat" action="<?php echo form_input('nama_obat',$hasil->nama_obat);?>">
</div>
</div>
<div class="form-group">
<label for="exampleInputJenis">Jenis Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-list"></i></span>
<select name="kode_jenis_obat" class="form-control" action="<?php echo form_dropdown('kode_jenis_obat',$hasil->kode_jenis_obat);?>">
<option value="none" selected="selected">Pilih Jenis Obat</option>
<!-----Displaying fetched cities in options using foreach loop ---->
<?php foreach($tb_jenisobat as $jenisobat):?>
<option value="<?php echo $jenisobat->kode_jenis_obat?>"><?php echo $jenisobat->nama_jenis_obat?></option>
<?php endforeach;?>
</select>
</div>
</div>
<div class="form-group">
<label for="exampleInputUsername">Stok Obat</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-list"></i></span>
<input type="text" class="form-control" name="stok" id="exampleInputUsername" placeholder="Stok" action="<?php echo form_input('stok',$hasil->stok);?>">
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
<!-- /.box -->
</div>
</div>
<!-- /.row -->
</section>
I am using form_dropdown for my select option
I think there is something wrong with my controller syntax?
$data['tb_jenisobat'] = $this->a_model->ambil_jenisobat();
But I am not sure enough, can someone help me?
Picture : edit form

Categories