我创建了一个类似于购物车的项目。
但是,用户需要在子窗口(Bootstrap Modal)上添加项目,然后提交后会自动在父窗口上显示该项目,而无需刷新页面。
提交项目时遇到问题,这些项目将另存为会话,然后在“父”窗口中检索。
我试图var_dump会话,它可以显示数组,但是我不知道在父窗口中检索会话数据。
控制器:
public function index(){
$data['session'] = is_array($this->session->userdata('item'))?array_values(unserialize($this->session->userdata('item'))) : array();
$data['product'] = $this->test->GetProduct();
$data['category'] = $this->test->GetCategory();
$this->load->view('import/test',$data);
}
public function AddCart(){
$product = $this->input->post('product_type');
$category = $this->input->post('category_code');
$qty = $this->input->post('qty');
$quality = $this->input->post('quality');
$gold_rate = $this->input->post('gold_rate');
$weight = $this->input->post('weight');
$item_remarks = $this->input->post('item_remarks');
$assessed_value = $this->input->post('assessed_value');
$pledge_amt = $this->input->post('pledge_amt');
$Pledge_Item = array(
'Product' => $product,
'Category' => $category,
'Qty' => $qty,
'Quality' => $quality,
'Gold_Rate' => $gold_rate,
'Weight' => $weight,
'Item_Remarks' => $item_remarks,
'Assessed_Value' => $assessed_value,
'Pledge_Amt' => $pledge_amt
);
//$this->session->set_userdata('item',$Pledge_Item);
//$Session = $this->session->userdata('item');
//var_dump($Session);
if(!$this->session->has_userdata('item')) {
$item = array($Pledge_Item);
$this->session->set_userdata('item', serialize($item));
} else {
//$index = $this->exists($product);
$item = array_values(unserialize($this->session->userdata('item')));
array_push($item, $Pledge_Item);
$this->session->set_userdata('item', serialize($item));
}
//var_dump($item);
redirect('Test/index');
}
查看(父窗口)
<table class="table table-bordered" id="result">
<thead>
<tr>
<th scope="col">Product Type</th>
<th scope="col">Category</th>
<th scope="col">Qty</th>
<th scope="col">Quality</th>
<th scope="col">Gold Rate</th>
<th scope="col">Weight</th>
<th scope="col">Item Remarks</th>
<th scope="col">Assessed Value</th>
<th scope="col">Pledge Amount</th>
</tr>
</thead>
<tbody>
<?php foreach($session as $row){?>
<tr>
<td><?php echo $row['Product'];?></td>
</tr>
<?php } ?>
</tbody>
</table>
查看(子窗口引导程序模型)
<div class="modal-body">
<?php echo form_open("Test/AddCart"); ?>
<tr>
<td>
<?php
$options = array(
'' => '~Select Product~'
);
foreach ($product as $prod){
$options[$prod->product_id] = $prod->product_type;
}
$select = array(
'name' => 'product_type[]',
'id' => 'product_type',
'class' => 'form-control form-pledge-item',
'required' => 'required'
);
echo form_dropdown('product_type[]', $options,set_value('product_type'),$select);
?>
</td>
</tr><br>
<tr>
<td>
<?php
$options = array(
'' => '~Select Category~'
);
foreach ($category as $categ){
$options[$categ->category_id] = $categ->category_code;
}
$select = array(
'name' => 'category_code[]',
'id' => 'category_code',
'class' => 'form-control form-pledge-item',
'required' => 'required'
);
echo form_dropdown('category_code[]', $options,set_value('category_code'),$select);
?>
</td>
</tr><br>
<tr>
<td>
<input type="number" name="qty[]" class="form-control form-pledge-item" placeholder="Quantity" min="0" required/>
</td>
</tr><br>
<tr>
<td>
<input type="text" name="quality[]" class="form-control form-pledge-item" placeholder="Quality"/>
</td>
</tr><br>
<tr>
<td>
<input type="number" name="gold_rate[]" class="form-control form-pledge-item" placeholder="Gold Rate/gm" min="0" step="any"/>
</td>
</tr><br>
<tr>
<td>
<input type="number" name="weight[]" class="form-control form-pledge-item" placeholder="Weight" min="0" step="any"/>
</td>
</tr><br>
<tr>
<td>
<input type="text" name="item_remarks[]" class="form-control form-pledge-item" placeholder="Item Remarks"/>
</td>
</tr><br>
<tr>
<td>
<input type="number" name="assessed_value[]" class="form-control form-pledge-item" placeholder="Assessed Value" min="0" step="any"/>
</td>
</tr><br>
<tr>
<td>
<input type="number" name="pledge_amt[]" class="form-control form-pledge-item" placeholder="Pledge Amount" min="0" step="any"/>
</td>
</tr><br>
<!--<button value="submit" type="button" class="btn btn-primary col-md-12" id="add_row">Add</button>-->
<button name="submit" value="submit" class="btn btn-info col-md-12"><i class="fa fa-check-circle" aria-hidden="true"></i> Add Item</button>
<!--<a class="btn btn-info col-md-12" href="<?php echo base_url() .'index.php/Test/AddCart/'; ?>">   <i class="fa fa-money" aria-hidden="true"></i> Add</a>-->
<br>
<?php echo form_close(); ?>
</div>
答案 0 :(得分:0)
<div class="modal-body">
<form action="javascript:void(0)" id="addcard">
<tr>
<td>
<?php
$options = array(
'' => '~Select Product~'
);
foreach ($product as $prod){
$options[$prod->product_id] = $prod->product_type;
}
$select = array(
'name' => 'product_type[]',
'id' => 'product_type',
'class' => 'form-control form-pledge-item',
'required' => 'required'
);
echo form_dropdown('product_type[]', $options,set_value('product_type'),$select);
?>
</td>
</tr><br>
<tr>
<td>
<?php
$options = array(
'' => '~Select Category~'
);
foreach ($category as $categ){
$options[$categ->category_id] = $categ->category_code;
}
$select = array(
'name' => 'category_code[]',
'id' => 'category_code',
'class' => 'form-control form-pledge-item',
'required' => 'required'
);
echo form_dropdown('category_code[]', $options,set_value('category_code'),$select);
?>
</td>
</tr><br>
<tr>
<td>
<input type="number" name="qty[]" class="form-control form-pledge-item" placeholder="Quantity" min="0" required/>
</td>
</tr><br>
<tr>
<td>
<input type="text" name="quality[]" class="form-control form-pledge-item" placeholder="Quality"/>
</td>
</tr><br>
<tr>
<td>
<input type="number" name="gold_rate[]" class="form-control form-pledge-item" placeholder="Gold Rate/gm" min="0" step="any"/>
</td>
</tr><br>
<tr>
<td>
<input type="number" name="weight[]" class="form-control form-pledge-item" placeholder="Weight" min="0" step="any"/>
</td>
</tr><br>
<tr>
<td>
<input type="text" name="item_remarks[]" class="form-control form-pledge-item" placeholder="Item Remarks"/>
</td>
</tr><br>
<tr>
<td>
<input type="number" name="assessed_value[]" class="form-control form-pledge-item" placeholder="Assessed Value" min="0" step="any"/>
</td>
</tr><br>
<tr>
<td>
<input type="number" name="pledge_amt[]" class="form-control form-pledge-item" placeholder="Pledge Amount" min="0" step="any"/>
</td>
</tr><br>
<!--<button value="submit" type="button" class="btn btn-primary col-md-12" id="add_row">Add</button>-->
<button name="submit" value="submit" class="btn btn-info col-md-12"><i class="fa fa-check-circle" aria-hidden="true"></i> Add Item</button>
<!--<a class="btn btn-info col-md-12" href="<?php echo base_url() .'index.php/Test/AddCart/'; ?>">   <i class="fa fa-money" aria-hidden="true"></i> Add</a>-->
<br>
</form>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#addcaard").on('submit', function(){
var url='Test/AddCart';
var formArray=$(this).serializeArray();
$.ajax({
type: 'POST',
url: url,
data:formArray,
success: function(data){
$("#result").children("tbody").append(data);
},
error: function(err){
console.log(err);
}
});
});
})
</script>
这是控制器
公共功能
AddCart(){
$product = $this->input->post('product_type');
$category = $this->input->post('category_code');
$qty = $this->input->post('qty');
$quality = $this->input->post('quality');
$gold_rate = $this->input->post('gold_rate');
$weight = $this->input->post('weight');
$item_remarks = $this->input->post('item_remarks');
$assessed_value = $this->input->post('assessed_value');
$pledge_amt = $this->input->post('pledge_amt');
$data = "<tr><td>".$product."</td><td>".$category."</td><td>".$qty."</td><td>".$quality."</td><td>".$gold_rate."</td><td>".$weight."</td><td>".$item_remarks."</td><td>".$assessed_value."</td><td>".$pledge_amt."</td></tr>";
echo $data;
}