【问题标题】:pass values from view page to controller page in codeigniter在codeigniter中将值从视图页面传递到控制器页面
【发布时间】:2015-12-17 05:23:38
【问题描述】:

我正在尝试通过 php CodeIgniter 将数据插入数据库,并在提交按钮期间将结果显示在同一页面中。下面是我的代码。问题是我没有从视图页面中获取任何值,我检查了 print_r 导致一个空数组。请帮忙。查看页面

<div class="container col-lg-4">
<?php echo validation_errors(); ?>
<?php echo form_open('Welcome/gallery1'); ?>                         
<div class="form-group has-info">
<input type="hidden" name="id" value="">
<br>
<label class="control-label col-lg-6" for="inputSuccess">Offer title
</label>
<input type="text" class="form-control col-lg-6" name="offered" value="<?php if(isset($_POST['offered'])) echo $_POST['offered'];  ?>" id="offered">
<label class="control-label col-lg-6" for="inputSuccess">Offer Description
</label>
<textarea id="description" name="description" value="<?php if(isset($_POST['description'])) echo $_POST['description'];  ?>" class="form-control col-lg-6"  rows="3" >
</textarea>
<br/>
<div>
<button type="submit" class="btn btn-primary col-lg-4">
<span>SUBMIT
</span>
</button>  
</div> 
</div>
<?php echo form_close(); ?>
</div>

控制器页面

public function gallery1()
{                    
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Login_set'); 
$form_data = $this->input->post();
$data1 = array('offer_id'=>$this->input->post('id'),
'hotel_id'=>1,
'offers_name'=>$this->input->post('offered'),
'offers_description'=>$this->input->post('description')
);
print_r($data1);
$this->Login_set->add_offer($data1);    
$page_id =$this->uri->segment(3);
$data['h']=$this->Login_set->select(); 
$this->load->view('App_stay/pages/hotel1_galery.php',$data); 
}

模型页面

<?php
class Login_set extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function add_offer($data1)
{
$this->load->database();
$this->load->helper('url');
if($this->db->insert('offer_hotel1',$data1))
return true;
else
return false;
}
}
?>

【问题讨论】:

    标签: php database html forms codeigniter


    【解决方案1】:

    您不需要在 HTML 输入中保存任何值。只需这样做:

    <div class="container col-lg-4">
    <?php echo validation_errors(); ?>
    <?php echo form_open('Welcome/gallery1'); ?>
    <div class="form-group has-info">
        <input type="hidden" name="id" value="">
        <br>
        <label class="control-label col-lg-6" for="inputSuccess">Offer title</label>
        <input type="text" class="form-control col-lg-6" name="offered" id="offered">
        <label class="control-label col-lg-6" for="inputSuccess">Offer Description</label>
        <textarea id="description" name="description" class="form-control col-lg-6"  rows="3" ></textarea>
        <br/>
        <div>
        <button type="submit" class="btn btn-primary col-lg-4">
        <span>SUBMIT</span>
        </button>
        </div>
    </div>
    <?php echo form_close(); ?>
    

    public function gallery1()
    {
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->model('Login_set');
        $data1 = array(
            'offer_id'          =>$this->input->post('id'),
            'hotel_id'          =>1,
            'offers_name'       =>$this->input->post('offered'),
            'offers_description'=>$this->input->post('description')
        );
        print_r($data1);
        $this->Login_set->add_offer($data1);
        $page_id =$this->uri->segment(3);
        $data['h']=$this->Login_set->select();
        $this->load->view('App_stay/pages/hotel1_galery.php',$data);
    }
    

    这些值将在控制器中转储为$this-&gt;input-&gt;post('description'); $this-&gt;input-&gt;post('offered');

    如果你想查看这个输入中是否已经设置了一个值,你需要从数据库中获取数据而不是value="&lt;?php if(isset($_POST['offered'])) echo $_POST['offered']; ?&gt;"

    【讨论】:

    • 但我没有将值插入数据库
    • 你在做$this->db->insert('offer_hotel1',$data1),不插入是什么意思?那你想做什么
    • 当我提交按钮时代码不起作用然后值也插入到数据库中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 2013-08-30
    • 2016-01-28
    • 1970-01-01
    • 2019-08-02
    相关资源
    最近更新 更多