我正在使用下面的功能向购物车添加一个项目(现在是一个数组,稍后将存储在会话中)。一切正常,唯一的问题是,当我单击“ 添加+ ”按钮时,它会将所有值存储在数组中,但之后会显示空白页。
我的控制器
if($action == 'Add+') {
if($this->session->userdata('cart')== ''){
$image=$this->input->post('hidden_image');
$name= $this->input->post('hidden_name');
$price= $this->input->post('hidden_price');
$cat= $this->input->post('hidden_cat');
$id= $this->input->post('hidden_id');
$product = array("name"=>$name, "price"=>$price, "id"=>$id, "image" => $image,"cat"=>$cat);
$item= array("0"=>$product);
$this->session->set_userdata('cart',$item);
}
else{
$count= count($this->session->userdata('cart'));
$name= $this->input->post('hidden_name');
$price= $this->input->post('hidden_price');
$id= $this->input->post('hidden_id');
$image=$this->input->post('hidden_image');
$cat= $this->input->post('hidden_cat');
$product = array("name"=>$name, "price"=>$price, "id"=>$id, "image" => $image,"cat"=>$cat);
$item= array($count=>$product);
$data = $this->session->userdata('cart');
$_SESSION["cart"][$count] = $product;
}
}
if($action == 'View') {
$image=$this->input->post('hidden_image');
$name= $this->input->post('hidden_name');
$price= $this->input->post('hidden_price');
$id= $this->input->post('hidden_id');
$cat= $this->input->post('hidden_cat');
$product = array("name"=>$name, "price"=>$price, "id"=>$id, "image" => $image,"cat"=>$cat);
$this->load->view('product_head');
$this->load->view('header',$product);
$this->load->view('product',$product);
}
我的视图
<?php $data= array ('id'=>'productform');
echo form_open_multipart('loader/product',$data);
$data = array(
'type' => 'hidden',
'name' => 'hidden_cat',
'value' => $row->category);
$val = set_value('hidden_cat');
echo form_input($data, $val);
$data = array(
'type' => 'hidden',
'name' => 'hidden_name',
'value' => $row->name
);
$val = set_value('hidden_name');
echo form_input($data, $val);
$data = array(
'type' => 'hidden',
'name' => 'hidden_image',
'value' => $row->image
);
$val = set_value('hidden_image');
echo form_input($data, $val);
$data = array(
'type' => 'hidden',
'name' => 'hidden_id',
'value' => $row->id
);
$val = set_value('hidden_id');
echo form_input($data, $val);
$data = array(
'type' => 'hidden',
'name' => 'hidden_price',
'value' => $row->price
);
$val = set_value('hidden_price');
echo form_input($data, $val);
$data = array(
'name' => 'action',
'style' => ' background-color: #4CAF50; border: none;',
'value' => 'View'
);
echo form_submit($data);
$data = array(
'name' => 'action',
'style' => ' background-color: #4CAF50; border: none;',
'id' => 'addbtn'
);
echo form_submit($data);
echo form_close();
?>
我想要的是,在成功提交表单之后,它将保留在同一页面上的同一位置,因为对于许多产品而言,有很多页面都具有这些隐藏的表单。
答案 0 :(得分:0)
您需要添加重定向
/**
* This function is used to add new user to the system
*/
function addNewUser()
{
if($this->isAdmin() == TRUE)
{
$this->loadThis();
}
else
{
$this->form_validation->set_rules('fname','Full Name','trim|required|max_length[128]');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|max_length[128]');
$this->form_validation->set_rules('password','Password','required|max_length[20]');
$this->form_validation->set_rules('cpassword','Confirm Password','trim|required|matches[password]|max_length[20]');
$this->form_validation->set_rules('role','Role','trim|required|numeric');
$this->form_validation->set_rules('mobile','Mobile Number','required|min_length[10]');
if($this->form_validation->run() == FALSE)
{
$this->addNew();
}
else
{
$name = ucwords(strtolower($this->security->xss_clean($this->input->post('fname'))));
$email = strtolower($this->security->xss_clean($this->input->post('email')));
$password = $this->input->post('password');
$roleId = $this->input->post('role');
$mobile = $this->security->xss_clean($this->input->post('mobile'));
$userInfo = array('email'=>$email, 'password'=>getHashedPassword($password), 'roleId'=>$roleId, 'name'=> $name,
'mobile'=>$mobile, 'createdBy'=>$this->vendorId, 'createdDtm'=>date('Y-m-d H:i:s'));
echo print($name);
echo '<pre>';print_r($userInfo);
exit();
$this->load->model('user_model');
$result = $this->user_model->addNewUser($userInfo);
if($result > 0)
{
$this->session->set_flashdata('success', 'New User created successfully');
}
else
{
$this->session->set_flashdata('error', 'User creation failed');
}
redirect('addNew');
}
}
}
按照此示例redirect('addNew');