【问题标题】:How to retrieve value of radio button and insert into database in code igniter?如何检索单选按钮的值并在codeigniter中插入数据库?
【发布时间】:2015-07-24 11:53:35
【问题描述】:

我有一个名为 feedback.php 的表单,其中包含两个问题。我想撤回所选单选按钮的值并在代码点火器中插入数据库。表名是“反馈”,我在其中存储这些值。 表单的html代码在这里

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

在控制器中,我有带有此代码的 feedback.php

 <?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');
}
}
?>

在模型中,我有带有此代码的 feedback_model.php。

<?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);
}
}
?>

【问题讨论】:

  • 你的表单日期如何进入控制器文件???
  • 这就是我想知道的。我是 codeigniter 的新手。
  • 您应该将method="post" 属性添加到您的formtag
  • 我已经添加了 post 方法现在我想知道如何在代码点火器中找到所选单选按钮的值并将其传递到数据库??

标签: javascript php html codeigniter-2


【解决方案1】:

你的表单数据没有提交,因为你没有在表单标签中写action

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

然后您在控制器中获取单选按钮数据

function index()
{
// Setting Values For Table Columns
$data = array(
'complain_id' => $this->input->post('complain_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);

}

在您的表单中没有任何输入类型具有此名称

$this->input->post('Question2')
$this->input->post('Question3')

【讨论】:

  • 提交表单后,我收到一个找不到页面的错误。
  • 我已经从 html 表单中删除了该代码,因为它们的概念都是相同的。
【解决方案2】:

我创造了和你一样的东西

首先,您需要正确设置您的项目 像数据库连接,CI 的自动加载助手

这是我的 feedback.php 控制器

<?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);
        }

        
    }
}

这是我的feedback_model.php,你的并没有真正改变

<?php

class feedback_model extends CI_Model{

    function form_insert($data){
        // Inserting in Table(feedback) of Database(college)
        $this->db->insert('feedback', $data);
    }
}

这是视图,在文件夹views下命名为feedback.php

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

在视图中我添加了一个属性

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

method 表示发往服务器的请求类型 并且 action 是您提交的表单的 uri

所以在这种情况下,表单将提交给控制器反馈,其中 方法是提交

来自提交方法

我检查请求方法是否是post

并获取帖子数据,即

问题1

问题2

并将其传递给 feedback_model

$data = array(
    'response1' => $this->input->post('Qustion1'),
    'response2' => $this->input->post('Question2')
);

// Transfering Data To Model
$this->feedback_model->form_insert($data);

您可以根据需要进行修改。

另外一件重要的事情是 MVC 的命名约定

因为你有一个控制器 feedback_model.php 然后再次为令人困惑的模型创建了一个 feedback_model.php。

希望对你有帮助

【讨论】:

  • @RahulSingh你已经解决了吗,我用的是漂亮的url,使用htaccess和mod_rewrite来删除index.php
猜你喜欢
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 2017-03-28
相关资源
最近更新 更多