如何检索单选按钮的值并在 codeigniter 中插入数据库?
How to retrieve value of radio button and insert into database in code igniter?
我有一个名为 feedback.php 的表单,其中有两个 questions.I 想要撤消所选单选按钮的值并在代码点火器中插入数据库。 table 名称是 'feedback' 我存储这些值的地方。
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);
}
}
?>
你的表单数据没有提交,因为你没有在表单标签中写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')
我创造了和你一样的东西
首先你需要正确设置你的项目
与数据库连接一样,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" and action="feedback/submit"
method表示发送到服务器的请求类型
动作是你提交的表单
所以在这种情况下,表单将提交给控制器反馈,其中
方法是提交
来自提交方法
我检查请求方法是否是post
并得到post数据即
Question1
Question2
并将其传递给 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 令人困惑。
希望对您有所帮助
我有一个名为 feedback.php 的表单,其中有两个 questions.I 想要撤消所选单选按钮的值并在代码点火器中插入数据库。 table 名称是 'feedback' 我存储这些值的地方。 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);
}
}
?>
你的表单数据没有提交,因为你没有在表单标签中写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')
我创造了和你一样的东西
首先你需要正确设置你的项目 与数据库连接一样,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" and action="feedback/submit"
method表示发送到服务器的请求类型 动作是你提交的表单
所以在这种情况下,表单将提交给控制器反馈,其中 方法是提交
来自提交方法
我检查请求方法是否是post
并得到post数据即
Question1
Question2
并将其传递给 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 令人困惑。
希望对您有所帮助