我用代码制作了一个控制器:
function register()
{
$this->load->view('backend/register');
}
我有一个表格(register.php),工作得很好。我需要在控制器中加载此代码:
$names = $this->input->post('name');
$birthday = $this->input->post('birthday');
$phones = $this->input->post('phone');
$emails = $this->input->post('email');
$passwords = $this->input->post('password');
$rolls = '0';
$data['name'] = $names;
$data['email'] = $emails;
$data['password'] = sha1($passwords);
$data['phone'] = $phones;
$data['birthday'] = $birthday;
//validate here, if the row(name, email, password) is empty or not
if($data['name'] == '' || $data['email'] == '' || $data['password'] == '')
continue;
$this->db->insert('student' , $data);
$student_id = $this->db->insert_id();
$data2['enroll_code'] = substr(md5(rand(0, 1000000)), 0, 7);
$data2['student_id'] = $student_id;
$data2['class_id'] = $this->input->post('class_id');
if($this->input->post('section_id') != '') {
$data2['section_id'] = $this->input->post('section_id');
}
$data2['roll'] = $rolls[$i];
$data2['date_added'] = strtotime(date("Y-m-d H:i:s"));
$data2['year'] = $this->db->get_where('settings' , array(
'type' => 'running_year'
))->row()->description;
$this->db->insert('enroll' , $data2);
echo 'alert("Contul a fost creat cu succes!")';
redirect(base_url() . 'index.php?login/' , 'refresh');
在控制器的当前表单中,页面加载非常好。当我添加上面的代码时,不再加载页面。我试图从控制器中删除代码行($ this-> load-> view(' backend / register');)+添加了我想要但没有结果的代码。 如何将上述代码组合起来?
答案 0 :(得分:0)
如果您正在使用单个控制器功能来显示寄存器页面并使用相同的控制器功能来处理数据并将数据插入数据库,请使用以下命令:
function register()
{
if (isset($_POST['submit'])) {
$names = $this->input->post('name');
$birthday = $this->input->post('birthday');
$phones = $this->input->post('phone');
$emails = $this->input->post('email');
$passwords = $this->input->post('password');
$rolls = '0';
$data['name'] = $names;
$data['email'] = $emails;
$data['password'] = sha1($passwords);
$data['phone'] = $phones;
$data['birthday'] = $birthday;
//validate here, if the row(name, email, password) is empty or not
if ($data['name'] == '' || $data['email'] == '' || $data['password'] == '')
continue;
$this->db->insert('student', $data);
$student_id = $this->db->insert_id();
$data2['enroll_code'] = substr(md5(rand(0, 1000000)), 0, 7);
$data2['student_id'] = $student_id;
$data2['class_id'] = $this->input->post('class_id');
if ($this->input->post('section_id') != '') {
$data2['section_id'] = $this->input->post('section_id');
}
$data2['roll'] = $rolls[$i];
$data2['date_added'] = strtotime(date("Y-m-d H:i:s"));
$data2['year'] = $this->db->get_where('settings', array(
'type' => 'running_year'
))->row()->description;
$this->db->insert('enroll', $data2);
echo 'alert("Contul a fost creat cu succes!")';
redirect(base_url() . 'index.php?login/', 'refresh');
} else {
$this->load->view('backend/register');
}
}
注意:您必须在提交按钮中设置name="submit"
,因为if (isset($_POST['submit']))
用于检查$_POST['submit']
是否具有值
我所做的就是
if (isset($_POST['submit'])) {
//do form processing and insert data
}else{
//display register page
$this->load->view('backend/register');
}