php - Codeigniter Form Validation Callback Function -
i'm trying create form validation callback function i'm having little trouble getting head around it.
what trying create contact form join mailing list option. if option join mailing list checked want name , email of person added mailing list database. part works want function check database ensure email address being added unique , bit can't head around.
controller:
public function contact() { $this->load->helper('form'); $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'your name', 'required', array('required'=>"<p class='required'>please provide %s</p><br>")); $this->form_validation->set_rules('email', 'your email address', 'required', array('required'=>"<p class='required'>please provide %s</p><br>")); if($this->form_validation->run() == false) { $this->load->view('templates/headder'); $this->load->view('contact'); $this->load->view('templates/footer'); } else { $this->load->library('email'); $name = $this->input->post('name'); $email = $this->input->post('email'); $phone = $this->input->post('phone'); $message = $this->input->post('message'); $list = $this->input->post('mailing_list'); $email_message = "name: $name<br>email: $email<br>phone: $phone<br>message:<br>$message"; $this->email->initialize(); $this->email->from($email, $name); $this->email->to('myaddress@mydomain.co.uk'); $this->email->subject('new query'); $this->email->message($email_message); $this->email->send(); if($this->email->send()){ $this->load->view('send_error'); } else { if($list == 'no') { $this->load->view('sent'); } else { $this->form_validation->set_rules('email', 'email', 'is_unique[mail_list, email]'); if($this->form_validation->run() == false) { $this->load->model('mailing_listm'); $this->mailing_listm->add_name(); $this->load->view('sent'); } else { $this->load->view('contact'); } } } } }
error message:
a database error occurred error number: 1064 have error in sql syntax; check manual corresponds mysql server version right syntax use near ' email 'myaddress@mydomain.co.uk' limit 1' @ line 3 select * `mail_list`, `email` mail_list, email 'myaddress@mydomain.co.uk' limit 1 filename: libraries/form_validation.php line number: 1134
hopefully able let me know daft thing i've done time.
also, function turning bit of monster, it's complicated thing i've every tried write. there way can split out made of several smaller functions instead of 1 gargantuan one?
thanks,
edit have updated code in line comment below using is_unique receiving error message.
edit model:
public function add_name() { $this->name = $this->input->post('name'); $this->email = $this->input->post('email'); $this->db->insert('mail_list', $this); }
for checking unique field there validation rule in codeigniter.
is_unique[table.field]
Comments
Post a Comment