我是Codeigniter的新手。我无法将数据插入到我的表中。我也使用过bootstrap文件。
这些是View - groups.php
中的行<input type="text" name="group" id="group" placeholder="Group">
<button type="button" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button>
模型 - groups_model.php
function insert_groups($dbdata) {
$this->db->insert('groups', $dbdata);
}
Controller - groups_controller.php
public function group()
{
$this->load->view('groups');
$this->load->model('groups_model');
if(isset($_POST['addgroup']))
{
$userID= '2';
$groupname= $this->input->post('group');
//$this->load->model('groups_model');
$data=array(
'userID'=>$userID,
'groupname'=>$groupname
);
$this->groups_model->insert_groups($data);
//var_dump($this->db->last_query());
}
}
}
我的完整HTML表单粘贴在下面。那里有很多评论。
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Content</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="<?php echo base_url(); ?>stylesheets/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="<?php echo base_url(); ?>stylesheets/bootstrap.css" rel="stylesheet" type="text/css">
<link href="<?php echo base_url(); ?>stylesheets/bootstrap-theme.min.css" rel="stylesheet" type="text/css">
<link href="<?php echo base_url(); ?>stylesheets/bootstrap-theme.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<SCRIPT language="javascript">
// var person;
// var element;
//
// function add() {
// var e1 = document.getElementById('group').value;
// var e2 = document.getElementById('select');
// var o = document.createElement('option');
// o.value = "group_body.html";
// o.text = e1;
// e2.options.add(o);
//
// }
// function goto() {
// var index = document.getElementById('select').selectedIndex;
// if (index.value != "select") {
// location = index.value;
// }
// }
// function add()
// {
// //Create an input type dynamically.
// element = document.createElement("button");
// element.className='btn btn-default';
// var person = document.getElementById('group').value;
// var t = document.createTextNode(person);
// element.appendChild(t);
// element.id=person;
// //Assign different attributes to the element.
//// element.type = type;
//// element.value = type; // Really? You want the default value to be the type string?
//// element.name = type; // And the name too?
//
// element.onclick = function() { // Note this is a function
// //alert(element.id);
//// $(document).ready(function(){
//// $("#element").click(function(){
// $('body').load('group_body.html');
//// });
//// });
// };
//
// var foo = document.getElementById("fooBar");
// //Append the element in page (in span).
// foo.appendChild(element);
//// var d = document.getElementById('fooBar');
//// d.appendChild(i);
// }
//
// function copy()
// {
// var n1 = document.getElementById('addKeyword');
// var n2 = document.getElementById('getKeyword');
// n2.innerText = n1.value;
// }
</SCRIPT>
</head>
<body>
<form method="post" name="form1">
<div class="btn-group" id="fooBar">
<!-- <button type="button" class="btn btn-default">Marketing</button>
<button type="button" class="btn btn-default">Internet</button>
<button type="button" class="btn btn-default">Politics</button>-->
select a group:
<select class="form-control" id="select" onchange="javascript:location.href = this.value;">
<option selected value="select">select</option>
<!-- <option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>-->
</select>
<button type="button" class="btn btn-default" id="btnAdd" data-toggle="modal" data-target="#myModal">Add Group</button>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Group Name</h4>
</div>
<div class="modal-body">
<h5>Please enter Group Name:</h5>
<input type="text" name="group" id="group" placeholder="Group">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="boxed">
<h4>Group:<span class="label label-default" id="span"><script>document.getElementById('group').innerText = document.getElementById('span').innerText</script></span>
<button type="submit" class="btn btn-default">Edit Name</button>
<button type="submit" class="btn btn-default">Disable</button>
<button type="submit" class="btn btn-default">Delete Group</button></h4>
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<input type="text" class="form-control" placeholder="Enter your keyword" id="addKeyword">
<span class="input-group-btn">
<button class="btn btn-default" type="button" onclick="copy()">Add</button>
</span>
</div><!-- /input-group -->
</div><!-- /.col-lg-6 -->
</div>
<h4>Keywords:</h4>
<!-- <div class="keyword" id="getKeyword"></div>-->
</div>
</form>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="<?php echo base_url(); ?>javascripts/bootstrap.min.js"></script>
<script src="<?php echo base_url(); ?>javascripts/bootstrap.js"></script>
</body>
</html>
答案 0 :(得分:2)
尝试
public function group()
{
$this->load->model('groups_model');
if($this->input->post('addgroup'))
{
$userID= '2';
$groupname= $this->input->post('group');
$data=array(
'userID'=>$userID,
'groupname'=>$groupname
);
$this->groups_model->insert_groups($data);
}
$this->load->view('groups');
}
}
并在表单中添加操作:
变化
<form method="post" name="form1">
要
<?= form_open('yourcontroller/action')?>
//anything HTML inside your form
<?= form_close()?>
您必须加载表单助手才能执行此操作。在autoload.php中
$autoload['helper'] = array('form');
希望它有所帮助!
答案 1 :(得分:0)
为什么要在提交时使用按钮。
更改此
<button type="button" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button>
到
<button type="submit" name="addgroup" class="btn btn-primary" id="add" data-dismiss="modal">Add</button>
您必须提交数据,以便在帖子中获取
答案 2 :(得分:0)
基本上,您的表单标记中没有action属性。
$this->load->helper('form');
echo form_open("yourcontroller/action");
您可以使用以下表达式:
Background="{StaticResource MyBlue}"
或使用Codeigniter表单助手
Style="{StaticResource TextBoxOutput}"
希望它对你有所帮助。