最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to retrieve value of radio button and insert into database in code igniter? - Stack Overflow

programmeradmin1浏览0评论

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 plain 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 plain 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(
'plain_id' => $this->input->post('plain_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);
}
}
?>

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 plain 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 plain 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(
'plain_id' => $this->input->post('plain_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);
}
}
?>
Share Improve this question edited May 13, 2015 at 9:38 Rahul Singh asked May 13, 2015 at 9:24 Rahul SinghRahul Singh 3402 gold badges5 silver badges16 bronze badges 4
  • How your form date enter into controller file??? – Saty Commented May 13, 2015 at 9:27
  • thats the thning i want to know . i am new in codeigniter. – Rahul Singh Commented May 13, 2015 at 9:31
  • You should add the method="post" attribute to your formtag – jmgross Commented May 13, 2015 at 9:31
  • I have added post method now i want to know how to find the value of selected radio button in code igniter and pass it to database?? – Rahul Singh Commented May 13, 2015 at 9:36
Add a ment  | 

2 Answers 2

Reset to default 2

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 plain 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 plain 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

You form data is not submit because you not write action in form tag

 <form role="form" method="post" name ="your_form" action="<?php echo base_url(); ?>/index.php/feedback_model/index" >

And you get your radio button data in your controller

function index()
{
// Setting Values For Table Columns
$data = array(
'plain_id' => $this->input->post('plain_id'),
'email' => $this->input->post('email'),
'response1' => $this->input->post('Qustion2'),// you radio button data
'response2' => $this->input->post('Qustion1'),
);
// Transfering Data To Model
$this->insert_model->form_insert($data);
// Loading View
$this->load->view('feedback',$data);

}

No any input type having name this in your form

$this->input->post('Question2')
$this->input->post('Question3')
发布评论

评论列表(0)

  1. 暂无评论