How to accept or reject the data using checkbox in codeiginter? - codeigniter

I am accepting and rejecting some data while clicking checkbox.From my code accept code is working well but rejected is same type of code but it is not working well.
My view page code:
<form class="form-horizontal" method="POST" action="<?=site_url('Request/Update_Event')?>">
<div class="panel panel-flat" id="id">
<div class="panel-heading">
<center> <h4 class="panel-title">Request Portal</h4></center>
</div> <button type="submit" class="btn btn-info btn-lg">
<span class="glyphicon glyphicon-thumbs-up"></span>Approved
</button> <a href="<?=site_url('Request/Rejected')?>" class="btn btn-info btn-lg" id="but">
<span class="glyphicon glyphicon-thumbs-down"></span> Rejected
</a><div>
My Controller Code:
public function Update_Event(){
$empid = $this->input->post('empid');
for($i=0; $i< sizeof($empid); $i++)
{
$data = array(
'backgroundColor' => 'green',
'event_status' =>'Approved',
);
$this->db->where(empid,$empid[$i]);
$this->db->update('events',$data);
}
redirect('Request','refresh');
}
public function Rejected(){
$empid = $this->input->post('empid');
for($i=0; $i< sizeof($empid); $i++)
{
$data = array(
'backgroundColor' => 'red',
'event_status' =>'Rejected',
);
$this->db->where(empid,$empid[$i]);
$this->db->update('events',$data);
}
redirect('Request','refresh');
print_r($empid);
}
My table code:
<table id="tb2" class="table datatable-responsive" >
<thead><tr><h6><th></th><th align="center">Name</th>
<th class="col-sm" >Leave Date</th>
<th class="col-sm" >Reason</th>
</tr></thead>
<?php foreach ($query as $row): ?>
<tr><td><input type="checkbox" name="empid[]" value="<?php echo $row['empid'];?>"></td>
<td>Leave Applied By <?php echo $row['first_name'];?> <?php echo $row['last_name'];?></td>
<td><?php echo date('d/m/Y', strtotime($row['event_date']));?></td>
<td><?php echo $row['title'];?> </td>
</tr></tbody><?php endforeach ?>
</table>
The rejected code is not working in my code and the empid is not passed to Rejected code when i print the empid value

You are facing this issue because at approval time you are submitting form and in rejection time you are only clicking on link. So when you click on reject button it will not post any value to controller.
You can do something like this. just pass extra parameter to reject function.
<a href="<?=site_url('Request/Rejected/123')?>" class="btn btn-info btn-lg" id="but">
<span class="glyphicon glyphicon-thumbs-down"></span> Rejected
</a>
public function Rejected($empid = ''){
for($i=0; $i< sizeof($empid); $i++)
{
$data = array(
'backgroundColor' => 'red',
'event_status' =>'Rejected',
);
$this->db->where(empid,$empid[$i]);
$this->db->update('events',$data);
}
redirect('Request','refresh');
print_r($empid);
}

Related

getting errors when showing data on page using ajax

I am adding a search functionality to my user admin page and I am using AJAX for search but I have not been able to show data on my page. It gives an error.
I have tried adding single quotation or double quotation on every side but it just give me an error every time
this is the controller function:
public function search(Request $request)
{
if($request->ajax())
{
$output = '';
$query = $request->get('query');
if($query != '')
{
$data =User::where('name', 'like', '%'.$query.'%')->get();
}
else
{
$data =User::orderBy('created_at' ,'desc')->get();
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $merchant)//That how i am showing data on the page
{
$output .=
"<tr>
<td>"{$users->name}"
"#if($users->allowded == 0)" //I need to add these if else functionality to my code but these thing are giving error
"#endif"
</td>
<td>{{$users->email}}</td>
<td>{{$users->permission->permission}}</td>
<td>
<a href="#" class="btn btn-xs btn-success">
<span style="padding-right: 16px;">Make Him Salaried User
</span>
</a>
<a href="#" class="btn btn-xs btn-success">
<span style="padding-right: 16px;">Make Him Commesioned User
</span>
</a>
<a href="#" class="btn btn-xs btn-success">
<span style="padding-right: 16px;">Make Him Salaried and Commesionded User
</span>
</a>
</td>
<td>
#if($users->user_status)
<a href="" class="btn btn-xs btn-danger">
<span style="padding-right: 10px;">Make Him Unpaid User
</span>
</a>
#else
<a href="" class="btn btn-xs btn-success">
<span style="padding-right: 16px;">Make Him Paid User
</span>
</a>
#endif
</td>
<td>
#foreach($cashtransfer as $cash)
#if($cash->user_id == $users->id)
{{ $cash->amount}}
#endif
#endforeach
</td>
<td>
#if($users->allowded)
<a href="" class="btn btn-xs btn-danger">
<span style="padding-right: 16px;">Bloack User</span>
</a>
#else
<a href="" class="btn btn-xs btn-success">
<span style="padding-right: 16px;">Allow User</span>
</a>
#endif
</td>
<td>
</td>
<td>
<a href="" class="btn btn-xs btn-danger">
<span style="padding-right: 16px;">Delete User</span>
</a>
</td>
</tr>";
}
}
else
{
//This is The Out Variable which is taking data to the page
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>';
}
$data = array(
'table_data' => $output,
'total_data' => $total_row
);
echo json_encode($data);
}
}
These Are the errors I am getting every time:
// ->when($request->has('query'), function($q){
// $q->where('name','like','%'. request('query').'%');
// })->get();
$data =User::where('name', 'like', '%'.$query.'%')->get();
}
else
{
$data =User::orderBy('created_at' ,'desc')->get();
// $pagi = "<div> {$data->links()}</div>";
}
$total_row = $data->count();
if($total_row > 0)
{
foreach($data as $merchant)
{
$output .= "
<tr>
<td>"{$users->name}"
"#if($users->allowded == 0)"
"#endif"
</td>
<td>{{$users->email}}</td>
<td>{{$users->permission->permission}}</td>
<td> <span style="padding-right: 16px;">Make Him Salaried User</span> <span style="padding-right: 16px;">Make Him Commesioned User</span> <span style="padding-right: 16px;">Make Him Salaried and Commesionded User</span></td>
<td>
#if($users->user_status)
<span style="padding-right: 10px;">Make Him Unpaid User</span>
#else
Why can't I just publish it?
I keep getting errors like I need more details
You cannot use blade inside normal strings. You have to use normal PHP to compose the output:
foreach($data as $merchant)
{
$optionalOutput = $users->allowded == 0 ? 'output if true' : 'output if false';
$output .= "<tr>
<td>{$users->name}
{$optionalOutput}
</td>";
// ...
}
Or you could move the code to a blade view:
foreach($data as $merchant)
{
$output .= view('path.to.view', [$merchant, $users])->render();
}

How to get the data after submit the form based on the form input value using laravel

My controller contain single function with multiple API calling when I returned the controller function to my view api data displayed automatically but i need to display the data while submit the form. Please suggest any solution.
My Controller Code
public function domaincheck(Request $request)
{
ini_set('max_execution_time', 300);
<!-- Domain Availability Check -->
$sld = $request['sld'];
$tld = $request['tld'];
$response = file_get_contents('https://resellertest.enom.com/interface.asp?command=check&sld='. $sld .'&tld='. $tld .'&uid=resellid&pw=resellpw&responsetype=xml');
$data = simplexml_load_string($response);
$configdata = json_encode($data);
$final_data = json_decode($configdata,true);
<!--Domain Name Suggestions -->
$response1 = file_get_contents('http://resellertest.enom.com/interface.asp?command=GETNAMESUGGESTIONS&uid=resellid&pw=resellpw&SearchTerm='. $sld .'&MaxResults=5&ResponseType=XML');
$data1 = simplexml_load_string($response1);
$configdata1 = json_encode($data1);
$final_data1 = json_decode($configdata1,true);
}
My view Code
<form class="form-horizontal" method="get">
<div class="form-group">
<div class=" col-lg-2"></div>
<div class="col-lg-8">
<div class="input-group m-b">
<span class="input-group-addon" style="padding-left:10px; background-color: #999;" class='unclickable'>www</span>
<input type="text" name="sld" class="form-control">
<span class="input-group-addon">
<select class="form-control" name="tld" style="width: 100px;">
<?php $j = 0; ?>
#foreach($final_data2['tldlist']['tld'] as $value)
<?php $j++; ?>
#endforeach
#for ($i = 0; $i < $j-1;)
<option value="{{($final_data2['tldlist']['tld'][$i]['tld'])}}">{{($final_data2['tldlist']['tld'][$i]['tld'])}}</option>
<?php $i++; ?>
#endfor
</select>
</span>
<span class="input-group-addon">
<button type="submit" class="btn btn-sm btn-success">Submit</button>
</span>
</div>
<p class="text-center text-dark customFont" >
#foreach($final_data as $key=>$value)
#if($key=='DomainName')
<b>{{$value}}</b> <b>-</b>
#endif
#if($key=='RRPText')
<b>{{$value}}</b>
#endif
#endforeach
</p>
{{print_r($final_data1)}}
When submit the form (Response) it shows the domain name is available or not available. Response1 is shows the domain name suggestions but it shows the data with out submit the form.Please suggest any solutions

Banner Images not being reinserted correct to database

When I update my banner_image it should delete the ones from the database Then re insert the ones that are set.
Currently what it does is deletes the banner image but then re inserts blank banner_image
But should remove that enitre row.
Question How to make sure when I update it that will remove the images rows that are no longer selected and not re insert them
public function update($bid = NULL, $data = array()) {
$banner_update = array('banner_title' => $data['banner_title'], 'banner_status' => $data['banner_status']);
$this->db->where('bid', $bid);
$this->db->update('banner', $banner_update);
$this->db->where('bid', $bid)->delete('banner_image');
if (isset($data['banner_image'])) {
$banner_images = array();
$i = 0;
foreach ($data['banner_image'] as $image) {
$banner_images[$i] = array(
'bid' => $bid,
'banner_image' => $image['image'],
);
$i++;
}
$this->db->insert_batch('banner_image', $banner_images);
}
}
View
<?php echo form_open($action);?>
<div class="container">
<div class="card mt-3">
<div class="card-body">
<?php if (validation_errors()) {?>
<?php unset($_POST);?>
<div class="bg-error-warning">
<ul>
<?php echo validation_errors('<li>', '</li>');?>
</ul>
</div>
<?php }?>
<div class="row">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
<p>Banner Title</p>
<div class="form-group">
<input type="text" name="banner_title" value="<?php echo $banner_title;?>" placeholder="Enter Banner Title" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
<table id="images" class="table table-striped table-bordered">
<tbody>
<tr>
<td>
<button type="button" onclick="addImage();" data-toggle="tooltip" title="Add Banner!" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button>
</td>
</tr>
<?php $image_row = 0; ?>
<?php foreach ($banner_images as $banner_image) { ?>
<tr id="image-row<?php echo $image_row; ?>">
<td>
<a href="" id="thumb-image<?php echo $image_row; ?>" data-toggle="image">
<img src="<?php echo $banner_image['thumb']; ?>" alt="" title="" data-placeholder="<?php echo $placeholder; ?>" class="img-thumbnail"/></a>
<input type="hidden" name="banner_image[<?php echo $image_row; ?>][image]" value="<?php echo $banner_image['image']; ?>" id="input-image<?php echo $image_row; ?>" />
</td>
</tr>
<?php $image_row++; ?>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
<p>Banner Status</p>
<div class="form-group">
<?php $options = array('0' => 'Disabled', '1' => 'Enabled'); echo form_dropdown('banner_status', $options, $banner_status, array('class' => 'form-control'));?>
</div>
</div>
</div>
<div class="row">
<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<button type="submit" class="btn btn-block btn-dark">Create Banner</button>
</div>
</div>
</div>
</div>
</div>
</div>
<?php echo form_close();?>
<script type="text/javascript">
var image_row = <?php echo $image_row; ?>;
function addImage() {
html = '<tr id="image-row' + image_row + '">';
html += '<td class="text-left">';
html += '<a href="" id="thumb-image' + image_row + '" data-toggle="image" >';
html += '<img src="<?php echo $placeholder; ?>" width="100" height="100" class="img-thumbnail" data-placeholder="<?php echo $placeholder; ?>"/>';
html += '<input type="hidden" name="banner_image[' + image_row + '][image]" value="" id="input-image' + image_row + '" />'
html += '</a>';
html += '</td>';
html += '<td class="text-left">';
html += '<button type="button" onclick="$(\'#image-row' + image_row + '\').remove();" data-toggle="tooltip" title="Remove Banner!" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button>'
html += '</td>';
html += '<script>';
html += '$(document).ready(function(){';
html += '$(\'[data-toggle="tooltip"]\').tooltip();';
html += '});';
html += '<\/script>';
$('#images tbody').append(html);
image_row++;
}
</script>
Have got it working just had to put empty in the foreach loop
public function update($bid = NULL, $data = array()) {
$banner_update = array('banner_title' => $data['banner_title'], 'banner_status' => $data['banner_status']);
// Updates the main banner table
$this->db->where('bid', $bid);
$this->db->update('banner', $banner_update);
// Removes banners so can have clean insert of new banners selected
$this->db->where('bid', $bid);
$this->db->delete('banner_image');
// Inserts new banner images & saved ones.
if ($this->input->post('banner_image')) {
foreach ($this->input->post('banner_image') as $image) {
if (!empty($image['image'])) {
$this->db->insert('banner_image', array('bid' => $bid, 'banner_image' => $image['image']));
}
}
}
}

Multidimensional input with codeigniter not setting second array correct

On my controller function get form I get my banner images and I also use multidimensional array in my view
For some reason, the second banner_image title not showing but the value has been placed on the first array instead?
print_r($_POST)
Question How am I able to make sure that no matter how many banner images I select it can set the multidimensional array correct for each post.
public function getForm()
{
$banner_id = $this->uri->segment(5);
if ($banner_id)
{
$data['action'] = 'admin/design/banners/edit/' . $banner_id;
} else {
$data['action'] = 'admin/design/banners/add';
}
$banner_info = $this->admin_model_banner->getBanner($banner_id);
if ($this->input->post('banner_name')) {
$data['banner_name'] = $this->input->post('banner_name');
} elseif (!empty($banner_info)) {
$data['banner_name'] = $banner_info['banner_name'];
} else {
$data['banner_name'] = '';
}
if ($this->input->post('banner_status')) {
$data['banner_status'] = $this->input->post('banner_status');
} elseif (!empty($banner_info)) {
$data['banner_status'] = $banner_info['status'];
} else {
$data['banner_status'] = '';
}
$banner_images = array();
$banner_images_post = $this->input->post('banner_image');
if (isset($banner_images_post)) {
$banner_images = $this->input->post('banner_image');
} elseif (isset($banner_id)) {
$banner_images = $this->admin_model_banner->getBannerImages($banner_id);
}
$data['banner_images'] = array();
foreach ($banner_images as $banner_image)
{
if (is_file(FCPATH . 'image/' . $banner_image['image'])) {
$image = $banner_image['image'];
$thumb = $banner_image['image'];
} else {
$image = '';
$thumb = 'catalog/no_image.jpg';
}
$data['banner_images'][] = array(
'image' => $image,
'thumb' => $this->model_tool_image->resize($thumb, 100, 100),
'title' => $banner_image['title'],
'sort_order' => $banner_image['sort_order']
);
}
$data['placeholder'] = $this->model_tool_image->resize('catalog/no_image.jpg', 100, 100);
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->load->view('design/banner_form_view', $data);
}
View
<?php echo $header;?>
<div class="container">
<?php
$form = array(
'id' => '',
'role' => 'form',
'class' => 'form-horizontal'
);
echo form_open_multipart($action, $form);?>
<div class="panel panel-default">
<div class="panel-heading"></div>
<div class="panel-body">
<div class="form-group">
<label class="col-lg-2">Banner Status</label>
<div class="col-lg-10">
<input type="text" name="banner_name" class="form-control" placeholder="Enter Banner Name" value="<?php echo $banner_name;?>" size="50"/>
<?php echo form_error('banner_name', '<div class="text-danger" style="margin-top: 2rem;">', '</div>'); ?>
</div>
</div>
<div class="form-group">
<label class="col-lg-2">Banner Name</label>
<div class="col-lg-10">
<?php
$options = array('1' => 'Enabled', '0' => 'Disabled');
echo form_dropdown('banner_status', $options, $banner_status, array('class' => 'form-control'));
?>
</div>
</div>
<table id="images" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td class="text-left">Title</td>
<td class="text-left">Image</td>
<td>Sort Order</td>
</tr>
</thead>
<tbody>
<?php $image_row = 0; ?>
<?php foreach ($banner_images as $banner_image) { ?>
<tr id="image-row<?php echo $image_row; ?>">
<td class="text-left">
<input type="text" name="banner_image[<?php echo $image_row; ?>][title]" value="<?php echo $banner_image['title']; ?>" class="form-control">
</td>
<td class="text-left">
<a href="" id="thumb_image_<?php echo $image_row; ?>" data-toggle="image" class="img-thumbnail">
<img src="<?php echo $banner_image['thumb']; ?>" alt="" title="" data-placeholder="<?php echo $placeholder; ?>" />
</a>
<input type="hidden" name="banner_image[<?php echo $image_row; ?>][image]" value="<?php echo $banner_image['image']; ?>" id="input_image_<?php echo $image_row; ?>" /></td>
<td class="text-right"><input type="text" name="banner_image[<?php echo $image_row; ?>][sort_order]" value="<?php echo $banner_image['sort_order']; ?>" placeholder="Sort Order" class="form-control" /></td>
<td class="text-left">
<button type="button" onclick="$('#image-row<?php echo $image_row; ?>').remove();" class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i></button></td>
</tr>
<?php $image_row++; ?>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td colspan="3"></td>
<td class="text-left">
<button type="button" onclick="addImage();" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button>
</td>
</tr>
</tfoot>
</table>
</div>
<div class="panel-footer">
<div class="text-right"><button type="submit" class="btn btn-primary"><i class="fa fa-floppy-o" aria-hidden="true"></i> Save</button></div>
</div>
</div>
<?php echo form_close();?>
</div>
<script type="text/javascript">
var image_row = <?php echo $image_row; ?>;
function addImage()
{
html = '<tr id="image-row' + image_row + '">';
html += '<td class="text-left">';
html += '<input type="text" name="banner_image[<?php echo $image_row; ?>][title]" class="form-control" value="">';
html += '</td>';
html += '<td class="text-left">';
html += '<a href="" id="thumb_image_' + image_row + '" data-toggle="image" class="img-thumbnail">';
html += '<img src="<?php echo $placeholder; ?>" data-placeholder="<?php echo $placeholder; ?>"/>';
html += '</a>';
html += '<input type="hidden" name="banner_image[' + image_row + '][image]" value="" id="input_image_' + image_row + '" />';
html += '</td>';
html += '<td class="text-right">';
html += '<input type="text" name="banner_image[' + image_row + '][sort_order]" value="" placeholder="Sort Order" class="form-control" />';
html += '</td>';
html += '</tr>';
$('#images tbody').append(html);
image_row++;
}
</script>
Solved
After doing some investigation I found problem was to do with
name="banner_image[<?php echo $image_row; ?>][title]"
On script, I forgot to add it like
name="banner_image[' + image_row + '][title]"
Working now

model not inserting select option

My model is not inserting my modify and access data that I select on my view.If my select option on my view is select option 1 it inserts value 0 even though 1 selected. Not sure why on model or view its not inserting the correct one?
Each controller has its own modify and access select option i would like to be able to insert if select 1 for enable or 0 disbale?
How can I make the both select options work with my model so inserts correct what have chosen.
Model Function
<?php
class Model_user_group extends CI_Model {
public function addUserGroup() {
$name = $this->input->post('name');
$controllers = $this->input->post('controller');
$access = $this->input->post('access');
$modify = $this->input->post('modify');
for($i=0;$i<count($controllers);$i++) {
$data = array(
'name' => strtolower($name),
'controller' => $controllers[$i],
'access' => $access,
'modify' => $modify
);
$this->db->insert($this->db->dbprefix . 'user_group', $data);
}
}
View
<?php echo Modules::run('admin/common/header/index');?>
<div id="wrapper">
<?php echo Modules::run('admin/common/menu/index');?>
<div id="page-wrapper" >
<div id="page-inner">
<div class="panel panel-default">
<div class="panel-heading clearfix">
<div class="pull-left" style="padding-top: 7.5px"><h1 class="panel-title"><?php echo $title;?></h1></div>
<div class="pull-right">
Cancel
<button type="submit" onclick="submit()" class="btn btn-primary">Save</button>
</div>
</div>
<div class="panel-body">
<?php echo validation_errors('<div class="alert alert-warning text-center">', '</div>'); ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'form-users-group');?>
<?php echo form_open_multipart('admin/users_group_add', $data);?>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2');?>
<?php echo form_label('User Group Name', 'name', $data);?>
<div class="col-sm-10">
<?php $data1 = array('id' => 'name', 'name' => 'name', 'class' => 'form-control', 'value' => set_value('name'));?>
<?php echo form_input($data1);?>
</div>
</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Controller Name</td>
<td>Access</td>
<td>Modify</td>
</tr>
</thead>
<?php foreach ($controllers as $controller) {?>
<tbody>
<tr>
<td>
<?php echo ucfirst(str_replace("_"," ", $controller));?>
<input type="hidden" name="controller[]" value="<?php echo $controller;?>" />
</td>
<td>
<select name="access" class="form-control">
<option value="0">Disabled</option>
<option value="1">Enabled</option>
</select>
</td>
<td>
<select name="modify" class="form-control">
<option value="0">Disabled</option>
<option value="1">Enabled</option>
</select>
</td>
</tr>
</tbody>
<?php } ?>
</table>
<?php echo form_close();?>
</div>
</div>
</div><!-- # Page Inner End -->
</div><!-- # Page End -->
</div><!-- # Wrapper End -->
<?php echo Modules::run('admin/common/footer/index');?>
Your view file seems has access and modify option for each controller. But your input name is same.So the value of $access and $modify is the last input value. You can solve this way.Give access and modify input name as array
<select name="access[]" class="form-control">
...
<select name="modify[]" class="form-control">
Now your model
$name = $this->input->post('name');
$controllers = $this->input->post('controller');
$accesses = $this->input->post('access');
$modifies = $this->input->post('modify');
for($i=0;$i<count($controllers);$i++) {
$data = array(
'name' => strtolower($name),
'controller' => $controllers[$i],
'access' => $accesses[$i],
'modify' => $modifies[$i]
);
$this->db->insert($this->db->dbprefix . 'user_group', $data);
}
Hope you understand and solves your problem

Resources