i have a similar thread using javascript but i found some problem and some bugs. so i changed to php
so far this is my code
if(!empty($_POST['check'])) {
foreach($_POST['check'] as $check) {
//get the html data beside the checked checkboxes
}
}else{
echo '<script language="javascript">';
echo 'alert("Nothing checked")';
echo '</script>';
}
and my stripped html code
<tr>
<td><?php echo $r['Name'] ?></td>
<td><?php echo $r['Age'] ?></td>
<td><?php echo $r['Address'] ?></td>
<td><input name="check[]" type="checkbox" ></td>
</tr>
i wan't to get the data on the same row where a checkbox is checked. please help me. thanks
You have to loop through the POST data, and assign $r[NAME] = VALUE
For the PHP code:
array $r;
foreach($_POST as $post=>$value) {
//get the html data beside the checked checkboxes
$r[ $post] = $value;
}
}
}else{
echo '<script language="javascript">';
echo 'alert("Nothing checked")';
echo '</script>';
}
Assuming that the HTML part your referring to is the result page
For the HTML:
<tr>
<td><?php echo $r['Name'] ?></td>
<td><?php echo $r['Age'] ?></td>
<td><?php echo $r['Address'] ?></td>
</tr>
First, lets add some data:
<?php
$data = [
'111' => [
'Name' => 'John',
'Age' => '25',
'Address' => 'Baker street, 11'
],
'222' => [
'Name' => 'Mary',
'Age' => '19',
'Address' => 'Elm street, 14'
],
'333' => [
'Name' => 'Nick',
'Age' => '23',
'Address' => '64th stree, 2'
]
];
?>
Output all data items and set checks from previous posted data:
<form method="POST">
<table>
<?php foreach($data as $id => $item): ?>
<tr>
<td><?= $item['Name'] ?></td>
<td><?= $item['Age'] ?></td>
<td><?= $item['Address'] ?></td>
<td><input name="check[]" type="checkbox" value="<?= $id; ?>"
<?php if (isset($_POST['check']) && in_array($id, $_POST['check'])): ?>
checked="checked"
<?php endif; ?>/></td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" />
</form>
<?php if (empty($_POST['check'])): ?>
<script>alert("Nothing checked");</script>
<?php endif; ?>
You'll get reloaded page with properly marked checkboxes after the form submission.
Related
I have looped through my record, when i use print_r(), i will see all my records, but if i try to display it in a table, it will only show one record.
Please how do i solve this?
<?php foreach ($clists as $clist) ;?>
<tr>
<td><?php echo $clist['Course']['id']; ?></td>
<td><?php echo $this->Html->link($clist['Course']['course_name'], array('controller' => 'this', 'action' => 'this')); ?></td>
<td><?php echo $clist['Course']['course_desc']; ?></td>
<td><?php echo $clist['Course']['closing_day']; ?></td>
</tr> <?php //endforeach; ?> <?php unset($clist); ?> –
}
How do you loop through your array?
Here is a great example on how to echo the results in an array:
<?php
//Example array
$colors = array(
"red",
"green",
"blue",
"yellow"
);
foreach ($colors as $value) {
echo "$value <br>";
}
?>
More info here and here
Also, for example;
$result = array(
[0] => 'DB result 1',
[1] => 'DB result 2',
[2] => 'DB result 3'
);
To show these results in a table for example in this case would be:
<table>
<?php foreach($result as $key => $value) { ?>
<tr>
<td><?= $value ?></td>
</tr>
<?php } // End foreach ?>
</table>
Good luck!
i'm having trouble getting my update to work. delete, view and add work fine.
this is my extraoptie_model:
function update($id, $extraOptie) {
$this->db->where('id', $id);
$this->db->update('extraOptie', $extraOptie);
}
my controller:
function update($id) {
// Check admin if not Acces Denied
$gebruiker = $this->session->userdata('gebruiker');
if ($gebruiker->accountLevel != "admin") {
$data['title'] = 'Acces Denied';
$partials = array('header' => 'main_header', 'content' => 'admin_noacces', 'footer' => 'main_footer');
$this->template->load('main_master', $partials, $data);
} else {
// prefill form values
$extraOptie = $this->extraoptie_model->get($id);
$data['id'] = $id;
$data['beschrijving'] = $extraOptie->beschrijving;
$data['actuelePrijs'] = $extraOptie->actuelePrijs;
$data['soort'] = $extraOptie->soort;
$data['aantalGangen'] = $extraOptie->aantalGangen;
$data['tabelNaam'] = $extraOptie->tabelNaam;
// set common properties
$data['title'] = 'Extra optie update';
$data['message'] = '';
$data['action'] = site_url('admin/extraoptie/updateExtraOptie/');
// load view
$partials = array('header' => 'main_header', 'content' => 'admin_extraoptieedit', 'footer' => 'main_footer');
$this->template->load('main_master', $partials, $data);
}
}
function updateExtraOptie() {
// Check admin if not Acces Denied
$gebruiker = $this->session->userdata('gebruiker');
if ($gebruiker->accountLevel != "admin") {
$data['title'] = 'Acces Denied';
$partials = array('header' => 'main_header', 'content' => 'admin_noacces', 'footer' => 'main_footer');
$this->template->load('main_master', $partials, $data);
} else {
// set common properties
$data['title'] = 'Update extra optie';
$data['action'] = site_url('admin/extraoptie/updateExtraOptie');
// save data
$id = $this->input->post('id');
$extraOptie = array('beschrijving' => $this->input->post('beschrijving'),
'actuelePrijs' => $this->input->post('actuelePrijs'),
'soort' => $this->input->post('soort'),
'aantalGangen' => $this->input->post('aantalGangen'),
'tabelNaam' => $this->input->post('tabelNaam'));
if ($extraOptie['aantalGangen'] == '') {
$extraOptie['aantalGangen'] = null;
}
if ($extraOptie['tabelNaam'] == '') {
$extraOptie['tabelNaam'] = null;
}
$this->extraoptie_model->update($id, $extraOptie);
// set user message
$data['message'] = '<div class="success">update success</div>';
// redirect to index
redirect('admin/extraoptie/index', 'refresh');
}
}
my view:
<table class='centered text'>
<form method="post" action="<?php echo $action; ?>">
<?php
$hidden = array('id' => $id);
form_hidden($hidden);
?>
<tr>
<td><?php echo form_label('Beschrijving:', 'beschrijving'); ?></td>
<td><?php echo form_textarea(array('name' => 'beschrijving', 'id' => 'beschrijving', 'value' => $beschrijving, 'cols' => '30', 'rows' => '5')); ?></td>
</tr>
<tr>
<td><?php echo form_label('Prijs:', 'prijs'); ?></td>
<td><?php echo form_input(array('name' => 'actuelePrijs', 'id' => 'actuelePrijs', 'value' => $actuelePrijs, 'size' => '10')); ?></td>
</tr>
<tr>
<td><?php echo form_label('Soort:', 'soort'); ?></td>
<td><?php echo form_input(array('name' => 'soort', 'id' => 'soort', 'value' => $soort, 'size' => '30')); ?></td>
</tr>
<tr>
<td><?php echo form_label('Aantal gangen:', 'aantalGangen'); ?></td>
<td><?php echo form_input(array('name' => 'aantalGangen', 'id' => 'aantalGangen', 'value' => $aantalGangen, 'size' => '10')); ?></td>
</tr>
<tr>
<td><?php echo form_label('Tabel naam:', 'tabelNaam'); ?></td>
<td><?php echo form_input(array('name' => 'tabelNaam', 'id' => 'tabelNaam', 'value' => $tabelNaam, 'size' => '30')); ?></td>
</tr>
<tr>
<td colspan='2' class='center'>
<?php echo form_submit('submit', 'Opslaan', 'id="opslaan"');; ?>
<?php echo form_button('annuleer', 'Annuleren', 'id="annuleer"'); ?>
<?php form_close(); ?>
</td>
</tr>
</table>
All help is welcome have been working on it for couple of hours and can't seem to find the problem.
Grtz Nella
possibly the issue arises from the fact that
you have a php form_close();
but do not have a php form_open();
not sure offhand how CI would react to a close without an open.
Do you have CSRF protection enabled? If so, you will need to either use form_open(), as steve has said, or add a hidden input with your csrf token such as:
<input type="hidden" name="<?php echo $this->security->get_csrf_hash();?>" value="<?php echo $this->security->get_csrf_token_name();?>"/>
Thank you both for your quick response!
I have found the error after staring at the code for another couple of hours together w/ a friend.
the problem was i didn't echo form_hidden($hidden) so no id was passed through to the update model.
<table class='centered text'>
<form method="post" action="<?php echo $action; ?>">
<?php
$hidden = array('id' => $id);
form_hidden($hidden);
?>
<tr>
so it had to be:
<table class='centered text'>
<form method="post" action="<?php echo $action; ?>">
<?php
$hidden = array('id' => $id);
echo form_hidden($hidden);
?>
<tr>
I want to insert multiple data into multiple rows when a submit btn is clicked
Below is my html form
<?php echo form_open('daily_shop_performance/insert_missing_records');?>
<table class="table" style=" width:100%; ">
<thead>
<tr>
<th>Days</th>
<th>Shop ID</th>
<th>Date</th>
<th>Takings</th>
<th>Payout</th>
<th>Actual Slippage</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>FRI</b></td>
<td><?php echo form_input('BDP_COST_CENTRE_NUMBER', set_value('BDP_COST_CENTRE_NUMBER', ''), 'class=span1' ); ?></td>
<td><?php echo form_input('BDP_DATE', set_value('BDP_DATE', ''), 'class=span2' ); ?></td>
<td><?php echo form_input('BDP_TAKE', set_value('BDP_TAKE', ''), 'class=span2' ); ?></td>
<td><?php echo form_input('BDP_PAYOUT', set_value('BDP_PAYOUT', ''), 'class=span2' ); ?></td>
<td><?php echo form_input('BDP_ACTUAL_SLIPPAGE', set_value('BDP_ACTUAL_SLIPPAGE', ''), 'class=span2' ); ?></td>
</tr>
<tr>
<td><b>SAT</b></td>
<td><?php echo form_input('BDP_COST_CENTRE_NUMBER1', set_value('BDP_COST_CENTRE_NUMBER1', ''), 'class=span1' ); ?></td>
<td><?php echo form_input('BDP_DATE1', set_value('BDP_DATE1', ''), 'class=span2' ); ?></td>
<td><?php echo form_input('BDP_TAKE1', set_value('BDP_TAKE1', ''), 'class=span2' ); ?></td>
<td><?php echo form_input('BDP_PAYOUT1', set_value('BDP_PAYOUT1', ''), 'class=span2' ); ?></td>
<td><?php echo form_input('BDP_ACTUAL_SLIPPAGE1', set_value('BDP_ACTUAL_SLIPPAGE1', ''), 'class=span2' ); ?></td>
</tr>
<tr>
<td><?php echo form_submit('submit', 'Next', 'class="btn btn-large"', 'type="button"'); ?></td>
</tr>
</table>
<?php echo form_close();?>
My insert statement is inserting each record as array
which is working fine, but am not sure how to only insert form fields with data and ignore empty form fields
Below is my insert query
public function insert_missing_records()
{
$insert_data = array(
array(
'BDP_ID'=> '',
'BDP_COST_CENTRE_NUMBER'=> $this->input->post('BDP_COST_CENTRE_NUMBER'),
'BDP_DATE' => $this->input->post('BDP_DATE'),
'BDP_DAY_CODE'=> '1',
'BDP_TAKE'=> $this->input->post('BDP_TAKE'),
'BDP_PAYOUT'=> $this->input->post('BDP_PAYOUT'),
'BDP_ACTUAL_SLIPPAGE'=> $this->input->post('BDP_ACTUAL_SLIPPAGE'),
'BDP_PROCESS_DATE'=> $this->performance_m->date(),
'BDP_RUN_DATE'=> $this->performance_m->date(),
'BDP_CREATED_BY'=> $this->session->userdata('BL_USERNAME'),
'BDP_SOURCE'=> 'M',
'BDP_UPDATE_DATE'=> '',
'BDP_UPDATED_BY'=> $this->session->userdata('BL_USERNAME')
),
array(
'BDP_ID'=> '',
'BDP_COST_CENTRE_NUMBER'=> $this->input->post('BDP_COST_CENTRE_NUMBER1'),
'BDP_DATE' => $this->input->post('BDP_DATE1'),
'BDP_DAY_CODE'=> '2',
'BDP_TAKE'=> $this->input->post('BDP_TAKE1'),
'BDP_PAYOUT'=> $this->input->post('BDP_PAYOUT1'),
'BDP_ACTUAL_SLIPPAGE'=> $this->input->post('BDP_ACTUAL_SLIPPAGE1'),
'BDP_PROCESS_DATE'=> $this->performance_m->date(),
'BDP_RUN_DATE'=> $this->performance_m->date(),
'BDP_CREATED_BY'=> $this->session->userdata('BL_USERNAME'),
'BDP_SOURCE'=> 'M',
'BDP_UPDATE_DATE'=> '',
'BDP_UPDATED_BY'=> $this->session- >userdata('BL_USERNAME')
)
);
$insert = $this->db->insert_batch('WHOUSE1.DLY_BWR_MAN_PERFORMANCE', $insert_data);
echo '<pre>' .$this->db->last_query(). '</pre>';
return $insert;
}
Will be happy if anyone can help.
Thank you
You can assign each field from the form to a variable and check if it's empty. If it is not empty then you add appropriate entry to the array.
Try something like this:
$array1 = array(
'BDP_ID'=> '',
//and others which needs to be inserted always
)
$variable1 = $this->input->post('BDP_COST_CENTRE_NUMBER');
if( !empty($variable) )
{
$array1['BDP_COST_CENTRE_NUMBER'] = $variable1;
}
//and so on for each variable
$insert_data = array(
$array1,
$array2, //same thing for this array
)
To improve this - you can put all parameters into an array (list) and iterate through it instead of writing lots of code. Good luck
I found other people that had the same problem when trying to select multiple checkboxes, only the last one is selected. Here is one and here is another.
People have suggested using options like this
echo $this->Form->input('video',array('options'=> array('Value 1'=>'Label 1',
'Value 2'=>'Label 2',
'Value 3'=>'Label 3'
),
'multiple' => 'checkbox'
));
I don't think either of these answer my question. My checkboxes are generated in a loop, but everyone suggested using "options" to set the values. I have no idea how many checkboxes are going to be generated so I don't see how this would work.
<table>
<?
echo $this->Form->create(null,array(
'onsubmit'=>'return confirm("Are you sure you want to archive?'));
?>
<th>Order ID</th><th>Order Date</th><th>Order Total</th><th>Status</th><th>View</th><th>Select to Archive</th>
<?php foreach ($orders as $order):
?>
<tr>
<td><?php echo $order['Order']['orderid'];?> </td>
<td><?php echo $formatted_date." ".$time; ?></td>
<td><?php echo $order['Order']['total'];?> </td>
<td><?php echo $order['Order']['order_status'];?> </td>
<td><a href="/orders/details/<?php echo $order['Order']['orderid']; ?>"/>View Order</a> </td>
<td><? echo $this->Form->checkbox('archive_value', array('hiddenField' => false, 'value' => $order['Order']['orderid'])); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php //options for the archiving values
$archive_options = array(
'selected' => 'Archive Selected Orders',
'filled' => 'Archive All Filled Orders',
);
?>
<table class = "table_order_status">
<tr>
<td>
<?
echo $this->Form->input('archive_values', array('options' => $archive_options, 'value' => $select_value, 'name' => 'archive'));
?>
</td>
<td>
<?
echo $this->Form->end(__('Submit'));
?>
</td>
</tr>
</table>
<?
Try appending a period to the end of the input element's name, i.e. video. (derived from this forum post). This will apparently cause the elements to adopt the array name style notation, e.g. video[], though it might be Model.video[]. In any case, this should allow you to receive all of the checked values back in the form data.
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.