want to pass the post value in to controller - codeigniter

Here am having the code like this
<div class="form-group">
<label class="col-lg-3 control-label">Start Date</label>
<div class="col-lg-5">
<div class="input-group">
<input type="text" required name="attendance_date" id="attendance_date" class="form-control datepicker" value="<?php echo date('Y-m-d');?>" data-date-format="<?= config_item('date_picker_format'); ?>" required>
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
</div>
</div>
</div>
<a data-toggle="modal" data-target="#myModal" type="button" name="update" href="<?php echo base_url();?>admin/attendance_confirmation/reject1/<?php echo $this->input->post('attendance_date');?>" id="update1" value="Reject" class="btn btn-danger ml" ><i class="fa fa-times"></i> <?= lang('reject') ?></a>
and my controller looks like this
public function reject1($date)
{
$data['date']=$date;
var_dump($data['date']);
$data['modal_subview'] = $this->load->view('admin/engineer_attendance_confirmation/attendance_confirmation_modal',$data,FALSE);
$this->load->view('admin/_layout_modal', $data);
}
but the value in the attendance_date is not getting in controller.how can we pass the value in the post to the controller using href tag

In CodeIgniter, POST data isn't passed through as variables into the controller function. You'd instead need to use the input library. You can then use $this->input->post() to get POST data:
$data['date'] = $this->input->post('attendance_date');

Using href tag you don't get data in the post, but you can get the data using a segment like this:
public function reject1()
{
$data['date']=$this->uri->segment(3);
var_dump($data['date']);
$data['modal_subview'] = $this->load->view('admin/engineer_attendance_confirmation/attendance_confirmation_modal',$data,FALSE);
$this->load->view('admin/_layout_modal', $data);
}

Related

pass object from HTML template back to the controller

I have the following HTML block. I want to pass the object "jobDTO" back to the contoller "/deleteJob" method. Whatever I do I am getting null object.
<th:block th:if="${jobDTO != null}" th:each="jobDTO: ${allJobDTOs.get(jobGroup)}">
<div id="accordion2" style="margin-bottom: 3px;">
<div class="card" id="headingOne">
<div class="card-header" style="padding: 0">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" th:attr="data-target='#accordion2_'+${jobDTO.identity.name}"
aria-expanded="true" aria-controls="collapseChild" >
<p class="font-weight-bold custom-p identity-black" > Job Identity </p>
<p class="custom-p" style="padding-left: 52px;" th:text="${jobDTO.identity.group} +' , ' + ${jobDTO.identity.name}"></p>
</button>
</h5>
</div>
<div th:id="'accordion2_'+${jobDTO.identity.name}" class="collapse" aria-labelledby="headingOne" data-parent="#accordion2">
<div class="card-body">
<dl class="row">
<dt class="col-lg-3">Trigger List</dt>
<dd class="col-sm-9">
<th:block th:each="trigger: ${jobDTO.triggers}">
<p><b>nextFireTime</b> <span th:text="${trigger.nextFireTime}"> </span></p>
<hr>
</th:block>
</dd>
</dl>
<!-- important part.. how to pass the jobDTO object back to the controller -->
<form id="form2" action="#" th:action="#{/deleteJob}" th:object="${jobDTO}" th:method="post">
<input type="text" th:value="*{identity.name}" th:field="*{identity.name}" hidden/>
<button type="submit" value="Submit" class="btn btn-danger btn-sm" >Delete Job</button>
</form>
</div>
</div>
</div>
</div>
</th:block>
my controller relevant parts are:
#GetMapping(value = "/deleteJob")
public String deleteJobPage(Model model) {
model.addAttribute("jobDTO", new ScheduleJobDTO());
//Returns the Home page with the prepared model attributes
return "Home";
}
// =================
#PostMapping("/deleteJob")
public String deleteJob(#ModelAttribute final ScheduleJobDTO jobDTOReturn, BindingResult bindingResult, Model model) {
// I want to receive the jobDTO object here
schedulerService.deleteJobFromGroup(jobDTOReturn.getIdentity().getGroup(),
jobDTOReturn.getIdentity().getName());
return "redirect:/";
}
what I am missing here?
I think there is an error in your input tag, try this :
<input type="text" th:value="${jobDTO.identity.name}" th:field="*{identity.name}" hidden/>

Laravel search functionality

I've got a searchbar on my homepage which looks like this:
<form action="{{ route('search', $query) }}" method="GET" role="search">
<div class="input-group mb-4 search_bar">
<input type="search" name="search" placeholder="Search..." aria-describedby="button-addon5" class="form-control search_input">
<div class="input-group-append">
<button id="button-addon5" type="submit" class="btn btn-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</form>
My route looks like this:
Route::get('all-{query}-posts', 'SearchController#index')->where('query', '[A-Za-z0-9-]+')->name('search');
So basically I want the input of the searchbar in my url like this: /all-stuff-posts instead of the usual: ?search=stuff
It seems to automatically add the ?search=stuff to the end even though I didn't add that anywhere so that's the first problem.
The second problem is that I can only retrieve the query in the controller but that gives me an error in web.php because the query is still not set. Is there a different way of doing this that does work?
Change your form action method below like this
<form action="{{url('')}}/all-{{$query}}-posts" ...>
Used Post method.
<form action="{{ route('search', $query) }}" method="POST" role="search">
If you want to develop a search engine in your app, I will suggest the following. (One of the simplest ways).
Learn about POST, GET, PUT first.\
Then, My model like this
public function scopeSearchByKeyword($query, $keyword,$location)
{
if ($keyword!='' and $location!='') {
$query->where(function ($query) use ($keyword,$location) {
$query->where("title", "LIKE","%$keyword%")
->where("location_id", "$location")
->where("status", "1");
});
}
else
{
$query->where(function ($query) use ($keyword) {
$query->where("title", "LIKE","%$keyword%")
->where("status", "1");
});
}
return $query;
}
Here I am searching by title and location. It can output even if only a keyword is entered.
Then my controller like this:
public function search_kasblar(Request $request)
{
$inputs = $request->all();
$keyword = $inputs['search_keyword'];
$location = $inputs['location'];
$jobs= JobBoards::SearchByKeyword($keyword,$location)->get();
$total_res=count($jobs);
return view('jobs.search',compact('jobs','total_res','keyword'));
}
Here we can search for incoming data in the input.
So my view blade like this:
<div class="finderform">
{!! Form::open(array('url' => 'listings/search','class'=>'','id'=>'search','role'=>'form')) !!}
<div class="col-md-5 col-sm-5 no-padding"> <i class="fa fa-search local-search-ic"></i>
<input type="text" class="form-control" name="search_keyword" id="input-search-term" title="Search for..." placeholder="Search anything here" value="" autocomplete="off">
</div>
<div class="form-group col-md-5 col-sm-5 no-padding"> <i class="fa fa-map-marker local-search-ic ic-map-location"></i>
<div class="">
<select id="location" name="location" class="form-control">
<option value="">Select Location</option>
#foreach(\App\Location::orderBy('location_name')->get() as $location)
<option value="{{$location->id}}">{{$location->location_name}}</option>
#endforeach
</select>
</div>
</div>
<button type="submit" class="btn tp-btn-default tp-btn-lg">Search</button>
{!! Form::close() !!}
You can use like this.
And use POST in your router...

intigrate wysiwyg with codeigniter

I'm using codeigniter and trying to pass content to the database using the WYSIWYG editor.
I installed the WYSIWYG into my VIEW like the below code.
<div class="blank">
<div class="blank-page">
<!------------- syam bek-------------->
<?php if(!empty($error)): ?>
<div class="alert alert-warning">
<strong>Warning!</strong> <?php echo $error; ?>
</div>
<?php endif;?>
<?php if(!empty($products)):?>
<?php $ver=array('id'=>'try','name'=>'try','class'=>'form-horizontal'); ?>
<?php echo form_open_multipart(base_url().'editproduct/edit',$ver);?>
<label>المنتج
<br>
<select class="selectpicker" data-show-subtext="true" data-live-search="true" name="select_product" onchange="[document.try.action='editproduct/show',document.try.submit()]">
<?php
if (count($products)>1) {
echo '<option selected></option>';
foreach ($products as $p):
echo "<option value ='$p->p_id'>" . $p->p_name . "</option>";
endforeach;
}
elseif(count($products)==1){
foreach ($products as $p):
echo "<option selected value ='$p->p_id'>" . $p->p_name . "</option>";
endforeach;
}
?>
</select>
<?php if(count($products)==1){
$name=$p->p_name;
$details=$p->details;
$price=$p->p_price;
}else{
$name="";
$details="";
$price="";
}
?>
</label>
</br>
<label>اسم المنتج
<input type="text" name="name" value="<?php echo $name;?>" class="form-control"/>
</label>
<br>
<label>تفاصيل للمنتج
<input type="text" name="details" value="<?php echo $details;?>" class="form-control" />
</label>
<br>
<label>سعر المنتج
<input type="text" name="price" value="<?php echo $price;?>" class="form-control" />
</label>
<br>
<br>
<br>
<div class="container">
<div class="hero-unit">
<div id="alerts"></div>
<div class="btn-toolbar" data-role="editor-toolbar" data-target="#editor">
<div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" title="Font"><i class="icon-font"></i><b class="caret"></b></a>
<ul class="dropdown-menu">
</ul>
</div>
<div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" title="Font Size"><i class="icon-text-height"></i> <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a data-edit="fontSize 5"><font size="5">Huge</font></a></li>
<li><a data-edit="fontSize 3"><font size="3">Normal</font></a></li>
<li><a data-edit="fontSize 1"><font size="1">Small</font></a></li>
</ul>
</div>
<div class="btn-group">
<a class="btn" data-edit="bold" title="Bold (Ctrl/Cmd+B)"><i class="icon-bold"></i></a>
<a class="btn" data-edit="italic" title="Italic (Ctrl/Cmd+I)"><i class="icon-italic"></i></a>
<a class="btn" data-edit="strikethrough" title="Strikethrough"><i class="icon-strikethrough"></i></a>
<a class="btn" data-edit="underline" title="Underline (Ctrl/Cmd+U)"><i class="icon-underline"></i></a>
</div>
<div class="btn-group">
<a class="btn" data-edit="insertunorderedlist" title="Bullet list"><i class="icon-list-ul"></i></a>
<a class="btn" data-edit="insertorderedlist" title="Number list"><i class="icon-list-ol"></i></a>
<a class="btn" data-edit="outdent" title="Reduce indent (Shift+Tab)"><i class="icon-indent-left"></i></a>
<a class="btn" data-edit="indent" title="Indent (Tab)"><i class="icon-indent-right"></i></a>
</div>
<div class="btn-group">
<a class="btn" data-edit="justifyleft" title="Align Left (Ctrl/Cmd+L)"><i class="icon-align-left"></i></a>
<a class="btn" data-edit="justifycenter" title="Center (Ctrl/Cmd+E)"><i class="icon-align-center"></i></a>
<a class="btn" data-edit="justifyright" title="Align Right (Ctrl/Cmd+R)"><i class="icon-align-right"></i></a>
<a class="btn" data-edit="justifyfull" title="Justify (Ctrl/Cmd+J)"><i class="icon-align-justify"></i></a>
</div>
<div class="btn-group">
<a class="btn" title="Insert picture (or just drag & drop)" id="pictureBtn"><i class="icon-picture"></i></a>
<input type="file" data-role="magic-overlay" data-target="#pictureBtn" data-edit="insertImage" />
</div>
<div class="btn-group">
<a class="btn" data-edit="undo" title="Undo (Ctrl/Cmd+Z)"><i class="icon-undo"></i></a>
<a class="btn" data-edit="redo" title="Redo (Ctrl/Cmd+Y)"><i class="icon-repeat"></i></a>
</div>
<input type="text" data-edit="inserttext" id="voiceBtn" x-webkit-speech="">
</div>
<div id="editor">
</div>
</div>
</div>
<button class="btn btn-warning" name="cancel" >الغاء</button>
<button type="submit" name="save" class="btn btn-primary">حفظ</button>
<button type="submit" name="del" class="btn btn-danger">حذف المنتج</button>
<?php echo form_close();?>
<?php else :?>
<?php echo "لايوجد منتجات ليتم التعديل عليها";?>
<?php endif;?>
</div>
</div>
check i trace the inspector from the broswer i found insertion happens in the <div id="editor">HERE</div>
i don't know to pass the DIV contents to the database, especially the DIV element not identified by name like other elements.
Thanks
You can use javascript and ajax to submit your form, this is rough outline of jquery solution
// listen for submit event on your form
$( "#try" ).submit(function( event ) {
event.preventDefault();
let content = $('#editor').val(); // get the value from the editor
let details = $("input[name='details']").val() // get the value of input with the name of 'details'
... // do the same for all other inputs
// validate inputs if desired
if(content == '') {
// for example do something if content is empty
return;
}
// collect all input data into one object
var data = {
content: content,
details: details,
...
}
// send ajax request to the server
$.ajax({
url : 'urlToYourBackendEndpoint',
type : 'POST',
data : data,
success : function(response) {
// check the response and redirect or show notice after submit here
},
error : function(request,error)
{
alert("Request: "+JSON.stringify(request));
}
});
});

Laravel 5 - How to send url paramaters in form

I have a form with some input in a search page who has an URL with many parameters and I want to send them in this form or any other way .
Form code
{!! Form::open(['route' => ['test.mail'],'method' => 'post','files' => true]) !!}
{!! Form::token() !!}
<div class="box-header">
<i class="fa fa-envelope"></i>
<h3 class="box-title">Quick Email</h3>
<!-- tools box -->
<div class="pull-right box-tools">
<!--<button class="btn btn-info btn-sm" data-widget="remove" data-toggle="tooltip" title="Remove"><i class="fa fa-times"></i></button>-->
</div><!-- /. tools -->
</div>
<div class="box-body">
<div class="form-group">
<input type="text" class="form-control" name="object" placeholder="Subject" />
</div>
<div class="form-group">
<textarea class="textarea" name="textu" placeholder="Message" cols="120" rows="8"></textarea>
</div>
<div class="form-group">
<input type="file" class="form-control" name="filee" />
</div>
</div>
<div class="box-footer clearfix">
<input type="submit" name="sendEmail" class="pull-right btn btn-default" id="sendEmail" value="Send">
</div>
{!! Form::close() !!}
This is how you get all the values in your url
foreach($_GET as $key=>$value){
echo $key, ' => ', $value, "<br/>n";
}
then to send them in your form you simply create an input type hidden like this:
<input id="someId" name="UrlValue" type="hidden" value="$UrlValue">
I hope this will help,
// Get the current URL without the query string...
echo url()->current();
// Get the current URL including the query string...
echo url()->full();
// Get the full URL for the previous request...
echo url()->previous();
Each of these methods may also be accessed via the URL facade:
use Illuminate\Support\Facades\URL;
echo URL::current();
https://laravel.com/docs/5.6/urls

how to handle Trying to get property of non-object error in laravel?

I am deleting item from cart and after deleting i am displaying fresh result. but all item from cart table is delete i am getting this error
Trying to get property of non-object (View:
D:\xampp\htdocs\avitra\resources\views\ordersummary.blade.php)
for following query. When all item is removed from cart & it display then it can not get any record from cart table so it return null value and getting above error? How can i handle this error in laravel?
$getCartResult=DB::table('product_details')
->join('cart','cart.product_id','=','product_details.product_id')
->join('subcategory','product_details.sub_id','=','subcategory.sub_id')
->select('subcategory.image','subcategory.name_of_subcategory','product_details.*','cart.*')
->where('cart.user_id',$userid)
->get();
blade file:
<?php $getCartResult=DB::table('product_details')
->join('cart','cart.product_id','=','product_details.product_id')
->join('subcategory','product_details.sub_id','=','subcategory.sub_id')
->select('subcategory.image','subcategory.name_of_subcategory','product_details.*','cart.*')
->where('cart.user_id',$userid)
->get();
?>
<?php
if (!empty($getCartResult)) {
?>
<div class="card-body cart_show" style="overflow-y: scroll;height: 300px;display:none;">
#foreach($getCartResult as $v_contents)
<div class="row">
<div class="col-sm-3 mt-3">
<div>
<img src="{{asset('images/'.$v_contents->image)}}" class="img-fluid">
</div>
<center>
<div class="input-group mt-2" style="width:100px">
<button type="button" class="cart-btn btn-default btn-number cart_qtyminus" data-type="minus" data-id="<?php echo $v_contents->cart_id;?>" data-value="<?php echo $v_contents->product_id;?>">
<span class="fa fa-minus cart-fa"></span>
</button>
<!-- <input type="text" name="cart_qty" class="form-control input-number" value="{{$v_contents->qty}}" min="1" max="10"> -->
<span class="cart_quantity " style="border: 1px solid;height: 30px;width:30px;"><?php echo $v_contents->qty; ?></span>
<button type="button" class="cart-btn btn-default btn-number cart_qtyplus" data-type="plus" data-id="<?php echo $v_contents->cart_id;?>" data-value="<?php echo $v_contents->product_id;?>">
<span class="fa fa-plus cart-fa"></span>
</button>
</div>
</center>
</div>
<div class="col-sm-5 mt-3">
<div>
<span><b>{{$v_contents->name_of_subcategory}}</b></span>
</div>
<div class="mt-2">
<span>Seller : Avitra Ayurved</span>
</div>
<?php $subtotal=$v_contents->discount_price*$v_contents->qty; ?>
<div class="mt-2">
Price :<span class="cart_subtotal"><?php echo $subtotal; ?></span>
</div>
<div class="mt-2">
<span><a class="btn btn-sm mt-3 cart_delete" data-id="<?php echo $v_contents->cart_id;?>"><span style="color: #FBA842;"><b>REMOVE</b></span></a></span>
</div>
</div>
<div class="col-sm-4">
<div class="mt-2">
<span>Delivery by Fri Jan 24 | Free</span>
</div>
</div>
</div><hr>
#endforeach
<div>
<span style="float: right;"><button class="btn btn-sm continue cart_payment_show"><span><b>CONTINUE</b></span></button></span>
</div>
</div>
<?php } ?>
You are getting this error because your query is not returning any result and your are trying to access it's property something like $getCartResult->id.
To resolve this you can check if item exists or not using if condition on your blade template for e.g
#if(!empty($getCartResult))
// your code
#endif
I think the problem is that you are trying to access a row that has been deleted. Check the query that you are returning after deleting an item.

Resources