Get variable from view to controller - php

I need to get all vars from view to controller(CodeIgniter 4). This is controller function for viewing data:
public function tambahTA()
{
$proyekID = $this->proyekModel->getProyekOrderByID();
$proyekName = $this->proyekModel->getProyekOrderByProyek();
$ta = $this->rhModel->getCV();// Untuk memilih nama dan mengambil id Tenaga Ahli
$data = [
'judul' => 'Tambah Tenaga Ahli',
'ta' => $ta, // Untuk memilih nama dan mengambil id dari Tenaga Ahli
'proyekID' => $proyekID, // Pengalaman diambilkan dari tabel proyek
'proyekName' => $proyekName,
];
return view('exp/tambahTA', $data);
}
and this is code in view (exp/tambahTA) that show id, name and checkbox in a table(listing):
<tbody>
<?php if ($ta) {
foreach ($ta as $v) {
$id = isset($v['id']) ? $v['id'] : '';
$nama = isset($v['nama']) ? $v['nama'] : '';
?>
<tr>
<td name='id' style="width:5%"><?= $id ?></td>
<td name='nama' style="width:25%"><?= $nama ?></td>
<td style="width:15%; text-align:center">
<label><input type='checkbox' name='check'></label>
</td>
</tr>
<?php
}
}
?>
</tbody>
I need to get all variables from above view in controller function(public function simpanTA) to save all checked id, name and checkboxes with insertBatch, but I don't have any idea how to do it ?

Related

How to get value from td tag and use it in foreach in codeigniter

I need to retrieve value carried in td tag that show value from database.Then i need to insert them in different database.From model i retrieved two values which are seatNumber and seatLabel and show them in checkboxex that carries seatNumber value.
my model for get seatValue
public function getSeats(){
$query = $this->db->get('seat');
if($query->num_rows() > 0){
return $query->result();
}
}
my view for displaying seatNumber and seatLabel
<tbody>
<?php if(count ($seats)):?>
<?php foreach ($seats as $seat):?>
<tr>
<td><input type="checkbox" name="seat_id[]" value=<?php echo $seat->seatNumber;?>></td>
<td id="seatLabel" name="seat_label"><?php echo $seat->seatLabel;?></td>
</tr>
<?php endforeach;?>
<?php else:?>
<td>No Records Founds!</td>
<?php endif;?>
</tbody>
My controller
public function message(){
if ($this->input->post('submit') == 'Set Schedule') {
$this->form_validation->set_rules('busNumber','Bus Number', 'required');
$this->form_validation->set_rules('seat_id[]','Seats', 'required');
$this->form_validation->set_rules('bookingDate','Date of Journey', 'required');
$this->form_validation->set_rules('reportingTime','Reporting Time', 'required');
$this->form_validation->set_rules('departureTime','Departure Time', 'required');
if($this->form_validation->run()){
$seat_ids = $this->input->post("seat_id[]");
$seat_label = $this->input->post("seat_label");
$busNumber = $this->input->post("busNumber");
$bookingDate = $this->input->post("bookingDate");
$reportingTime = $this->input->post("reportingTime");
$departureTime = $this->input->post("departureTime");
$this->load->model('Queries');
$insert = $this->Queries->saveMessage($seat_ids, $seat_label, $busNumber, $bookingDate, $reportingTime, $departureTime);
if($insert) {
echo "Success";
}
else {
echo "error";
}
} else {
echo validation_errors();
}
}
My model for inserting date to new database
public function saveMessage($seat_ids, $seat_label, $busNumber, $bookingDate, $reportingTime, $departureTime){
foreach($seat_ids as $seat_id)
{
$record = array(
'seatNumber' => $seat_id,
'seatLabel' => $seat_label
'bookingDate' => $bookingDate,
'reportingTime' => $reportingTime,
'departureTime' => $departureTime,
'busNumber' => $busNumber,
'seatUse' => 'Enabled',
'seatStatus' => 'Available',);
$this->db->insert('schedule', $record);
}
return true;
}
THE PROBLEM COMES IN DATABASE THERE IN NO SEATLABEL VALUE
ERROR
There is way like this. if you change this
<td id="seatLabel" name="seat_label"><?php echo $seat->seatLabel;?></td>
to
<td><input type="text" name="seat_label" value="<?php echo $seat->seatLabel;?>"></td>
but if you don't want to show the text box, then create hidden field also.
<input type="hidden" name="seat_label" value="<?php echo $seat->seatLabel;?>">
<td><?php echo $seat->seatLabel;?></td>

update product details not working CodeIgniter

I'm making a fuction on the CodeIgniter framework so users are able to edit/update product detail information. But when I submit the form I get a blank page and the product isn't updated in my database. I couldn't find a mistake myself.
Here is my view file form (cadeaubewerken.php):
<table class="aanbieding-cadeau">
<form action="<?php echo base_url() . "KdGwController/update_product"; ?>" method="post">
<tr>
<h4>Cadeau naam</h4>
<td><?php echo form_input(array('id'=>'product_naam', 'name'=>'product_naam', 'value' => $product["product_naam"] , 'size'=>25));?></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'product_id', 'name'=>'product_id', 'value' => $product['product_id']));?>
</tr>
<tr>
<td><?php echo form_open_multipart('Product/upload'); ?> </td>
</tr>
<tr>
<td>
<h4>Kies een categorie</h4>
<select name="category_id">
<?php foreach (get_categories_h() as $category) :
if ($category->id == $product["category_id"]){
echo '<option value="'.$category->id .'" selected>' . $category->name . '</option>';
}else{
echo '<option value="'.$category->id .'">'.$category->name . '</option>';
}
endforeach; ?>
</select>
</td>
</tr>
<tr>
<td><h4>Ophaal plaats</h4><?php echo form_input(array('id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'value' => $product["ophaal_plaats"], 'size'=>25));?></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'value' => $product['ophaal_plaats']));?>
</tr>
<tr>
<td>
<div class="checkbox">
<label><input type="checkbox" value="">Gebruik adres van mijn account</label>
</div>
</td>
</tr>
<tr>
<td>
<h4>Huidig cadeau profiel foto</h4>
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>" class="img-responsive">
</td>
</tr>
<tr>
<td>
<h4>Cadeau profiel foto veranderen</h4>
<input type="file" name="userfile"/>
</td>
</tr>
<tr>
<td><?php echo form_textarea(array('type'=>'textarea','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'value' =>$product['product_beschrijving'], 'size'=>25));?></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'value' => $product['product_beschrijving']));?>
</tr>
<tr>
<td><input type="submit" class="btn btn-primary" name="submit" value="Cadeau bewerken!" /></td>
</tr>
</table>
</form>
This is my controller file (KdGwController.php):
<?php
class KdGwcontroller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Product_model');
$this->load->model('Update_product_model');
$this->load->helper(array('form', 'url'));
}
public function details_bewerken($product_id) {
//load the Product_model
$this->load->model('Product_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->Product_model->getdata();
//get product details
$data['product'] = $this->Product_model->get_product_details($product_id);
//laad view
$data['main_content'] = 'cadeaubewerken';
$this->load->view('cadeaubewerken',$data);
}
public function update_product() {
$id= $this->input->post('product_id');
$data = array(
'product_naam' => $this->input->post('product_naam'),
'category_id' => $this->input->post('category_id'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
);
}
}
And this is my model file : (Update_product_model.php):
<?php
class Update_product_model extends CI_Model {
function update_product($id,$data){
$this->db->where('product_id', $id);
$this->db->update('products', $data);
header ('location:https://kadokado-ferran10.c9users.io//AlleCadeausController');
}
}
?>
No need to add update in model.
Also, your controller update function should be like
public function update_product() {
$id= $this->input->post('product_id');
$data = array(
'product_naam' => $this->input->post('product_naam'),
'category_id' => $this->input->post('category_id'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
);
// here I am assuming that `Product` is model
$this->Product->where($id);
$this->Product->update($data);
}
You have not call model function in your controller.
Change your controller update_product() function like this
public function update_product() {
$id= $this->input->post('product_id');
$data = array(
'product_naam' => $this->input->post('product_naam'),
'category_id' => $this->input->post('category_id'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
);
$this->Update_product_model->update_product($id,$data);
}
You are not calling function update_product() of Update_product_model.php from KdGwController.php
Change this in your controller...
public function update_product() {
$id= $this->input->post('product_id');
$data = array(
'product_naam' => $this->input->post('product_naam'),
'category_id' => $this->input->post('category_id'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
);
$this->Update_product_model->update_product($id,$data); // this line you missed
}

404 error when I try to update my form with id 1, but its not happen to another id

i have db with id 1,2,4 (img-1)
but if I try to update id_bank = 1, i got 404 error (img-2)
but that update work with another id (img-3)
this application work fine yesterday, but I got this error this morning, I dont know why, I really need help :(
Update:
I use codeigniter framework,
this is my controller code
function form($id_bank = false)
{
$this->id_bank = $id_bank;
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$data['page_title'] = lang('bank_form');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => lang('home'),
'href' => base_url()
);
$data['breadcrumbs'][] = array(
'text' => lang('bank_form'),
'href' => base_url('bank')
);
//default values are empty if the provinsi is new
$data['id_bank'] = '';
$data['nama_bank'] = '';
$this->form_validation->set_rules('nama_bank', 'lang:nama_bank', 'trim|required|max_length[64]');
//$this->form_validation->set_rules('id_categories', 'lang:id_categories', 'trim|required|max_length[64]');
// validate the form
if ($this->form_validation->run() == FALSE)
{
$this->view('bank/form', $data);
}
else
{
if (!$this->auth->hasPermission('modify', 'bank'))
{
$this->session->set_flashdata('error', lang('error_acces'));
redirect('bank');
}
$this->load->helper('text');
$save['id_bank'] = $id_bank;
$save['nama_bank'] = $this->input->post('nama_bank');
$this->Model_bank->save_provinsi($save);
$this->session->set_flashdata('message', lang('message_provinsi_saved'));
//go back to the category list
redirect('bank');
}
}
this is on my view
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th><?php echo lang('no');?></th>
<th><?php echo sort_url('nama_bank', 'nama_bank', $order_by, $sort_order, $code);?></th>
<th><?php echo sort_url('gambar', 'image', $order_by, $sort_order, $code);?></th>
<th class="text-center"><?php echo lang('action');?></th>
</tr>
</thead>
<tbody>
<?php echo (count($type) < 1)?'<tr><td style="text-align:center;" colspan="5">'.lang('no_type').'</td></tr>':''?>
<?php
$pages = ($page-1)*10;
foreach ($type as $merks):
$pages+=1;
?>
<tr>
<td style="width:10px;"><?php echo $pages; ?></td>
<td style=""><?php echo $merks->nama_bank; ?></td>
<td style="text-align:center"> <span><img width="50px" height="50px" src='<?php echo $this->config->item('url_image').'photo/bank/'.$merks->image?>'></span></td>
<td style="width:10%;" class="text-center">
<i class="fa fa-pencil"></i>
<?php if ($this->auth->hasPermission('delete', 'bank')) { ?>
<i class="fa fa-trash-o"></i>
<?php } ?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
this on my routes:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "dashboard";

Yii Undefined index

Hi I'm using Yii framework and trying to submit my data from view but It got error, says Undefined index, here's the printscreen:
And the codes are:
Here's my model:
<?php
class ConfirmPayment extends CActiveRecord{
public $nomorPemesanan; //nomorpemesanan
public $bankAsal; // nama bank asal
public $pemilikRekAsal; // nama pemilik rek asal
public $bankTujuan; //nama bank tujuan transfer
public $nominalTransfer; // nominal transfer / jumlah uang yang di transfer
public $dataPaymentText;
public static function model($className = __CLASS__){
return parent::model($className);
}
public function tableName(){
return 'tbl_confirm_payment';
}
public function afterFind(){
parent::afterFind();
$this->dataPaymentText = explode('#',$this->text_detail);
}
public function rules(){
return array(
array('order_code,text_detail','required'),
array('nomorPemesanan,bankAsal,pemilikRekAsal,bankTujuan,nominalTransfer','required'),
);
}
public function attributeLabels(){
return array(
'id'=>'ID Konfirmasi',
'nomorPemesanan'=>'Nomor Pemesanan',
'pemilikRekAsal'=>'Nama Pemilik Rekening Asal',
'bankAsal'=>'Nama Bank Asal',
'bankTujuan'=>'Bank Tujuan Transfer',
'nominalTransfer'=>'Jumlah yang ditransfer',
);
}
}
Here is my controller:
public function actionOrders($id = '')
{
// component IsAuth
IsAuth::Customer();
// konfirmasi pembayaran
if (isset($_GET['confirm']))
{
// panggil model Confirmpayment
$model = new ConfirmPayment;
// jika data ConfirmPayment dikirim dengan method $_POST
if (isset($_POST['ConfirmPayment']))
{
// set value field
$order_code = $_POST['ConfirmPayment']['nomorPemesanan'];
$model->attributes = $_POST['ConfirmPayment'];
$model->order_code = $_POST['ConfirmPayment']['nomorPemesanan'];
$model->text_detail .= $_POST['ConfirmPayment']['bankAsal'] . '#';
$model->text_detail .= $_POST['ConfirmPayment']['pemilikRekAsal'] . '#';
$model->text_detail .= $_POST['ConfirmPayment']['bankTujuan'] . '#';
$model->text_detail .= $_POST['ConfirmPayment']['nominalTransfer'] . '#';
// jika data confirmPayment disimpan maka
if ($model->save())
{
// find data order by attributes berdasarkan order_code
$modelOrder = Order::model()->findByAttributes(array('order_code' => $order_code));
// set field payment_status dari 0 menjadi 1, artinya pembayaran sudah dikonfirmasi
$modelOrder->payment_status =1;
exit(CActiveForm::validate($modelOrder));
// simpan
$modelOrder->save();
// setFlash konfirmasi pembayaran sukses
Yii::app()->user->setFlash('success', 'Belanjaan dengan nomor pesanan #'.$order_code.' berhasil dikonfirmasi!');
// redirect
$this->redirect(array('orders'));
return;
}
}
// render ke file customer/confirm_payment
$this->render('confirm_payment', array('model' => $model));
return;
}
// untuk view list_orders
if (empty($id))
{
// panggil model Order dan function search
$model = new Order('search');
// hapus default values pada attributes
$model->unsetAttributes();
// untuk filter data Order berdasarkan id customer
$model->customer_id = Yii::app()->user->customerId;
// jika data order dikirim view get
if (isset($_GET['Order']))
{
// set attributes untuk pencarian
$model->attributes = $_GET['Order'];
}
// render ke file customer/list_orders
$this->render('list_orders', array('model' => $model));
return;
}
// untuk view detail_order
if (!empty($id))
{
// join query untuk mendapatkan detail_order
$dataOrderDetail = Yii::app()->db->createCommand()
->select('tbl_orders.*,tbl_orderdetail.*,tbl_products.*')
->from('tbl_orders')
->join('tbl_orderdetail', 'tbl_orderdetail.order_id = tbl_orders.id')
->join('tbl_products', 'tbl_products.id = tbl_orderdetail.product_id')
->where('tbl_orders.id=:id_order', array(':id_order' => $id))
->queryAll();
// join query untuk mendapatkan data order dan customer
$dataOrder = Yii::app()->db->createCommand()
->select('tbl_orders.*, tbl_customer.customer_name')
->from('tbl_orders')->join('tbl_customer', 'tbl_orders.customer_id = tbl_customer.id')
->where('tbl_orders.id=:id', array(':id' => $id))
->queryRow();
// render ke view customer/detail_order
$this->render('detail_order', array(
'dataOrder' => $dataOrder,
'orderDetail' => $dataOrderDetail,
'subtotal' => '',
'grandtotal' => '',
));
return;
}
and my view:
<div style="padding:5px 10px 0 0;margin:5px 5px 15px 5px; border:1px solid #CCC;text-align: justify;">
<div style="clear: left;"></div>
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::errorSummary($model); ?>
<table>
<tr>
<td><?php echo CHtml::activeLabel($model, 'nomorPemesanan'); ?></td>
<td>:</td>
<td><b><?php echo $_GET['confirm']; ?></b></td>
</tr>
<tr>
<td><?php echo CHtml::activeLabel($model, 'bankAsal'); ?></td>
<td>:</td>
<td><b><?php echo CHtml::activeTelField($model, 'bankAsal'); ?></b></td>
</tr>
<tr>
<td><?php echo CHtml::activeLabel($model, 'pemilikRekAsal'); ?></td>
<td>:</td>
<td><b><?php echo CHtml::activeTextField($model, 'pemilikRekAsal'); ?></b></td>
</tr>
<tr>
<td><?php echo CHtml::activeLabel($model, 'bankTujuan'); ?></td>
<td>:</td>
<td>
<select name="Confirmpayment[bankTujuan]">
<option value="BCA 1700-******* a.n. L******">BCA 1700-******* a.n. L******</option>
<option value="Mandiri 150-00-********* L****** Jr">Mandiri 150-00-********* a.n. L******</option>
<option value="BNI 1580****** a.n. L******">BNI 1580****** a.n. L******</option>
</select>
</td>
</tr>
<tr>
<td><?php echo CHtml::activeLabel($model, 'nominalTransfer'); ?></td>
<td>:</td>
<td><b><?php echo CHtml::activeTelField($model, 'nominalTransfer'); ?></b></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><?php echo CHtml::submitButton('Konfirmasi'); ?></td>
</tr>
</table>
<?php echo CHtml::endForm(); ?>
<div style="clear: both;"> </div>
Hope anybody can solve this.. Thanks in advance!
You don't have the nomorPemesanan active text field in your form.
You could try updating the form this way:
<tr>
<td><?php echo CHtml::activeLabel($model, 'nomorPemesanan'); ?></td>
<td>:</td>
<td><b><?php echo CHtml::activeTextField($model, 'nomorPemesanan'); ?></b></td>
</tr>

Codeigniter can't pass value to controller

i have views here
<?php echo form_open('c_kategorimaterial/tambah'); ?>
<table>
<tr>
<td>Kode Kategori Material / Jasa</td> //primary key of table
<td><?php $inkode=array('name' => 'kkmj', 'disabled' => 'disabled', 'value' => $nextval, 'class' => 'GUI'); echo form_input($inkode) ?></td>
<td class="error"> <?php echo form_error('kkmj'); ?></td>
//i make the form_input disabled, because the value is
//auto generated from the last ID of table in database
</tr>
<tr>
<td>Nama Material / Jasa</td>
<td><?php $innama=array('name' => 'nmj', 'class' => 'GUI'); echo form_input($innama) ?></td>
<td class="error"> <?php echo form_error('nmj'); ?></td>
</tr>
<tr>
<td></td>
<td><?php $intambah=array('name' =>'login','class' =>'button','value' => 'Tambah'); echo form_submit($intambah); echo nbs(); $inreset=array('name' =>'reset','class' =>'button','value' => 'Hapus'); echo form_reset($inreset); ?></td>
</tr>
<?php echo form_close(); ?>
and it pass the value to the controller
function tambah()
{
$id = $this->input->post('kkmj');
$nama = $this->input->post('nmj');
$this->form_validation->set_rules('nmj','Nama','trim|required|min_length[2]|max_length[20]|xss_clean');
if($this->form_validation->run() == false)
{
$lastval = $this->m_admin->getlastval('KKMJ','ms_kategori_material','kode_kategori_material_jasa');
$data['nextval'] = $this->m_admin->gencode('KKMJ',3,$lastval);
$data['title'] = 'QB Tambah Kategori Material';
$this->load->view('head',$data);
$this->load->view('content/add_kategori_material',$data);
}
else
{
echo 'id adalah '.$id.' namanya adalah '.$nama; //$id is not printed
}
}
and then i fill the second field with 'water' and click the button. my page showing blank white screen with a text id adalah namanya adalah water
how come the ID is not printed ? how do i resolve this ?
Disabled elements do not get posted when you submit a form. If you need a field with unchangeable value, try to set it as read-only instead of disabled.
Because the field is disabled.
Use readonly attribute.

Categories