0

I think my algorithm is wrong and I don't know why. Could you please tell my mistake ? Here is the problem : Everytime I refresh my page, it always sends '0' to each field, even I have not filled the form yet.

The controller :

function index() {
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->helper('html');
    $data = array (
        'nama' =>$this->input->post('nama'),
        'email' =>$this->input->post('email'),
        'telepon' =>$this->input->post('telepon'),
        'line' =>$this->input->post('line')
        );
        $this->load->model('site_model');
        $this->site_model->add_record($data);
        $this->load->view('index');
}

The Model :

class Site_model extends CI_Model {
    function add_record($data) {
        $this->load->database();
        $this->db->insert('tabel_data', $data);
        return;
    }
}

Some lines from index.php (view) :

<tr>
     <td><p><label>Nama</label></p></td>
     <td>: <input type="text" name="nama" id="nama" title="Tolong isi nama" autofocus required></td>

Help me, please !! My deadline is so close :D

4

1 回答 1

0

因为您不检查表单验证是否正在运行。因此,当您输入索引控制器时,它的行为就像来自表单帖子一样,因此所有值都分配为零。在初始化数据数组之前,您应该检查是否有表单出现。在您的控制器中,您应该检查:

 $this->load->library('form_validation');   
     if ($this->form_validation->run() == TRUE){
              $data = array (
              'nama' =>$this->input->post('nama'),
              'email' =>$this->input->post('email'),
              'telepon' =>$this->input->post('telepon'),
               'line' =>$this->input->post('line')
               );
              $this->load->model('site_model');
              $this->site_model->add_record($data);
              $this->load->view('index');
    }
    else
    {
        $this->load->view('index');
    } 
于 2013-11-14T20:59:12.013 回答