Search code examples
javascriptphphtmlcodeigniter-2

How to retrieve value of radio button and insert into database in code igniter?


I have a form named feedback.php with two questions.I want to retreat the value of selected radio button and insert into database in code igniter. The table name is 'feedback' where i am storing this values. html code for form is here

<form role="form" method="post" name ="your_form" action="<?php echo base_url();?>/index.php/feedback_model/index" >
           <span class="badge">1</span></a> Is your complain solved ?  
          <div class="form-group">
            <div class="radio">
          <label><input type="radio" name="Question1"          
value="yes">Yes</label>
            </div>
            <div class="radio">
              <label><input type="radio" name="Question1" value="no">NO</label>
            </div>
        </div>  


         <span class="badge">2</span></a> How easy was it to complain to us?  
          <div class="form-group">
            <div class="radio">
              <label><input type="radio" name="Question2" value='excellent'>Excellent</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="Question2" value="good">Good </label>
            </div>
            <div class="radio">
                <label><input type="radio" name="Question2" value="bad">Bad</label>
            </div>
            <div class="radio">
              <label><input type="radio" name="Question2" value="verybad">Very Bad</label>
            </div>
        </div>  
    </form>

In controller i have feedback.php with this code

 <?php
class feedback_model extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('feedback_model');
}
function index()
{
// Including Validation Library



// Setting Values For Tabel Columns
$data = array(
'complain_id' => $this->input->post('complain_id'),
'email' => $this->input->post('email'),
'response1' => $this->input->post('Qustion1'),
 'response1' => $this->input->post('Question2'),
 'response1' => $this->input->post('Question3'),
 'response1' => $this->input->post('Question4')

);
// Transfering Data To Model
$this->insert_model->form_insert($data);
// Loading View
$this->load->view('feedback');
}
}
?>

And in model i have feedback_model.php with this code.

<?php
class feedback_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
// Inserting in Table(feedback) of Database(college)
$this->db->insert('feedback', $data);
}
}
?>

Solution

  • I created same something like yours

    First is you need to setup your project properly Like Database Connection, Autoloaded Helpers of CI

    Here is my feedback.php controller

    <?php
    
    class feedback extends CI_Controller {
    
        function __construct() {
    
            parent::__construct();
    
            $this->load->model('feedback_model');
        }
    
        function index() {
            
            // Loading View
            $this->load->view('feedback');
        }
    
        function submit() {
    
            // check for method
            if ($this->input->post('REQUEST_METHOD') == 'POST') {
    
                // Including Validation Library
                // Setting Values For Tabel Columns
                $data = array(
                    'response1' => $this->input->post('Qustion1'),
                    'response2' => $this->input->post('Question2')
                );
    
                // Transfering Data To Model
                $this->feedback_model->form_insert($data);
            }
    
            
        }
    }
    

    Here is my feedback_model.php nothings really change with yours

    <?php
    
    class feedback_model extends CI_Model{
    
        function form_insert($data){
            // Inserting in Table(feedback) of Database(college)
            $this->db->insert('feedback', $data);
        }
    }
    

    Here is the view, named as feedback.php under the folder views

    <html>
      <head>
        <title></title>
      </head>
      <body>
        <form role="form" method="POST" action="feedback/submit">
          <span class="badge">1</span></a> Is your complain solved ?
          <div class="form-group">
            <div class="radio">
              <label><input type="radio" name="Question1"
              value="yes">Yes</label>
            </div>
            <div class="radio">
              <label><input type="radio" name="Question1" value="no">NO</label>
            </div>
          </div>
          <span class="badge">2</span></a> How easy was it to complain to us?
          <div class="form-group">
            <div class="radio">
              <label><input type="radio" name="Question2" value='excellent'>Excellent</label>
            </div>
            <div class="radio">
              <label><input type="radio" name="Question2" value="good">Good </label>
            </div>
            <div class="radio">
              <label><input type="radio" name="Question2" value="bad">Bad</label>
            </div>
            <div class="radio">
              <label><input type="radio" name="Question2" value="verybad">Very Bad</label>
            </div>
          </div>
          <div class="form-group">
            <input type="submit" value="submit" name="submit">
          </div>
        </form>
      </body>
    </html>
    

    In the view i added a attribute

    method="POST" and action="feedback/submit"

    method means the request type going to the server and action is uri your form being submitted

    so in this case the form will be submitted to controller feedback where the method is submit

    from the submit method

    i check if the request method is post

    and get the post data namely the

    Question1

    Question2

    and pass it to the feedback_model

    $data = array(
        'response1' => $this->input->post('Qustion1'),
        'response2' => $this->input->post('Question2')
    );
    
    // Transfering Data To Model
    $this->feedback_model->form_insert($data);
    

    you can modified it according to your need.

    and one more thing that is important is the naming conventions for MVC

    since you have a controller feedback_model.php then created again a feedback_model.php for model which is confusing.

    Hope it help