Codeigniter如何在提交后保持价值

时间:2013-02-22 13:44:04

标签: php forms codeigniter validation

我有一个表单可以正常工作,但是当我提交表单时,字段只会在刷新浏览器后更新。如何在不刷新浏览器的情况下自动保留字段值。

这是我的代码:

控制器

<?php

function input($account_id = '', $platform = '')
{

    if ($account_id > '') {
        $query = $this->db->get_where('db_accounts', array('account_id' => $account_id));
        if (isset($query))
            if ($query->num_rows() > '') {
                $row = $query->row_array();
            }
        //declare variables
        $data['name'] = '';
        $data['account_id'] = '';
        $data['platform'] = '';
        $data['status'] = '';
        $data['location_city'] = '';
        $data['location_country'] = '';
        $data['region'] = '';
        $data['type'] = '';

        //pass field data
        $data['fname']['value'] = $row['name'];
        $data['faccount_id']['value'] = $row['account_id'];
        $data['fplatform']['value'] = $row['platform'];
        $data['fstatus']['value'] = $row['status'];
        $data['flocation_city']['value'] = $row['location_city'];
        $data['flocation_country']['value'] = $row['location_country'];
        $data['fregion']['value'] = $row['region'];
        $data['ftype']['value'] = $row['type'];
    }

    if (isset($account_id)) {
        if ($this->input->post('submit')) {
            if ($this->input->post('account_id')) {
                $this->dashboard_model->entry_update();

                $data['message'] = '- update complete...';
            } else {
                $data['message'] = 'There was an error, update not complete.';
            }
        }
        //echo $name;
        $data['account'] = $this->dashboard_model->get_account($account_id, $platform);
        $this->load->view('manage/input', $data);
    }
}
?>

查看

<?php
$hidden = array(
    'account_id' => $faccount_id['value'],
    'platform' => $fplatform['value']
);
//form for Edit
echo '<form method="post" accept-charset="utf-8">';
echo form_hidden($hidden);
?>
<a href="/platform/<?php echo $fplatform['value'] . '/account/' . $faccount_id['value']; ?>">Return to Account</a>

<?php
//Platform
echo '<br><br><span  font-weight:bold"><font  color="#C40909">' . $fplatform['value'] . '</font>';
if (isset($message)) {
    echo '<font color=#C40909> ' . $message . '</font></span>';
};
//Header Name
echo '<h1 class="header"><span class="title">' . $fname['value'] . '</span></h1><br>';
//Form Name 
echo '<b> Name</b>' . form_input('name', set_value('name', $fname['value']));
//Form Platform 
echo '<b> Platform</b>' . form_input('platform', $fplatform['value']);

//Form Dropdown Status
$status_options = $this->lang->line('status');
echo form_label('<b> Status</b>', 'status');
echo form_dropdown('status', $status_options, $fstatus, 'style=width:313px');

//Form Location City 
echo '<b> Location: City</b>' . form_input('location_city', set_value('location_city', $flocation_city['value']));

//Form Location Country  
echo '<b> Location: Country</b>' . form_input('location_country', set_value('location_country', $flocation_country['value']));

//Form Region 
$region_options = $this->lang->line('region');
echo form_label('<b> Region</b>', 'region');
echo form_dropdown('region', $region_options, $fregion, 'style=width:313px');
$data = array('name' => 'submit', 'value' => 'Update Account', 'style' => 'width:313px');
echo form_submit($data);
echo form_close();
?>

2 个答案:

答案 0 :(得分:4)

使用CodeIgniter表单验证set_value

一定会对你有所帮助。

答案 1 :(得分:0)

在此处添加了form_validation:

if(isset($account_id)){
    $this->form_validation->set_rules('account_id', 'AAccount ID', 'trim|required');
    $this->form_validation->set_rules('name', 'Name', 'trim|required');
    if($this->form_validation->run()){
if($this->input->post('submit')){
//if($this->input->post('account_id')){
 $this->dashboard_model->entry_update();

$data['message'] = '- update complete...';
      }else{
        $data['message'] = 'There was an error, update not complete.';
      }
}
//echo $name;
    $data['account'] = $this->dashboard_model->get_account($account_id,$platform);
    $this->load->view('manage/input',$data);    
}

}

参考此处:以我的形式

   //Form Name 
echo form_error('name'); 
echo '<b> Name</b>'.form_input('name', set_value('name', $fname['value']) );