Form inputs with Datatables - php

I have a Datatable with form inputs.
This is how I create it
public function getpurchasedata(Request $request)
{
if($request->get('orderID')){
$id = $request->orderID;
$orderNo = PurchaseOrder::find($id);
$order_items = PurchaseOrderItem::where('order_id',$orderNo->order_id)
->with('material')->get();
return DataTables::of($order_items)
->addColumn('name',function($order_items){
$name = $order_items->material;
return $name['name'];
})->addColumn ('expdate',function($order_items){
return '<input type ="date" id="expdate"
name="expdate" class="form-control">';
})->addColumn('action', function ($order_items) {
$buttons ='<button id="edit"
class="btn btn-info btn-sm">
<i class="far fa-edit"></i>
</button>
<button id="remove"
class="btn btn-danger btn-sm">
<i class="far fa-trash-alt"></i>
</button>';
return $buttons;
})->rawColumns(['expdate','action'])->make(true);
}
}
I use the following method to pass Data table values to the controller
grn_tableData:grn_table.data().toArray(),
but when i submit data using an ajax function i get html markups instead on input values for the expdate column. how can i get the real value ? explain me

You can add a hidden column next to your expdate column. Doing this, you can get the real value after form submission.

Related

Show search results on another view with CakePHP

I'm working with CakePHP now and I need to do a search with one view and then send the data to another action on the same controller, that will trigger another view with the results of that search. The thing is, on the search screen, there's also a table showing some data of the same model, and I believe that's one big problem.
So right now, it's here what I got:
public function busca() {
$emergency = TableRegistry::get('EmergencySheets');
$manufacturers = TableRegistry::get('Manufacturers');
$data = $this->request->is('get') ? $this->request->query : $this->request->getData();
$query = $emergency->find()
->select(['id', 'data_atualizacao_fabricante', 'tarja', 'manufacturer_id', 'nome_comercial'])
->where('EmergencySheets.data_atualizacao_fabricante')
->order(['data_atualizacao_fabricante'=>'DESC'])
->limit(7);
$manufacturer_query = $manufacturers->find()
->select(['id','nome'])
->where($query->manufacturer_id = 'id');
$manufacturer = $manufacturer_query->toArray();
$sheets = $query->toArray();
$this->set('manufacturers', $manufacturer);
$this->set('sheets', $sheets);
if($data){
return $this->redirect(['action' => 'ficha' , $data]);
}else{
return $this->redirect(['action' => 'busca404']);
}
}
How can I handle this?
Thank you all!
Edit:
Forgot to mention, but the $data variable always come empty on the form, even when I type something on the form input. Here's the view code, too!
<section class="search-section">
<div class="container px-0">
<div class="search-wrapper">
<div class="search-title">
<h2><span>Quais produtos</span>você vai transportar?</h2>
<p><span>Pesquise pelos produtos no campo de busca</span>
ou clique nas letras ao lado.
</p>
</div>
<div class="search-bar">
<?=$this->Form->create()?>
<div class="ml-5 bar">
<input type="text" placeholder="Procure várias fichas de uma só vez" class="formcontrol tip"
data-toggle="tooltip" data-placement="top">
<span class="removeClick"><i class="fas fa-times-circle fa-2x"></i></span>
<button type="submit" class="btn"><i class="fa fa-search fa-2x"></i></button>
</div>
<?=$this->Form->end()?>
<div class="ml-5 alfabeto text-center">
<button href="#A">A</button> <button href="#B">B</button> <button href="#C">C</button> <button href="#D">D</button>
<button href="#E">E</button> <button href="#F">F</button> <button href="#G">G</button> <button href="#H">H</button>
<button href="#I">I</button> <button href="#J">J</button> <button href="#K">K</button> <button href="#L">L</button>
<button href="#M">M</button> <button href="#N">N</button> <button href="#O">O</button> <button href="#P">P</button>
<button href="#Q">Q</button> <button href="#R">R</button> <button href="#S">S</button> <button href="#T">T</button>
<button href="#U">U</button> <button href="#V">V</button> <button href="#W">W</button> <button href="#X">X</button>
<button href="#Y">Y</button> <button href="#Z">Z</button><button href="#0-9">0-9</button>
</div>
</div>
</div>
</div>
I would use the same function and template for the search form and results. Eliminate the whole if section with redirects, only do the search if there is data to search on, and change your view to check whether there are results to display. Something like this:
$data = $this->request->is('get') ? $this->request->getQueryParams() : $this->request->getData();
if (!empty($data)) {
// Do your searches using $data here, set the results for the view
$this->set('results', $results);
}
Then in your template, you have it like you've shown, but add a section with
if (isset($results)):
// Display your search results here
endif;
Please consider that you are puting code after the line:
return $this->redirect(['action' => 'busca404']);
}
In that case all those lines wont be executed in any case because you are forcing redirects either if the request is "get" or not. So all that code wont be executed.
I think that you need to define the conditions to redirect to the "ficha" action and in which conditions remain in to the "busca" action

Laravel: How to store href id in the new variable and display or use in the input field?

I want To Store the current user $emp->id from href , and use in the input value as like in my code that is written below. Is is possible? or if possible then please help me? and if this Question is not a big problem so i am sorry for that in advance.
<a href="{{'/employee'}}?id={{$emp->id}}" type="button" name="user_id" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Apply Attribute
</a>
<form action="{{'/rating'}}" method="post">
{{csrf_field()}}
<input type="hidden" name="user_id" value="{{store here current user}}">
</form>
Seems you just need paste your current user inside INPUT:
<form action="{{'/rating'}}" method="post">
{{csrf_field()}}
<input type="hidden" name="user_id" value="{{ $emp->id }}">
</form>
.....
If you want to use an id from a url you can :
Route::get('/url/{emp}', 'YourController#method');
In your controller:
public function method(Employee $emp) {
//Your code
return view('youre.view', compact('emp'))
}
Now in your view you have $emp and can acces id like $emp->id.
Offcourse name it what you want but be sure to name it the same in your route,controller and view.
Now you don't need a forEach loop, because of the binding you already have the employee from the url
p.s: Employee model is just an assumption..name it whatever your model is called.
You can use route function in blade for this.
Try this on web.php :
Route::get('/employee/{id}', 'YourController#YourMethod')->name('routename');
on your Controller your method need to have an arguments
public function YourMethod($id){
// Code here
}
And on your blade make your href with route
Link
Href Like That:
<a href="{{$emp->id}}" type="button" id="uu_id" class="btn btn-primary uu"
data-toggle="modal" data-target="#myModal"> Apply Attribute </a>
Using This Script To get Value from Href:
<script type="text/javascript">
$(document).ready(function() {
$(".uu").click(function(event) {
var u_id = $(this).attr('href');
event.preventDefault();
document.getElementById("hiddenVal").value = u_id;
});
});
</script>
And in your Form like this:
<form action="{{'/rating'}}" method="post">
{{csrf_field()}}
<input type="submit" style="margin-bottom: 10px;" class="btn btn-success
pull-right" name="apply" value="Apply"/>
<input type="hidden" name="hiddenVal" id="hiddenVal" />
</form>
And Last How to get this Value in the Controller And save to Database:
public function store(Request $request)
{
$rates = new Rating;
$user_id = $_POST['hiddenVal'];
$rates->user_id = $user_id;
$rates->save();
return redirect('/employee');
}

jquery-the output is always in the first item only

I am trying to create a web store and I use Jquery in radio button for selecting a category but the output is always on the first item only not in each items.
here is the jquery code
function populateswatches() {
var swatchesName = $('input[name=optradio]:checked').val();
$.getJSON('getdata.php', {swatchesName: swatchesName}, function(swatch) {
var html = '';
$.each(swatch, function(index, array) {
html = html + '<div class="col-md-3 w3-margin-top"><div class="w3-display-container w3-hover-opacity"><div class="w3-center"><input type="radio" name="swatches_code" value="' + array['swatches_code'] + '" required></div><div class="w3-display-middle w3-display-hover"><button type="button" class="btn btn-primary"><i class="fa fa-search fa-lg" aria-hidden="true"></i></button></div><img src="../backend/functions/images/'+array['images']+'" style="width:100%"><div class="w3-center">' + array['swatches_code']+'</div></div></div>';
});
$('#swatches').html(html);
});
}
$(function() {
$(this).change(function() {
populateswatches();
});
});
here is the code for php. it is not complete though.
$priceSQL="SELECT * FROM price WHERE id_item='$IDitem'";
$priceRES=mysqli_query($conn,$priceSQL);
while($priceROW=mysqli_fetch_assoc($priceRES)){
$id_categ=$priceROW["id_category"];
$catSQL="SELECT * FROM category WHERE id_category='$id_categ'";
$catRES=mysqli_query($conn,$catSQL);
while($catROW=mysqli_fetch_assoc($catRES)){
echo '
<div class="radio">
<label><input type="radio" name="optradio" value="'.$priceROW["price"].'-'.$id_categ.'" required>'.$catROW["swatches_category"].'- ₱'.formatMoney($priceROW["price"]).'</label>
</div>
';
}
}
</li>
</ul>
<h4>SWATCHES</h4>
<div class="form-group">
<span id="swatches"></span>
</div>
<input type="hidden" value="'.$IDitem.'" name="id_item">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-success">Add to Cart</button>
here is the output
here is the image output for it
Reason:-
id is treated as unique identifier in jQuery.Since all spansin your code have same id, that's why code only working for first-one.
Solution:-
Convert id to class in your HTML code like below:-
<span class="swatches"></span>
And change # to . in jQuery like below:-
$('.swatches').html(html);
Note:- class is treated as a group identifier in jQuery and work on each element which share same class.

How to know if im pushing a button in Laravel

I am trying to get this button
<button type="submit" class="btn btn-default btn-block btn-lg" name='submit'>
Retrieve
</button>
in my Middleware and I want to verify if I'm pushing this button, this is my middleware:
public function handle($request, Closure $next){
$var = isset($_POST[$request->submit]);$other = ((bool) $var);
if (redirect('retrieve_money')) {
return redirect()->guest(route('home'));
} else if ($other) {
return redirect()->guest(route('retrieve_money.index'));
}
}
I don't know how to verify how to know if someone is pushing this button.
I am considering your form is GET.
Just have this inside your VerifyCsrfToken.php
$submit = $request->submit;
if (isset($submit)) {
echo $submit;
}
And then your button should be changed like
<input type="submit" class="btn btn-default btn-block btn-lg" >
Then if you click the submit then you should see the submit's value in the top of the page.
Tip :
You shall see the output directly by page's source by
view-source:http://localhost/yourproject/?submit=1221

Get id of Buttons using php once clicked

I have 4 buttons,and also an exportexcel button.I want to set the excel filename.If i clicked getToday button, i want to set the today date to the excel.
<div class="span3 btns_state">
<div data-toggle="buttons-radio" id="toggleButton" class="btn-group clearfix sepH_a">
<button type="button" name="getTday" id="getToday" class="btn btn-info rep_data tip-bottom active" data-original-title="<?php echo $today;?>"onclick = "show()">Today</button>
<button type="button" name="getWeek" id="getWeek" class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $this_week;?>"onclick = "show()">This Week</button>
<button type="button" name="getMonth" id="getMonth" class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $this_month;?>"onclick = "show()">This Month</button>
<button type="button" name="getPreMonth" id="getpremon"class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $previous_month;?>"onclick = "show()">Last Month</button>
</div>
</div>
My php code to get the dates.
$str = $_GET['getTday'];
if($str == 'getToday')
{
$var=$today;
}
else
{
$var=$this_week;
}
I want to retrieve the dates corresponding to the button i clicked,but only else part is working.
Here is the excel export.
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), Report-<?php echo $var?>.xlsx");
You can use query string to pass the value of clicked button
refer the "Today" button below.
You can set the value of variable "$var" to your execel or anything.
[Note:I done in the same page,create "test.php" and paste the following code and try,it will works well]
Thank You.
<?php
//Assume Variables $today and $this_week like below
$today = date('Y-m-d');
$this_week = "This week";
$var = "";
if(isset($_GET['getTday'])):
$str = $_GET['getTday'];
if($str == 'getToday')
{
$var=$today;
}
else
{
$var=$this_week;
}
endif;
echo $var;
?>
<div class="span3 btns_state">
<div data-toggle="buttons-radio" id="toggleButton" class="btn-group clearfix sepH_a" >
<button type="button" name="getTday" id="getToday" class="btn btn-info rep_data tip-bottom active" data-original-title="<?php echo $today;?>"onclick = "show('getToday')">Today</button>
<button type="button" name="getWeek" id="getWeek" class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $this_week;?>"onclick = "show()">This Week</button>
<button type="button" name="getMonth" id="getMonth" class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $this_month;?>"onclick = "show()">This Month</button>
<button type="button" name="getPreMonth" id="getpremon"class="btn btn-info rep_data tip-bottom" data-original-title="<?php echo $previous_month;?>"onclick = "show()">Last Month</button>
</div>
</div>
<script>
function show(val)
{
if(val == 'getToday')
{
location.href = "test.php?getTday=getToday";
}
}
</script>
Make button type submit with form tag
<form>
<button type="submit" name="getTday">
and so on...
</form>
Then you will get in php like $_GET['getTday'] this will get name attr of field so keep id and name same
else you need to use jquery/js to get id of button
add value attribute in your button like this value="getToday".
You can get this in php file.
Example
<button type="button" name="getTday" id="getToday" value="getToday" class="btn btn-info rep_data tip-bottom active" data-original-title="<?php echo $today;?>"onclick = "show()">Today</button>
Using jquery click event to perform your action. You have to remove all click event inside html
$(".btn.btn-info.rep_data.tip-bottom").click(function(){
var name=$(this).attr("name");
// you can get what ever it is
// this.id;
// this.value
});
DEMO
NOTE: Dont write event in html it is not good practis

Categories