I am trying to make selecting one checkbox from a total of four checkboxes required but it's not working. Whether I select a checkbox or not, it always passes validation.
Here is my method in my controller Cargo_inquiry() {}
function step_two() {
$country_to_select = $this->session->userdata('freight_address_country');
$this->data['users'] = $this->My_Users_Model->get_companies_list($country_to_select);
// load up the validation rules for blog Info form
$this->config->load('validate_cargo');
$this->form_validation->set_rules($this->config >item('validate_cargo_create_step_two') );
if ($this->form_validation->run('validate_cargo_create_step_two') === FALSE) {
echo "FALSE";
// die;
$this->load->view('_layouts/main/template1', $this->data);
} else {
echo "TRUE";
// die;
redirect('/Cargo_inquiry/step_three');
}
}
Here is my validation rules located in validate_cargo.php:
$config['validate_cargo_create_step_two'] = array(
'user_id' => array(
'field' => 'user_id',
'label' => '',
'rules' => 'required',
'errors' => array(
'required' => 'Please select at least one company.',
),
),
);
Here is my view/form:
<?php echo form_open('Cargo_inquiry/step_three',array('class'=>'form-horizontal'));?>
<table class="table table-bordered table-striped" id="mytable">
<thead>
<tr>
<th width="3%">Select</th>
<th>company</th>
<th>location</th>
<th>rating</th>
<th>web</th>
<th>Associations/serv_hhgpe</th>
</tr>
</thead>
<tbody>
<?php
foreach ($users as $user) { ?>
<tr>
<td>
<?php // echo form_checkbox('user_id[]', 'user_id[]', set_checkbox("user_id[]"), "Company"); ?>
<input type="checkbox" name="user_id" value="<?php echo $user->id; ?>" />
</td>
<td>
<?php echo $user->company ?>
</td>
<td>
<?php echo $user->city ?>
</td>
<td>
<?php echo $user->state_province ?>
</td>
<td>
<?php echo $user->name ?>
</td>
<td style="text-align:center" width="200px">
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php echo form_submit('cargo_create_step_two', 'Next->', 'class="btn btn-primary btn-lg btn-block"');?>
<?php echo form_close();?>
Arrrgg!!! I was submitting to the wrong form :( So very sorry.
Related
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.
I am new in codeigniter. I am trying to make a table for product information under a search box in which table row will be increased dynamically from database according to each search by product model number. Now previous search data replaced by new search result and show only one product data but I want to show all previous search result in table row.I tried it by using session but not working. Thanks for any help.
Model:
class Product_Model extends CI_Model {
function product_search($model)
{
$this->db->like('modelno',$model);
$query = $this->db->get('alliteam');
if($query->num_rows() > 0){
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
Controller:
public function sell(){
$this->load->library('session');
$data = array(
'title' => 'New Customer',
'action' => site_url('Product/sell'),
'button' => 'Add Item'
);
$model = $this->input->post('model',TRUE);
$data['productinfo'] = $this->Product_Model->product_search($model);
$this->session->set_userdata($productinfo);
$this->load->view('customer',$this->session->all_userdata());
}
View
<form action="<?php echo $action;?>" method="post">
<fieldset>
<div class="control-group">
<label class="control-label" >Model No :</label>
<div >
<input type="text" name="model" value="" >
<span> Enter a product model no</span>
</div>
<div class="form-actions">
<input type="submit" name="add_item" value="<?php echo $button;?>"/>
</div>
</fieldset>
</form>
<table >
<thead>
<tr>
<th>Name</th>
<th>Model Number</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
if($this->input->post('add_item')){
foreach($productinfo as $product):?>
<tr>
<td class="center"><?php echo $product->name ?></td><br>
<td class="center"><?php echo $product->modelno ?></td>
<td class="center"><?php echo $product->product_description?></td>
</tr>
<?php endforeach; } ?>
</tbody>
</table>
You don't need to use sessions here.
Edit your controller
public function sell(){
$data = array(
'title' => 'New Customer',
'action' => site_url('Product/sell'),
'button' => 'Add Item'
);
$model = $this->input->post('model',TRUE);
$data['productinfo'] = $this->Product_Model->product_search($model);
$this->load->view('customer',$data);
}
i have some problem in my libraries cart when i try to insert my product into the cart. could you help me.
here is my code link for product into the cart.
<form method="post" action="<?php echo base_url(); ?>cart/add">
<span style="width: 70%;"><input type="submit" class="fa fa-cart-arrow-down btn btn-primary btn-sm btn-block" value="Add to Cart" <?php echo "$mati";?>/></span>
<input type="hidden" name="id_books" value="<?php echo $row->id_books; ?>"/>
<input type="hidden" name="id_category" value="<?php echo $row->id_category; ?>"/>
<input type="hidden" name="title" value="<?php echo $row->title; ?>"/>
<input type="hidden" name="images" value="<?php echo $row->images; ?>"/>
<input type="hidden" name="price" value="<?php echo $total; ?>"/>
<input type="hidden" name="qty" value="1"/>
</form>
and this is cart controller
class Cart extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library(array('cart'));
session_start();
}
function index()
{
$data['menu'] = 'cart';
$this->load->view('v_cart', $data);
}
function add()
{
$data = array(
'id_books' => $this->input->post('id_books'),
'id_category' => $this->input->post('id_category'),
'qty' => $this->input->post('qty'),
'price' => $this->input->post('price'),
'images' => $this->input->post('images'),
'title' => $this->input->post('title'));
$this->cart->insert($data);
/*print_r($data);*/
/*echo "<meta http-equiv='refresh' content='0; url=".base_url()."cart/'>";*/
redirect('cart','refresh');
}
and this is cart view
<?php if($cart = $this->cart->contents()):
?>
<?php echo form_open('cart/update'); ?>
<table id="cart-table" class="table table-condensed">
<thead>
<tr>
<th>Action</th>
<th>Image</th>
<th>Product</th>
<th>Price</th>
<th>Quanity</th>
<th>Sub Total</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
<?php foreach($cart as $item): /* $title = str_replace(' ', '-', $item['title']); ?>
<?php $price = $row['price']; $jumlah_desimal = "0"; $pemisah_desimal =""; $pemisah_ribuan =".";?>
<?php /*echo form_hidden('rowid[]', $item['rowid']);*/
echo form_hidden($i.'[rowid]', $item['rowid']); ?>
<tr>
<th class="product-remove">
<a class="remove" title="Remove this product" href="<?php echo base_url().'cart/delete/'.$item['rowid'];?>">×</a>
</th>
<th>
<div class="media">
<div class="relative">
<a href="shop-single.html" title="">
<img src="<?php echo base_url().$item['images'];?>" alt=""/>
</a>
</div>
</div><!-- end media -->
</th>
<th>
<?php echo $item['title'];?>
</th>
<td>Rp. <?php echo $this->cart->format_number($item['price']); /*echo rupiah($item['price']); */?></td>
<td>
<?php echo form_input(array('name' => $i.'[qty]', 'value' => $item['qty'], 'maxlength' => '3', 'class' => 'j')); ?> pcs
</td>
<td>
Rp. <?php echo $this->cart->format_number($item['subtotal']);/*echo rupiah($item['subtotal']);*/ ?>
</td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td colspan="5" style="text-align: right!important;"><b>Total </b>(biaya buku)</td>
<td>
Rp. <?php echo $this->cart->format_number($this->cart->total()); ?>
</td>
</tr>
</tbody>
</table>
<?php
echo form_close();
else:
echo 'Sorry, Your Cart is empty';
endif;
?>
the result always show
Sorry, Your Cart is empty
where is the bug? anybody can help me :)
thanks :)
Codeigniter cart has predefined format:
https://ellislab.com/codeigniter/user-guide/libraries/cart.html
$data = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
$this->cart->insert($data);
So you code will be,
function add()
{
$data = array(
'id'=>someID,
'name'=>SomeName,
'qty' => $this->input->post('qty'),
'price' => $this->input->post('price'),
'options' =>array('id_books'=> $this->input->post('id_books'),'id_category'=> $this->input->post('id_category'),'images' => $this->input->post('images'),'title' => $this->input->post('title'));
$this->cart->insert($data);
/*print_r($data);*/
/*echo "<meta http-equiv='refresh' content='0; url=".base_url()."cart/'>";*/
redirect('cart','refresh');
}
I have an HTML table with some fields, and each row has a link to save the data. Can I redirect to the controller, but do not know how to get the data in the controller to save.
<table class="table table-bordered">
<thead>
<tr>
<th>User</th>
<th>Value</th>
<th>Category</th>
<th>#</th>
</tr>
</thead>
<tr>
<td>Gleydson</td>
<td><input class="form-control" type="text" name="value"></td>
<td>
<select class="form-control">
<option value="tipo">Type 1</option>
<option value="tipo">Type 2</option>
<option value="tipo">Type 3</option>
</select>
</td>
<td> <?php echo $this->Html->link(__('Register'),'/payments/register/'.$user['User']['id'],array('class' => 'btn btn-success')) ?> </td>
</tr>
<tr>
<td>Emília</td>
<td><input class="form-control" type="text" name="value"></td>
<td>
<select class="form-control">
<option value="tipo">Type 1</option>
<option value="tipo">Type 2</option>
<option value="tipo">Type 3</option>
</select>
</td>
<td> <?php echo $this->Html->link(__('Register'),'/payments/register/'.$user['User']['id'],array('class' => 'btn btn-success')) ?> </td>
</tr>
</table>
You should be using the Form Helper to build your form. Check out the docs here: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
If you're submitting to a different controller you may have to use "action" or "url" within the ->create method. But the issue you're experiencing is that the post data isn't being submitted, mainly because you're using "link" and you're not posting or creating a form correctly.
Make sure to use debug($this->data); within the correct method of the controller to check the data submission.
The method you're using just produces a link (lorem)
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link
It may be worth going through the getting started guides to get a better idea of how Cake works. http://book.cakephp.org/2.0/en/getting-started.html
If you're looking to then redirect the user after the form submission you can use code within your controller to redirect them to the correct location.
Your view:
<?php echo $this->Form->create('Register'); ?>
<table class="table table-bordered">
<thead>
<tr>
<th>User</th>
<th>Value</th>
<th>Category</th>
<th>#</th>
</tr>
</thead>
<tr>
<td>Gleydson</td>
<td><?php echo $this->Html->input('value', array('class' => 'form-control', 'label' => false, 'div' => false)); ?></td>
<td>
<?php echo $this->Form->select('types', array('Type 1', 'Type 2', 'Type 3'), array('class' => 'form-control')); ?>
</td>
<td> <?php echo $this->Html->submit(__('Register'),array('class' => 'btn btn-success')) ?> </td>
</tr>
<tr>
<td>Emília</td>
<td><?php echo $this->Html->input('value2', array('class' => 'form-control', 'label' => false, 'div' => false)); ?></td>
<td>
<?php echo $this->Form->select('types2', array('Type 1', 'Type 2', 'Type 3'), array('class' => 'form-control')); ?>
</td>
<td> <?php echo $this->Html->submit(__('Register'), array('class' => 'btn btn-success')) ?> </td>
</tr>
</table>
<?php echo $this->Form->end(); ?>
Your Controller:
Also see:
http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html
and
http://book.cakephp.org/2.0/en/controllers.html
public function update() {
if ($this->request->is('post')) {
if ($this->Register->validates()) {
return $this->redirect(
array('controller' => 'orders', 'action' => 'thanks')
);
} else {
$this->Session->setFlash(
__('Unable to validate')
);
}
}
}
More info on saving your data:
http://book.cakephp.org/2.0/en/models/saving-your-data.html
I am using following form but when I am submitting form without entering any mandatory fields, then in other input fields values are converting into garbage values.
<?php echo form_open_multipart($formAction); ?>
// form action is coming from controller
<div id="content">
<table border="0" cellpadding="0" cellspacing="5" width="100%" >
<tr>
<td>First Name<em>*</em></td>
<td>
<?php
$data = array('name' => 'firstName');
echo form_input($data, $firstName);
?>
</td>
</tr>
<tr>
<td>Middle Name<em>*</em></td>
<td>
<?php
$data = array('name' => 'middleName');
echo form_input($data, $middleName);
?>
</td>
</tr>
<tr>
<td>Last Name<em>*</em></td>
<td>
<?php
$data = array('name' => 'lastName');
echo form_input($data, $lastName);
?>
</td>
</tr>
<tr>
<td colspan="2">
<div class="formbuttons">
<?php echo form_submit('submit', "Save", "class='button'"); ?>
</div>
</td>
</tr>
</table>
<?php echo form_close(); ?>
If I enter "test's" in first name field and submit form without entering other mandatory fields then in First Name text box it is showing test's.
Try this..
echo form_input($data, html_entity_decode($firstName,ENT_QUOTES));
this should work.