2

I created a contact form to submit details to a database named 'ccp' and table name is 'inquiries' But after pressing the Submit button, it does not direct to any page and does not submit data to the database. Please help me to fix this error.

Controller File - Contactform.php

<?php

class Contactform extends CI_Controller {

public function __construct(){

        parent::__construct();
        $this->load->helper(array('form','url'));
        $this->load->library(array('session','form_validation'));

        $this->load->database();

}
function index(){
        //set validation rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required|callback_alpha_space_only');
        $this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
        $this->form_validation->set_rules('subject', 'Subject', 'trim|required');
        $this->form_validation->set_rules('message', 'Message', 'trim|required');

        //run validation on post data
        if ($this->form_validation->run() == FALSE)
        {   //validation fails
            $this->load->view('Contact_form_view');
        }
        else
        {
            //insert the contact form data into database
            $data = array(
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
                'subject' => $this->input->post('subject'),
                'message' => $this->input->post('message')
            );

            if ($this->db->insert('inquiries', $data))
            {
                // success
                $this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
                redirect('Contactform/index');
            }
            else
            {
                // error
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error.  Please try again later!!!</div>');
                redirect('Contactform/index');
            }
        }
}
    //custom callback to accept only alphabets and space input
function alpha_space_only($str){
        if (!preg_match("/^[a-zA-Z ]+$/",$str))
        {
            $this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
}
}
?>

View File - Contact_form_view.php

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ciao Code Contact Form</title>
    <link href="<?php echo base_url("css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="col-md-6 col-md-offset-3">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Contact Form</h3>
        </div>
        <div class="panel-body">
            <?php $attributes = array("name" => "Contactform");
            echo form_open("Contactform/index", $attributes);?>
            <div class="form-group">
                <label for="name">Name</label>
                <input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
                <span class="text-danger"><?php echo form_error('name'); ?></span>
            </div>

            <div class="form-group">
                <label for="email">Email ID</label>
                <input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
                <span class="text-danger"><?php echo form_error('email'); ?></span>
            </div>

            <div class="form-group">
                <label for="subject">Subject</label>
                <input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>" />
                <span class="text-danger"><?php echo form_error('subject'); ?></span>
            </div>

            <div class="form-group">
                <label for="message">Message</label>
                <textarea class="form-control" name="message" rows="4" placeholder="Message"><?php echo set_value('message'); ?></textarea>
                <span class="text-danger"><?php echo form_error('message'); ?></span>
            </div>

            <div class="form-group">
                <button name="submit" type="submit" class="btn btn-success">Submit</button>
            </div>
            <?php echo form_close(); ?>
            <?php echo $this->session->flashdata('msg'); ?>
        </div>
    </div>
</div>
</body>
</html>

Apart from this, database.php file is updated as follows

    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'ccp',
    'dbdriver' => 'mysqli',

and routes.php is as follows:

$route['default_controller'] = 'Contactform';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

and the following is the error I'm getting enter image description here

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
dilk
  • 103
  • 3
  • 10

1 Answers1

3

Follow these methods

  1. set Base URL for your project. Check my answer to another question
    • In the image, I can see something like [::1]. Its overcome because of whenever base URL is empty.
  2. Load site without index.php.Check .htaccess mentioned in another question

    • you can add index.php in form open to avoid this. but not recommended.

      echo form_open("index.php/Contactform/index", $attributes);?>
      
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85