我是这个论坛的新手,我很难更新来自文本框值的复选框中的所选值。
场景:
我想更新选定的项目,例如,我想更新Item-2,并且将启用接收到的项目中的文本框,我将输入一个数字(例如1),而总接收中的文本框将使用ajax自动求和。
问题是从总计接收到的值提交后,正在更新的记录为空,但是当我尝试检查和print_r时,值在那里。
还有一件事情是,如果选中了所有复选框并为收到的每个项目输入一个数字,则它只会得到的值是最后一个值,并且将被选中的所有复选框都将被更新。
注意: 复选框是一个数组 来自总接收的文本框是一个数组
你们能帮我吗?
这是我的用户界面:
控制器:
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';
}
}
查看:
<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>
型号:
public function modUpdatedynamicval($table, $column, $data, $equal_to){
$this->db->where($column, $equal_to);
$this->db->update($table, $data);
}
任何帮助将不胜感激。
谢谢...
编辑:
让我们假设, 我在item_receive文本框1中输入了1,总接收为10, 在item_receive文本框2中为2,总接收为11, item_receive文本框3中为3,总接收为12,
代码:
$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);
输出:
Check ValueArray ( [0] => 1 [1] => 2 [2] => 3 )
Total RecieveArray ( [0] => 10 [1] => 11 [2] => 12 )
但是,如果我只进入第二个文本框,则输出如下:
Check ValueArray ( [0] => 2 )
Total RecieveArray ( [0] => [1] => 11 [2] => )
答案 0 :(得分:0)
据我所知,您在确定哪个输入索引是哪个后端方面遇到了问题。您可以在每个输入上设置索引键,如下所示:
<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>
在那之后,然后替换for循环:
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
有了这个foreach:
foreach ($check as $key => $item) {
$updateData = array('rec_qty' => $total_rec[$key] );
$this->dbquery->modUpdatedynamicval('tblstock', 'id', $updateData, $check[$key]);
}
因此,您不必手动检查total_rec
的值,而是只检查提交的check
帖子数据。
答案 1 :(得分:0)
可以尝试使用行键(项目ID)。使用$ 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>
场景类似:
如果您检查第一行和第一行的item_id($ row-> id)为2,然后输入7,那么您将得到如下数组:-
print_r($_POST['total_rec']);
输出类似:
Array([2]=>7)
您可以使用数组键轻松找到数据
更新:
<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>
提交操作:例如
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
}
}
链接:-https://prnt.sc/mswdoc 如果检查支票簿并提交,那么您将得到类似的输出 输出:-https://prnt.sc/mswefv
答案 2 :(得分:0)
就像其他人说的那样,您很难找到正确的检查项目行。 我建议您编辑多维数组中表单的命名方式。赌你不知道吗?
所以在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>
,您可以轻松循环浏览每一行的数据,同时验证是否选中了该复选框
<?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>';
}
答案 3 :(得分:0)
您的复选框都具有相同的ID,这总是会导致问题。更改循环中复选框的ID。
<div
v-for="item in faqItems"
:key="item.id"
>
<span class="faq-item-title" style="white-space: pre-wrap;">
{{ item.title }}
</span>
<span class="faq-item-details" style="white-space: pre-wrap;">
{{ item.text }}
</span>
<svg-icon name="chevron-down"></svg-icon>
</div>