Hi All
Please help me before i pull my hair out and burn it.
I’m learning codeigniter and trying to create an ajax form that submits to the database. I’m new to OOP & MVC and starting to get my head round it except in trying to create the form i’m getting an error of “Unable to load the requested file: contact/index” but have no idea why as the ->view points straight to it. I have header and footer working ok if i take the main page view out of the equation. Please can one of you kind folk look over my controller & model to see if there is something i’m missing? I feel there is something super obvious i’m missing but i’ve been staring at it too long to see.
The index file is located as follows: views/contact/index/php.
Also i have set a route to the file as follows:-
$route['contact'] = 'contact/index';
Controller:-
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Contact extends CI_Controller
{
// construct
public function __construct()
{
parent::__construct();
$this->load->model('Contactmodel', 'contactmodel');
}
// index contactmodel
public function index()
{
$data = array();
$data['metaDescription'] = 'Contact Form Meta Desc';
$data['metaKeywords'] = 'Contact Form Keywords';
$data['title'] = "Contact Us";
$this->load->view('templates/header');
$this->load->view('contact/index', $data);
$this->load->view('templates/footer');
}
// submit contact form
public function submitEnquiry()
{
$json = array();
$name = $this->input->post('name');
$email = $this->input->post('email');
$contactNo = $this->input->post('contact_no');
$note = $this->input->post('note');
$enquiry = $this->input->post('enquiry');
// name validation
if(empty(trim($name))){
$json['error']['name'] = 'Please enter full name';
}
// email validation
if(empty(trim($email))){
$json['error']['email'] = 'Please enter email address';
}
// check email validation
if ($this->contactmodel->validateEmail($email) == FALSE) {
$json['error']['email'] = 'Please enter valid email address';
}
// check conatct no validation
if($this->contactmodel->validateMobile($contactNo) == FALSE) {
$json['error']['contactno'] = 'Please enter valid contact no. format: 9000000001';
}
// note validation
if(empty($note)){
$json['error']['note'] = 'Please enter your enquiry';
}
if(empty($json['error'])){
$this->contactmodel->setName(trim($name));
$this->contactmodel->setEmail($email);
$this->contactmodel->setContactNo($contactNo);
$this->contactmodel->setNote($note);
$this->contactmodel->setEnquiry($enquiry);
try {
$last_id = $this->contactmodel->create();
} catch (Exception $e) {
var_dump($e->getMessage());
}
if (!empty($last_id) && $last_id > 0) {
$json['status'] = 'success';
}
}
echo json_encode($json);
}
}
Model:-
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Contactmodel extends CI_Model {
private $_name;
private $_email;
private $_contactNo;
private $_note;
private $_enquiry;
public function setName($name)
{
$this->_name = $name;
}
public function setEmail($email)
{
$this->_email = $email;
}
public function setContactNo($contactNo)
{
$this->_contactNo = $contactNo;
}
public function setNote($note)
{
$this->_note = $note;
}
public function setEnquiry($enquiry)
{
$this->_enquiry = $enquiry;
}
// save value in database
public function create()
{
$data = array(
'name' => $this->_name,
'email' => $this->_email,
'contactno' => $this->_contactNo,
'note' => $this->_note,
'enquiry' => $this->_enquiry,
);
$this->db->insert('contactenq', $data);
return $this->db->insert_id();
}
// email validation
public function validateEmail($email)
{
return preg_match('/^[^\@]+@.*.[a-z]{2,15}$/i', $email)?TRUE:FALSE;
}
/* mobile validation
public function validateMobile($mobile)
{
return preg_match('/^[0-9]{10}+$/', $mobile)?TRUE:FALSE;
}*/
}
Also separately, is there an easier way to debug codeigniter. I used to use the PHP error log when i was doing procedural but i cant find the same function with CI.
Thank you in advance.