如何在使用codeigniter刷新页面时停止插入数据

时间:2015-05-23 17:24:34

标签: php codeigniter

我需要在刷新页面时停止插入数据 我在codeigniter中的新功能并不能轻松地重定向,请支持 view:pages / home.php视图,其中有一个表单

print 'test'
print 'test'
go

模型:add_model 具有insert_to_db函数的模型     

<html>
<head></head>
</body>
<?php echo validation_errors(); ?>
<?php echo form_open('speed/insert_to_db'); ?>

Branch: <input type="text" name="branch" /><br/>
Business Unit: <input type="text" name="buinessUnit" /><br/>
Device Type: <input type="text" name="deviceType" /><br/>
Brand: <input type="text" name="brand" /><br/>
Device Model: <input type="text" name="deviceModel" /><br/>
SN: <input type="text" name="SN" /><br/>
status: <input type="text" name="status" /><br/>
department: <input type="text" name="department" /><br/>
username: <input type="text" name="username" /><br/>
notes: <input type="textarea" name="notes" /><br/>
computername: <input type="text" name="computerName" /><br/>
Save:<input type="submit" name="save" />

</form>
</body>
</html>

控制器:

       public function insert_into_db(){
           $post=$this->input->post();
           if(!isset($post['save'])) return;
           $data=array('Branch'=>$post['branch'],'BusinessUnit'=>$post['buinessUnit'],'DeviceType'=>$post['deviceType'],'Brand'=>$post['brand'],'DeviceModel'=>$post['deviceModel'],'SN'=>$post['SN'],'Status'=>$post['status'],'Departmant'=>$post['department'],'UserName'=>$post['username'],'Notes'=>$post['notes'],'ComputerName'=>$post['computerName']);
           $this->db->insert('hardware_assets', $data);
return $this->db->insert_id(); // if using mysql
       }
}

2 个答案:

答案 0 :(得分:0)

使用$ this-&gt; input-&gt; post('input_name')而不是原始代码&amp;在控制器中检查表单提交。即

<?php

class Speed extends CI_Controller {

    function insert_to_db()
       {

         $this->load->model('add_model');
         if($this->input->post('save')){
            $this->add_model->insert_into_db();
            $this->load->view('pages/home');//loading success view
         }


       }
}

还从模型中删除if(!isset($post['save'])) return;

答案 1 :(得分:0)

首先,我在模型中没有任何$post$get数据。使用CI可以这样做,但是,在控制器中获取数据,验证数据然后将其传递给模型更为标准。

其次,查看Form Validation Library

的文档

它提供了一些很好的,简单的示例,说明如何处理表单提交。

停止表单重新提交(IMO)的最简单方法是在从提交中获取所有数据后重定向。您可以使用url_helper文件中的redirect()功能执行此操作。

希望这有帮助!