Retrieve data from checkboxes - php

I want to get all the checked data from my table. I'm using checkboxes in my table. When I click "change status", it will change my role status.
But I have problems with retrieving the checkboxes values. In the code below, it failed to update my data. Checkboxes value that I retrieve is NULL. How can I solve this problem?
Model
function deaktifRole($id_role,$editBy)
{
$data = array(
'update' =>date('Y-m-d H:i:s'),
'updateBy' =>$editBy,
'flag' => '0'
);
$this->db->where('id_role',$id_role);
$this->db->update('tbl_role',$data);
}
Controller
function deaktifRole()
{
$session_data = $this->session->userdata('logged_in');
$editBy = $session_data['username'];
foreach ($this->input->post['pilih'] as $value) {
$this->Role->deaktifRole($value->value,$editBy);
}
redirect('Home/Role');
echo '<script>alert("Your form was successfully submitted!");</script>';
}
View
<div class="x_panel">
<div class="x_title">
<h2>Manage Staff Role</small></h2>
<?php echo form_open_multipart('Home/deaktifRole');?>
<div align="right">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Add</button>
<button type="submit" class="btn btn-primary">Change Status</button>
</div>
<div class="clearfix"></div>
</div>
<div class="x_content">
<table id="datatable-checkbox" class="table table-striped table-bordered bulk_action">
<thead>
<tr>
<th><input type="checkbox" id="check-all" class="flat"></th>
<th>No</th>
<th>Role</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php $j=0; foreach ($RoleList as $rows)
{
$j++;
?>
<tr>
<td><input type="checkbox" class="flat" name="pilih[]" value="<?php echo $rows['id_role']; ?>"></td>
<td><?php echo $j; ?></td>
<td><?php echo $rows['role']; ?></td>
<td>Aktif</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>

As $this->input->post('pilih') is just an associative array, you can access it directly without ->value properties. Try use this code :
foreach ($this->input->post('pilih') as $value) {
$this->Role->deaktifRole( $value,$editBy );
}

use $this->input->post('pilih');
to fetch checkbox data.

Related

Can't redirect after update data codeigniter and still in current page

Help, i cant redirect after update data, after edit the page still in current page. but when i place the redirect function after query $update, when i press edit button the page will go to redirect page and data edit form not show. How to fix this?
My Controller
is it i make a wrong if?
public function edit_anggaran($getId)
{
$id = encode_php_tags($getId);
$data_id = $this->uri->segment('4');
$this->_validasi();
if ($this->form_validation->run() == false) {
$data['title'] = "Detail Anggaran Pengajuan Proposal";
$data['detail_anggaran_proposal'] = $this->admin->get('detail_anggaran_proposal', ['id_detail_anggaran_proposal' => $data_id]);
$data['proposal'] = $this->admin->get('proposal', ['id_proposal' => $id]);
if ($this->template->load('templates/dashboard', 'proposal/edit_anggaran', $data)){
$d = array( 'nama_kebutuhan'=> $this->input->post('nama_kebutuhan'),
'harga' => $this->input->post('harga'),
'jumlah'=> $this->input->post('jumlah')
);
}
$update = $this->admin->update_detail('detail_anggaran_proposal', 'id_detail_anggaran_proposal', $data_id, $d);
redirect('proposal/anggaran/'.$id);
} else {
if ($update) {
set_pesan('data berhasil disimpan.');
redirect('proposal/anggaran/'.$id);
} else {
set_pesan('data gagal disimpan', false);
redirect('proposal/add');
}
// $input = $this->input->post(null, true);
}
}
My Model
public function update_detail($table, $pk, $id, $data)
{
$this->db->where($pk, $id);
return $this->db->update($table, $data);
}
My View
<div class="card-body">
<?= $this->session->flashdata('pesan'); ?>
<?= form_open('', [], ['id_detail_anggaran_proposal' => $detail_anggaran_proposal['id_detail_anggaran_proposal']]); ?>
<table class="table table-bordered" id="">
<thead>
<tr>
<th>Nama Kebutuhan</th>
<th>Harga</th>
<th>Jumlah</th>
<th>Total Harga</th>
</tr>
</thead>
<tbody>
<td><input value="<?= set_value('nama_kebutuhan', $detail_anggaran_proposal['nama_kebutuhan']); ?>" name="nama_kebutuhan" id="nama_kebutuhan" type="text" class="form-control" ></td>
<td><input value="<?= set_value('harga', number_format($detail_anggaran_proposal['harga'],'0',',','.')); ?>" name="harga" id="harga" type="text" class="form-control" ></td>
<td><input value="<?= set_value('jumlah', $detail_anggaran_proposal['jumlah']); ?>" name="jumlah" id="jumlah" type="text" class="form-control" ></td>
<?php $total=0; ?>
<td>Rp. <?= number_format($subtotal=$detail_anggaran_proposal['harga'] * $detail_anggaran_proposal['jumlah'],'0',',','.'); $total += $subtotal; ?> </td>
</tr>
<tr>
<td colspan="3" class="text-center"><h5><b>Total Anggaran </b></h5></td>
<td>Rp. <?= number_format($total,'0',',','.') ?></td>
</tr>
</tbody>
</table>
<div class="row form-group">
<div class="col-md-9 offset-md-3">
<button type="submit" class="btn btn-primary" style="float: right;">Simpan</button>
</div>
</div>
</div>
</div>
<?= form_close(); ?>
</div>

How to insert html table row when checkbox is clicked in codeigniter

How can I solve this? I am new to this codigniter so can anyone explain to this how to solve this
<form class="form-horizontal form-label-left" method="post" action="<?php echo base_url()?>Quiz/add_assign_stu">
<input type="hidden" name="teach_id" value="<?php echo $teach_id; ?>">
<table id="datatable" class="table table-striped table-bordered">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>User Id</th>
</tr>
</thead>
<tbody>
<?php
foreach ($student as $stu) {
?>
<tr>
<th>
<input class="form-group" type="checkbox" name="click[]" value="<?php echo $stu->users_id;?>">
</th>
<th><?php echo $stu->name; ?></th>
<td><?php echo $stu->users_id;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<div class="form-group">
<div class="col-md-12 col-sm-12 col-xs-12">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
controller
public function add_assign_stu()
{
$click = $this->input->post('click');
$this->Common_model->insert_record('student_exam_list',$values);
redirect("Quiz/importFile/");
}
I have a HTML table with a checkbox in one of the columns. I want to know how I could get the row data when the user clicks on the checkbox with javascript (without jquery)? Can anyone please help me with this?

Get php checkbox data from multiple tables

First I will tell what I want to achieve, and then I will explain how I was trying to do it.
I have two tables, one stores type of vessels with a description and their own Id. Then I have another table in wich the vessel type has been stored as text. My goal is to select each one (both records in both tables) with a checkbox in both and store in the table 2 the Id from the table 1.
I will introduce data, to help.
Table 1
1|Balandra|Some description
2|Bergantin|Some description
3|Whatever |Whatever.....
Table2
Balandra
Bergantin
Whatever
Then, I have created a php page that shows both tables with the checkbox I mentioned above. Checkboxes store the Table1 Id and Table2 vesseltypename.
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<a href=<?php echo '../vista/modificar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
pencil"></em></a>
<a href=<?php echo '../datos/borrar.php?id=' .
$navio['idtiponavio']; ?> class="btn btn-default"><em class="fa fa-
trash"></em></a>
</td>
<td class="hidden-xs"><?php echo
$navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid" value=<?
php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body paneltodo">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
So, I want to check both table records and store the table1 Id in the table2 idtypevessel field.
I thought that a php file could store both items and call the update function with those parameters, like this:
<?php
require './modelo.php';
$idnavio = $_GET['agruparid'];
$vessel = $_GET['agruparvessel'];
Any suggestions, because I think I have to do a button to submit this parameters, but it must be working on both tables, and I don't know how to access both foreach loop at the same time.
Thanks in advance.
E. Salas
review bellow reference code to submit multi selected checkbox values
for multi selected checkbox submission you must use [] operator after name attribute in html
index.php
<form action="/checkbox.php" method="post">
<strong>Cars:</strong><br>
<?php
$cars = array("Volvo", "BMW", "Toyota");
$colors = array("Red", "Green", "Black");
foreach($cars as $single){
?>
<input type="checkbox" name="cars[]" value="<?php echo $single; ?>">
<?php
}
<br>
<strong>colors:</strong><br>
foreach($colors as $single){
?>
<input type="checkbox" name="colors[]" value="<?php echo $single; ?>">
<?php
}
?>
<br>
<input type="submit" value="Submit!">
</form>
checkbox.php
<?php
echo "<pre>";
var_dump($_POST);
exit;
In your case:
<form action="/checkbox.php" method="post">
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th class="hidden-xs">idtiponavio</th>
<th>Tipo de Navío</th>
<th>Descripción</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosdyncoop as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-pencil"></em>
<em class="fa fa-trash"></em>
</td>
<td class="hidden-xs"><?php echo $navio['idtiponavio']; ?></td>
<td><?php echo $navio['tiponavio']; ?></td>
<td><?php echo $navio['descripcion']; ?></td>
<td><input type="checkbox" name="agruparid[]" value=<?php echo $navio['idtiponavio']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="col-md-6">
<div class="panel-body">
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th><em class="fa fa-cog"></em></th>
<th>Tipo de Navío</th>
<th>Agrupar</th>
</tr>
</thead>
<tbody>
<?php foreach ($naviosforsea as $key => $navio) { ?>
<tr>
<td align="center">
<em class="fa fa-arrow-circle-o-left"></em>
</td>
<td><?php echo $navio['typevessel']; ?></td>
<td><input type="checkbox" name="agruparvessel[]" value=<?php echo $navio['typevessel']; ?> /></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<input type="submit" value="Submit!">
</form>
Finally I solved this with Javascript. One of my partners helped me, I post this to help people in my situation.
I created a script, here is the code:
<script type="text/javascript">
function cogeNavioDyncoopnet(){
var checkedValueNavioD = null;
var inputElements = document.getElementsByClassName('checknaviod');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioD = inputElements[i].value;
break;
}
}//return checkedValueNavioD;
var input_nav_dyn = document.getElementById("nav_dyn");
input_nav_dyn.value = checkedValueNavioD;
}
function cogeNavioForSea(){
var checkedValueNavioFs = null;
var inputElements = document.getElementsByClassName('checknaviofs');
for(var i=0; inputElements[i]; ++i){
if(inputElements[i].checked){
checkedValueNavioFs = inputElements[i].value;
break;
}
}//return checkedValueNavioFs;
var input_nav_fs = document.getElementById("nav_fs");
input_nav_fs.value = checkedValueNavioFs;
}
</script>
Then I created a Form below, that collects values and sends them to my .php control file.
<div class="hidden-xs">
<form class="hidden-xs" method="POST"
action="../datos/actualizarnavios.php">
<input id="nav_dyn" type="text" name="idnaviodyncoop">
<input id="nav_fs" type="text" name="navioforsea" >
<input id="botonasignar" type="submit" name="enviardatosdynfs">
</form>
</div>
I hope this helps. Thanks for the feedback, as always.

Only variables should be passed by reference, database/DB_active_rec.php

I am new to codeigniter, I just want to ask how to get rid with this thing
A PHP Error was encountered
Severity: Notice
Message: Only variables should be passed by reference
Filename: database/DB_active_rec.php
Line Number: 225
I dont know why, this suddenly comes out, but my page is doing fine, but this is at the top of my page, how to get rid of this thing, this is what i have:
Pages_Controller:
public function clients_ordered_products($id){
if(isset($_SESSION["id"])){
if($_SESSION["Usertype"]==1){
$this->load->view('admin/header');
$this->load->view('title/title_admin');
$this->load->view('admin/navigation');
$this->data["posts"] = $this->Generate_Model->getProductsClientsOrdered($id);
$this->load->view('admin/clients_ordered_products',$this->data);
$this->load->view('admin/footer');
}
else if($_SESSION["Usertype"]==0){
redirect('Pages_Controller/Ooops_Client');
}
}
else{
redirect($url);
}
}
and
Generate_Model:
public function getProductsClientsOrdered($id){
$this->db->select('*');
$this->db->select_sum('tbl_product_selected.Quantity');
$this->db->from('tbl_product_selected');
$this->db->join('tbl_product','tbl_product.Product_ID = tbl_product_selected.Product_ID');
$this->db->where('Account_ID',$id);
$this->db->where('Purchase_time',NULL);
$this->db->where('Active',1);
$this->db->group_by('tbl_product_selected.Product_ID');
$query = $this->db->get();
return $query->result();
}
and
<div id="page-wrapper">
<div class="cart">
<div class="container">
<div class="col-md-9 cart-items">
<!-- /.panel-heading -->
<div class="panel-body">
<table id="cart" class="table table-hover table-responsive">
<thead>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
<th class="text-center">Subtotal</th>
<th>Action</th>
</tr>
</thead>
<?php $total = 0; ?>
<?php foreach($posts as $post){ ?>
<form action="<?php echo base_url(); ?>index.php/Transaction_Controller/removeItem/<?php echo $post->Product_selected_id; ?>" method="post">
<tbody>
<tr>
<td data-th="Product">
<div class="row">
<div class="col-sm-2 hidden-xs"> <img style="height:100px;"src="<?php echo $post->Directory?>"></div>
<div class="col-sm-10">
<h4 class="nomargin"></h4>
</div>
</div>
</td>
<td data-th="Price"><?php echo $post->Price;?></td>
<td data-th="Price" style="text-align:left"><input type="number" value="<?php echo $post->Quantity; ?>" class="form-control" style="width:60px"/></td>
<td data-th="Price" style="text-align:center"><?php echo $subtotal = $post->Price * $post->Quantity; ?></td>
<td class="actions" data-th="">
UPDATE
<button class="btn btn-danger btn-sm">DELETE<i class="fa fa-trash-o"></i></button>
</td>
</tr>
<?php $total += $subtotal; ?>
</tbody>
<tfoot>
<tr class="visible-xs">
<td class="text-center"><strong>
</strong></td>
</tr>
<tr>
<td></td>
<td colspan="2" class="hidden-xs"></td>
<td></td>
<td></td>
</tr>
</tfoot>
</form>
<?php } ?>
</table>
<center><form action="<?php echo base_url(); ?>index.php/Transaction_Controller/Products_Ordered/" method="post">
<input type="hidden" value="<?=$total?>" name="total"/>
<input type="hidden" value="<?php echo $post->Account_ID; ?>" name="id"/>
<button type="submit" class="btn btn-success btn-block">PAY NOW! <i class="fa fa-angle-right"></i></button>
</form><br/><br/>
</div>
</div>
</div>
</div>
why is this happening?
I've got the same error after upgrade from PHP 5.6 to PHP 7.1 on one project using CodeIgniter 2.
I fix it like this:
// line 224 of file /system/database/DB_active_rec.php
$explode = explode('.', $item);
return end($explode);
instead of
return end(explode('.', $item));
This notice is triggered by E_STRICT error reporting, because of this code in CodeIgniter 2:
// DB_active_rec.php line 225
end(explode('.', $item));
CodeIgniter 2 is not developed anymore and you should update to CodeIgniter 3.

recursive function in php app with $_SESSION arrays

Im trying to build an ap in php to learn foreign languages. I got an function in it that builds a form and starts the test. it looks like:
public function test($pol_ang)
{
$words = $this->wordsArray();
if($pol_ang == 1)
{
$_SESSION['correctAnswers'] = $this->answersPolAng($words);
}else {$_SESSION['correctAnswers'] = $this->answersAngPol($words);}
if(isset($_POST['btn-confirm']))
{
$answer = strip_tags($_POST['txt_word']);
if($answer != "")
{
$_SESSION['usersAnswerArray'][$_SESSION['licznik']-1] = $answer;
$_SESSION['licznik']++;
}
if($_SESSION['licznik'] > count($words))
{
unset($_SESSION['licznik']);
$this->auth_user->redirect('result.php');
}
}
if(empty($_SESSION['licznik']))
{
$_SESSION['licznik'] = 1;
}
?>
<form method="post" class="form-signin">
<table class="table table-striped table-hover">
<thead>
<tr>
<th><?php echo $_SESSION['licznik'].' z '.count($words); ?></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<?php
$word = explode(";",$words[($_SESSION['licznik']-1)]);
if($pol_ang == 1)
{
?>
<td><?php echo $word[0]; ?></td>
<?php
}
else
{
?>
<td><?php echo $word[1]; ?></td>
<?php
}
?>
<td>-</td>
<td>
<div class="form-group">
<input type="text" class="form-control" name="txt_word" placeholder="Tłumaczenie" required />
<span id="check-e"></span>
</div>
</td>
</tr>
</tbody>
</table>
<hr />
<div class="form-group">
<button type="submit" name="btn-confirm" class="btn btn-default">
<i class="glyphicon glyphicon-log-in"></i> END
</button>
</div>
</form>
<?php
}
At the begginning wordsArray() function explode class's variable and $words becomes an array: [0]->wordLanguage1;wordLanguage2, [1]->wordLanguage1;wordLanguage2 etc...
$pol_ang is language option. If equals 1 then $_SESSION['correctAnswers'] becomes "wordsLanguage1", if equals 0 then becomes "wordLanguage2".
This function works fine. But what I wanna do is that my functio after ending the test should repeat asking for words that user entered incorrect. I can not find the solution to my problem

Categories