how to insert data into array without replacing previous data - php

I'm trying to insert data into an array, I want it to add the new data without replacing the previous data
I'm trying to use array_push but it still replaces the previous data.
<?php
if (isset ($_POST['submit']; )) {
$num = 0.5;
$data[] = array(
'no1' => $_POST['no1'],
'no2' => $_POST['no2'],
'no3' => $_POST['no3']
);
array_push($data, array(
'no1' => $_POST['no1'],
'no2' => $_POST['no2'],
'no3' => $_POST['no3']
));
}
?>
and this is the form
<h4>Add data</h4>
<form class="" action="" method="post">
<ul>
<li>
<label for="ni1">No 1 :</label>
<input type="number" id="no1" name="no1" value="">
</li>
<li>
<label for="ni1">No 2 :</label>
<input type="number" id="no2" name="no2" value="">
</li>
<li>
<label for="ni1">No 3 :</label>
<input type="number" id="no3" name="no3" value="">
</li>
</ul>
<button type="submit" name="submit">ADD</button>
</form>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>No 1</td>
<td>No 2</td>
<td>No 3</td>
</tr>
<?php foreach ($data as $n): ?>
<tr>
<td><?= $n['no1']* $num ?></td>
<td><?= $n['no2']* $num ?></td>
<td><?= $n['no3']* $num ?></td>
</tr>
<?php endforeach; ?>
</table>
I expect the new data inserted not replacing the previous data and showed into the table

You can use Push like this function for example:
<?php
$dataArray = array('Mathematics','Physics');
array_push($dataArray,'Chemistry','Biology');
print_r($dataArray);
?>
output:
Array ([0] => Mathematics [1] => Physics [2] => Chemistry [3] => Biology)

Related

Values from textbox array not updating

I'm new in this forum and I'm having a hard time updating selected value from checkbox coming from the textbox value.
Scenario:
I want to update selected items, For example, I want to update Item-2 and the textbox from item received will be enabled, I will enter a number for example 1 and the textbox from total receive will automatically sum using AJAX.
The problem is after submitting the value from total receive the records that were updating is blank, but when I try to check and print_r the value is there.
And one more thing if all checkboxes are checked and enter a number for each item received, the value that it only get is the last value and will be updated will all selected checkbox.
Notes:
The checkbox is an array
Textbox from total receive is an array
Here's my UI:
Controller:
public function recitem_insert(){
$this->load->model('dbquery');
$check = $this->input->post('check');
$total_rec = $_POST['total_rec'];
if(isset($check)){ //Check if check is checked
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$updateData = array('rec_qty' => $total_rec[$j] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$i]);
}
}//end for loop
echo "<script type='text/javascript'>
alert('Successfully Added!');
window.close();
</script>";
}else{ //End If
echo 'Please select a checkbox';
}
}
View:
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<div class="box">
<div class="box-header">
<h3 class="box-title">System ID: <b><?php echo $process_id; ?></b></h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Action</th>
<th>Item Code</th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $row){
?>
<tr>
<td><input type="checkbox" name="check[]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[]"></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
Model:
public function modUpdatedynamicval($table, $column, $data, $equal_to){
$this->db->where($column, $equal_to);
$this->db->update($table, $data);
}
Edit
Let's assume:
I've input 1 in item_receive textbox one and the total receive will be 10,
2 in item_receive textbox two and the total receive will be 11,
3 in item_receive textbox three and the total receive will be 12,
CODE:
$check = $this->input->post('check');
$total_rec = $_POST['total_rec'];
echo 'Check Value';
print_r($check);
echo '<br><br>';
echo 'Total Recieve';
print_r($total_rec);
OUTPUT:
Check ValueArray ( [0] => 1 [1] => 2 [2] => 3 )
Total RecieveArray ( [0] => 10 [1] => 11 [2] => 12 )
But If I only input the second textbox here's the output:
Check ValueArray ( [0] => 2 )
Total RecieveArray ( [0] => [1] => 11 [2] => )
From what I see, you are having a problem to determine which input index is which on the back end. You could set an index key on each of the input like this :
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $key => $row){ //added key to be used as inputs key
?>
<tr>
<td><input type="checkbox" name="check[<?php echo $key ?>]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[<?php echo $key ?>]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[<?php echo $key ?>]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[<?php echo $key ?>]"></td>
</tr>
<?php
}
?>
</tbody>
After that, then replace the for loop :
for($i=0;$i<sizeof($check);$i++){
for($j=0;$j<sizeof($total_rec);$j++){
$updateData = array('rec_qty' => $total_rec[$j] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$i]);
}
}//end for loop
With this foreach :
foreach ($check as $key => $item) {
$updateData = array('rec_qty' => $total_rec[$key] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$key]);
}
So you don't manually check for the total_rec value but instead it only checks on submitted check post data.
from what i can see, like everybody else is saying, you have trouble in finding the correct rows of checked item.
I suggest you edit the way you name the form in multidimensional array. bet you didnt know that?
so in view.php
<form method="post" action="<?php echo base_url() ?>user/recitem_insert">
<div class="box">
<div class="box-header">
<h3 class="box-title">System ID: <b><?php echo $process_id; ?></b></h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Action</th>
<th>Item Code</th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
//WTF?? it's 2019. Say hello to SQL Injection
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $i=>$row){
?>
<tr>
<td><input type="checkbox" name="data[<?php echo $i;?>][check]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="data[<?php echo $i;?>][last_item_rec]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="data[<?php echo $i;?>][item_rec]" id="txt" disabled=""></td>
<td><input type="text" name="data[<?php echo $i;?>][total_rec]"></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
and you can easily cycle through the data for each rows while at the same time validating if the checkbox is checked
<?php
//simple way of traversing the data
if ( isset( $_POST['data'] ) )
{
echo '<table>';
foreach ( $_POST['data'] as $d )
{
//you have all the data here
//start checking on checked box
if(isset($d['check']))
{
echo '<tr>';
echo ' <td>', $d['last_item_rec'], '</td>';
echo ' <td>', $d['item_rec'], '</td>';
echo ' <td>', $d['total_rec'], '</td>';
echo '</tr>';
}
}
echo '</table>';
}
Can try with row key (item id). Update your array key with $row->id.
<tbody>
<?php
$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
foreach ($query->result() as $row){
?>
<tr>
<td><input type="checkbox" name="check[]" id="opt" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row->item_code; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->qty_order; ?></td>
<td><?php echo $row->rec_qty; ?></td>
<input type="hidden" name="last_item_rec[<?php echo $row->id; ?>]" value="<?php echo $row->rec_qty; ?>">
<td><input type="text" name="item_rec[<?php echo $row->id; ?>]" id="txt" disabled=""></td>
<td><input type="text" name="total_rec[<?php echo $row->id; ?>]"></td>
</tr>
<?php
}
?>
</tbody>
Scenraio Like:
1 item Id "2"
2 item Id "3"
3 item Id "5"
If you check first st row and first row item_id ($row->id) is 2 and enter 7 than you will get array like :-
print_r($_POST['total_rec']);
Output Like:
Array([2]=>7)
You can easy to find data using array key
Update:
<form method="post" action="<?php echo base_url(); ?>">
<div class="box">
<div class="box-header">
<h3 class="box-title">
System ID: <b> <?php echo '12'; ?></b>
</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th> Action</th>
<th>Item Code </th>
<th>Item Description</th>
<th>Required QTY Order</th>
<th>Last QTY Recieve</th>
<th>Item Recieve</th>
<th>Total Recieve</th>
</tr>
</thead>
<tbody>
<?php
//$query = $this->db->query("SELECT * FROM tblstock where process_id = '$process_id'");
$new_item_list = array(
array('id'=> '1','item_code'=> 'ITEM-1','description' => 'ITEM Desc', 'qty_order'=> '2','rec_qty'=> '2'),
array('id'=> '2','item_code'=> 'ITEM-2','description' => 'ITEM Desc 2', 'qty_order'=> '3','rec_qty'=> '3' ),
array('id'=> '3','item_code'=> 'ITEM-3','description' => 'ITEM Desc', 'qty_order'=> '5','rec_qty'=> '4' ),
array('id'=> '4','item_code'=> 'ITEM-4','description' => 'ITEM Desc', 'qty_order'=> '1','rec_qty'=> '3' )
);
foreach ($new_item_list as $row){
?>
<tr>
<td><input type="checkbox" name="<?php echo $row['id']; ?>[check]" id="opt" value="<?php echo $row['id']; ?>" onclick="valueChanged()"> </td>
<td><?php echo $row['item_code']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['qty_order']; ?></td>
<td><?php echo $row['rec_qty']; ?></td>
<input type="hidden" name="<?php echo $row['id']; ?>[last_item_rec]" value="<?php echo $row['rec_qty']; ?>">
<td><input type="text" name="<?php echo $row['id']; ?>[item_rec]" id="txt" value='' ></td>
<td><input type="text" name="<?php echo $row['id']; ?>[total_rec]" value=''>
</td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
<!-- /.box-body -->
</div>
<div class="box-footer">
<button type="submit" class="btn bg-olive btn-flat margin">Submit</button>
</div>
</form>
Submit Action: For example
foreach($_POST as $key => $value){
if(isset($value['check'])){
//Row ID of item table $key
$total = $value['total_rec'] * $value['item_rec'];
echo "Total Number :".$total."</br>";
#here is your update query
}
}
Link:- https://prnt.sc/mswdoc
If check checkbook and submit from then you will get output like
Output:- https://prnt.sc/mswefv
Your checkboxes all have the same ID, which always leads to problems. Change the id of the checkbox in the loop.
<td><input type="checkbox" name="check[]" id="opt-<?php echo $row->id; ?>" value="<?php echo $row->id; ?>" onclick="valueChanged()"> </td>

Value not posting for first row generated with php foreach

Need assistance with following code. Value for "customer_id" is not posting after hitting submit for the first generated row only. the second and following work as intended.
Other pages use this same format for deleting, but do not redirect to another page like this one does. No issues with deleting.
<html>
<!-- the body section -->
<body>
<main>
<section>
<!-- display a table of customers -->
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<?php foreach($customers as $cus) : ?>
<tr>
<td><?php echo $cus['firstName'].' '.$cus['lastName']; ?></td>
<td><?php echo $cus['email']; ?></td>
<td class="right"><?php echo $cus['city']; ?></td>
<td><form action="view_and_update.php" method="post" id="search">
<input type="text" name="customer_id" value="<?php echo $cus['customerID']; ?>">
<input name="submit" type="submit" value="Select">
<?php include "redirect.php";?>
</form></td>
</tr>
<?php endforeach; ?>
</table>
</section>
</main>
</body>
</html>
redirect.php looks like this:
<?php
if(isset($_POST["submit"])){
// To redirect form on a particular page
header("Location: view_and_update.php");
}
?>
Any assistance is greatly appreciated. This is for an assignment, and we are using the languages as specified with XAMPP/myphpadmin. No problem retrieving, editing, adding, or deleting data in any other circumstance. This is the only problem page.
By using the following example code I got the correct post to view_and_update.php
It has to be something else, it is not the code.
<?php $customers = [
['firstName' => 'Alice', 'lastName' => 'Al', 'email' => 'alison#domain.com', 'city' => 'MyCity', 'customerID' => 123],
['firstName' => 'Bob', 'lastName' => 'Bo', 'email' => 'bob#domain.com', 'city' => 'MyCity', 'customerID' => 124]
]; ?>
<html>
<!-- the body section -->
<body>
<main>
<section>
<!-- display a table of customers -->
<h2>Results</h2>
<table>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>City</th>
<th> </th>
</tr>
<?php foreach($customers as $cus) : ?>
<tr>
<td><?php echo $cus['firstName'].' '.$cus['lastName']; ?></td>
<td><?php echo $cus['email']; ?></td>
<td class="right"><?php echo $cus['city']; ?></td>
<td><form action="view_and_update.php" method="post" id="search">
<input type="text" name="customer_id" value="<?php echo $cus['customerID']; ?>">
<input name="submit" type="submit" value="Select">
</form></td>
</tr>
<?php endforeach; ?>
</table>
</section>
</main>
</body>
</html>

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.

How to submit multiple rows tabular form data in database using code igniter?

Hello i am creating a school management system and facing a little problem.
The problem is that i am having a complex form containing following fields in a table
I have a table fee structure in which i want to insert data.
having columns
id | classID | sectionID | feetypeID | must | amount | active
1 1 1 1 must 500 yes
2 1 1 2 optional 500 no
so how to create an array such that it uses above form to insert data in table.
Basically i want users to choose feetype they want to have for classes and set their amount and then choose classes they want to apply it for so an entry is created in database for each section and each feetype they select.
I don't know how to make controller and model for this but i have this code of view
View
<form class="form-horizontal" role="form" method="post">
<div class="box-body">
<div class="row">
<div class="col-sm-12">
<h3>Fee Types</h3><br>
<div id="hide-table">
<table id="example1" class="table table-striped table-bordered table-hover dataTable no-footer">
<thead>
<tr>
<th class="col-sm-2"><?=$this->lang->line('slno')?></th>
<th class="col-sm-2"><?=$this->lang->line('feetype')?></th>
<th class="col-sm-2"><?=$this->lang->line('amount')?></th>
<th class="col-sm-2"><?=$this->lang->line('must')?></th>
<th class="col-sm-2"><?=$this->lang->line('active')?></th>
<th class="col-sm-2"><input type="checkbox"> Select All</th>
</tr>
</thead>
<tbody>
<?php if(count($feetypes)) {$i = 1; foreach($feetypes as $feetype) { ?>
<tr>
<td data-title="<?=$this->lang->line('slno')?>">
<?php echo $i; ?>
</td>
<td data-title="<?=$this->lang->line('feetype_name')?>" name="feetypeID" value="<?php echo $feetype->feetypeID;?>">
<?php echo $feetype->feetype; ?>
</td>
<td data-title="<?=$this->lang->line('amount')?>" name="amount">
<input type="text" class="form-control" value="<?php echo $feetype->defaultamount; ?>">
</td>
<td data-title="<?=$this->lang->line('must')?>">
<select class="form-control" name="must">
<option value="MUST">Must</option>
<option value="OPTIONAL">Optional</option>
</select>
</td>
<td data-title="<?=$this->lang->line('active')?>">
<select class="form-control" name="active">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
<td data-title="<?=$this->lang->line('select')?>">
<input type="checkbox" name="selected">
</td>
</tr>
<?php $i++; }} ?>
</tbody>
</table>
</div>
<div class="row">
<div class="col-sm-8">
<h3> Apply To</h3><br>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<table class="table table-bordered table-striped">
<thead>
<th class="col-sm-3">Class</th>
<th class="col-sm-4">Section</th>
</thead>
<tbody>
<?php
foreach ($categories as $category)
{
?>
<tr>
<td> <input type="checkbox"><span style="font-size:18px;margin-left:10px;"> <?php echo $category->classes; ?></span>
</td>
<td>
<?php
if(!empty($category->subs)) {
foreach ($category->subs as $sub) {
echo "<input type='checkbox'> ".$sub->section.' ' ;
}
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<br><br>
<div class="form-group">
<div class="col-sm-12">
<input type="submit" class="btn btn-success" value="<?=$this->lang->line("add_feestructure")?>" >
</div>
</div>
</div>
</form>
Controller
public function addfeestructure(){
$usertype = $this->session->userdata("usertype");
if($usertype == "Admin" || $usertype == "Accountant"){
$this->data['categories'] = $this->feestructure_m->get_categories();
$this->data['feetypes'] = $this->feestructure_m->get_feetypes();
if($_POST){
}
$this->data['subview'] = "feestructure/addfeestructure";
$this->load->view('_layout_main', $this->data);
}
}
To submit multiple fields of the same name you need to add square brackets to your the name attribute like this; <input type="text" name="fieldName[]">.
Then in your controller you'll use a foreach loop to process the form:
public function submitForm() {
$fieldA = $this->input->post('fieldName'); // this will be an array
for ($i = 0; $i < sizeof($fieldA); $i++) {
$array = array('fieldA' => $fieldA[$i]);
$this->your_model->addRecordToDatabase($tableName, $array);
}
}

Display only the checkbox checked data on form submit

I have a form that consists of a table with four data cells for comparison in it and also each column has a check box. When I click on the checkbox and submit the form then I want to display only the checked tables in new page. How can I do that? Can anyone help me with working example?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comparision of Printers</title>
</head>
<body>
<form action="newpage.php" method="get">
<table width="1023" border="1">
<tr>
<td width="193"></td>
<td class="p1" width="113"><img src="images/upmini.png"/>
<label>
<input type="checkbox" name="p[]" value="Printer1" id="CheckboxGroup1_0" />
Printer1</label>
</td>
<td class="p2" width="67"><img src="images/upmini.png"/>
<label>
<input type="checkbox" name="p[]" value="Printer2" id="CheckboxGroup1_1" />
Printer2</label></td>
<td class="p3" width="67"><img src="images/upmini.png"/><label>
<input type="checkbox" name="p[]" value="Printer3" id="CheckboxGroup1_2" />
Printer3</label></td>
<td class="p4" width="67"><img src="images/upmini.png"/><label>
<input type="checkbox" name="p[]" value="Pritner4" id="CheckboxGroup1_3" />
Printer4</label></td>
</tr>
<tr>
<td>Title</td>
<td class="p1" >Printer1</td>
<td class="p2" >Printer2</td>
<td class="p3" >Printer3</td>
<td class="p4" >Printer4</td>
</tr>
<tr>
<td>Manufacturer</td>
<td class="p1" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
<td class="p2" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
<td class="p3" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
<td class="p4" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
</tr>
</table>
<input type="submit" name="compare" value="compare" />
</form>
</body></html>
I think you should use POST instead of GET and store your printer and manufacturer information in a array. i use a 2D array to store the information to make it easy. and get the key values of this array in GET REQUEST and than print only those which match the keys. very simple..
this is full functional code ( changed little bit )
Comparision of Printers
</head>
<body>
// store your infomation in a arrya and use KEY same as in your form checkbox name
<?php
$printers = array(
'printer1' => array(
'title' => 'printer1',
'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 1'
),
'printer3' => array(
'title' => 'printer2',
'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 2'
),
'printer2' => array(
'title' => 'printer3',
'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 3'
),
'printer4' => array(
'title' => 'printer4',
'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 4'
)
)
?>
//if form submited then print the submited index only
<?php if(isset($_GET['p'])):?>
<?php $printerIndex = $_GET['p'];?>
<table width="1023" border="1">
<td>Title</td>
<?php foreach($printerIndex as $index): ?>
<td class = "p1" ><?php echo $printers[$index]['title']; ?></td>
<?php endforeach; ?>
</tr>
<tr>
<td>Manufacturer</td>
<?php foreach($printerIndex as $index): ?>
<td class = "p1" ><?php echo $printers[$index]['manufacturer']; ?></td>
<?php endforeach; ?>
</tr>
</table>
<?php endif; ?>
//make the table
<?php if(!isset($_GET['p'])): ?>
<form action="" method="get">
<table width="1023" border="1">
<tr>
<td width="193"></td>
<?php foreach($printers as $printer ): ?>
<td class="p1" width="113"><img src="images/upmini.png"/>
<label> <?php echo $printer['title']; ?></label>
<input type="checkbox" name="p[]" value="<?php echo $printer['title']; ?>" id="CheckboxGroup1_0" />
</td>
<?php endforeach; ?>
</tr>
<tr>
<td>Title</td>
<?php foreach($printers as $printer): ?>
<td class = "p1" ><?php echo $printer['title']; ?></td>
<?php endforeach; ?>
</tr>
<tr>
<td>Manufacturer</td>
<?php foreach($printers as $printer): ?>
<td class = "p1" ><?php echo $printer['manufacturer']; ?></td>
<?php endforeach; ?>
</tr>
</table>
<input type="submit" name="compare" value="compare" />
</form>
<?php endif; ?>
</body></html>
The checked printers are stored as array p in the $_GET variable. Eg. checking printer 1 and 3 in your example will show the following result:
<?php var_dump($_GET) ?>
array
'p' =>
array
0 => string 'Printer1' (length=8)
1 => string 'Printer3' (length=8)
'compare' => string 'compare' (length=7)
You have only ONE table with several TDs.
So within newpage.php you can do sth like:
if (isset($_GET['P']))
{
$P = $_GET['P'];
for ($i=0;$i<sizeof($P);$i++)
{
// Show what you like, e.g.
echo "Printer".$P[$i]."<br />";
}
}
Is it what you've meant?

Categories