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