i using codeigniter 2.2.2. in controller captured input pass view in case validation failed, user not forced fill fields start, set following variable through $data array
$data['fullname'] = $this->input->post('fullname');
but when user access form @ first gives error follows.
a php error encountered severity: notice message: undefined variable: fullname
if use $this->input->post('fullname');
directly inside view no error triggered, want pass controller.
the second issue when set form validation rule input $this->form_validation->set_rules('fullname', 'full name', 'required|alpha');
validation rule alpha
doesn't allow spaces in case require, because form field full name, there must space e.g., "albert einstein".
i can solve problem setting 2 different form input fields "first name" , "last name", don't that, because think framework should make life easier not harder.
here example gist showing example code, trigger errors said. example gist
try this.
public function contact() { $this->load->library("session"); $data['message'] = $this->session->flashdata("message"); $data['fullname'] = $this->session->flashdata("fullname"); $this->load->view('site_header'); $this->load->view('site_nav'); $this->load->view('content_contact', $data); $this->load->view('site_footer'); } public function send_email() { $this->load->library('form_validation'); $this->form_validation->set_rules('fullname', 'full name', 'required|callback_fullname_check'); $this->form_validation->set_rules('email', 'email address', 'required|valid_email'); $this->form_validation->set_rules('message', 'message', 'required'); if ($this->form_validation->run() == false) { $this->session->set_flashdata("message", validation_errors()); $this->session->set_flashdata("fullname", $this->input->post('fullname')); } else { $this->session->set_flashdata("message", "the email has been sent!"); } redirect("site/contact"); } public function fullname_check($str) { if (! preg_match("/^([a-z0-9 ])+$/i", $str)) { $this->form_validation->set_message('fullname_check', 'the %s field can alpha numeric'); return false; } else { return true; } }
please avoid loading view repeatedly dry (don't repeat yourself)
edit
try use custom validation rule achieve alpha numeric string space.
hope useful you.