CodeIgniter Form始终发送“0”数据 [英] CodeIgniter Form always sends '0' data

查看:93
本文介绍了CodeIgniter Form始终发送“0”数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的算法是错误的,我不知道为什么。你能告诉我的错误吗?
这里是问题:每次刷新我的页面,它总是发送'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.

控制器:

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');
}

模型:

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

index.php中的一些行(视图) / p>

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>

我的截止日期很接近:D

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

推荐答案

因为你不检查表单验证是否正在运行。
因此,当您输入索引控制器时,它的行为就像是来自一个表单发布,所有的值都被赋值为零。
在初始化数据数组之前,你应该检查是否有表单来了。
在你的控制器你应该检查:

because you do not check whether the form validation is running or not. So when you enter the index controller it behaves like it is coming from a form post so all the values are assigned to zero. You should check whether there is a form coming or not before initializing your data array. In your controller you should check :

 $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');
    } 

这篇关于CodeIgniter Form始终发送“0”数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆