如何在 codeigniter3 控制器中获取 POST 数据
How to get POST data in codeigniter3 controller
我正在使用前端 angularjs 和后端 codeignitor(3)。
我想 post 后端控制器中的数据。
但是我无法获取数据。
这是我的 html-
<form>
<fieldset class="form-group">
<h3>Get data of your table</h3>
<label for="exampleInputPassword1">Select coloumn value</label>
<input type="text" class="form-control" ng-model="table.table_column" id="exampleInputPassword1" placeholder="table coloumn values">
<small class="text-muted">Please enter valid data.</small>
</fieldset>
<fieldset class="form-group">
<label for="exampleInputEmail1">Enter Table Name</label>
<input type="text" ng-model="table.table_name" class="form-control" id="exampleInputEmail1" placeholder="Enter Table Name">
<small class="text-muted">Please enter valid table name.</small>
</fieldset>
<fieldset class="form-group">
<label for="exampleInputPassword1">Where</label>
<input type="text" class="form-control" ng-model="table.table_where" id="exampleInputPassword1" placeholder="Where">
<small class="text-muted">Please enter valid value</small>
</fieldset>
<fieldset class="form-group">
<label for="exampleInputPassword1">Value</label>
<input type="text" class="form-control" ng-model="table.table_value" id="exampleInputPassword1" placeholder="Table Value">
<small class="text-muted">Please enter valid value</small>
</fieldset>
<button type="submit" class="btn btn-primary" ng-click="submitForm(table)">Submit</button>
</form>
</div>
这是我的 angularjs 控制器函数-
$scope.submitForm = function(table) {
$http.post("http://localhost/DatabaseApp/welcome/getData/",table)
.success(function(response) {$scope.data = response});
}
这是我的 codeignitor 函数-
function getData()
{
$table_column = $this->input->post('table_column');
$table_name = $this->input->post('table_name');
$table_value = $this->input->post('table_value');
$table_where = $this->input->post('table_where');
print_r($table_name);
}
因此,如果有人可以帮助我,我将不胜感激。非常感谢。
当然你不能用 $_POST 数组获取数据,因为 angular $http 将请求数据作为字符串流发送,所以你应该使用 :
$data = file_get_content("php//input");
$data = json_decode($data);
$table_column = $data->table_column;
$table_name = $data->table_name;
$table_value = $data->table_value;
$table_where = $data->table_where;
祝你好运
1) 您应该在表单标签中添加 method="POST"
和 action="~/nameController/nameAction"
。
2) 为您的输入命名。
<form method="POST" action="~/nameController/nameAction">
<input type="text" name="exampleInputPassword1"/>
<!--You code here-->
</form>
我正在使用前端 angularjs 和后端 codeignitor(3)。 我想 post 后端控制器中的数据。 但是我无法获取数据。
这是我的 html-
<form>
<fieldset class="form-group">
<h3>Get data of your table</h3>
<label for="exampleInputPassword1">Select coloumn value</label>
<input type="text" class="form-control" ng-model="table.table_column" id="exampleInputPassword1" placeholder="table coloumn values">
<small class="text-muted">Please enter valid data.</small>
</fieldset>
<fieldset class="form-group">
<label for="exampleInputEmail1">Enter Table Name</label>
<input type="text" ng-model="table.table_name" class="form-control" id="exampleInputEmail1" placeholder="Enter Table Name">
<small class="text-muted">Please enter valid table name.</small>
</fieldset>
<fieldset class="form-group">
<label for="exampleInputPassword1">Where</label>
<input type="text" class="form-control" ng-model="table.table_where" id="exampleInputPassword1" placeholder="Where">
<small class="text-muted">Please enter valid value</small>
</fieldset>
<fieldset class="form-group">
<label for="exampleInputPassword1">Value</label>
<input type="text" class="form-control" ng-model="table.table_value" id="exampleInputPassword1" placeholder="Table Value">
<small class="text-muted">Please enter valid value</small>
</fieldset>
<button type="submit" class="btn btn-primary" ng-click="submitForm(table)">Submit</button>
</form>
</div>
这是我的 angularjs 控制器函数-
$scope.submitForm = function(table) {
$http.post("http://localhost/DatabaseApp/welcome/getData/",table)
.success(function(response) {$scope.data = response});
}
这是我的 codeignitor 函数-
function getData()
{
$table_column = $this->input->post('table_column');
$table_name = $this->input->post('table_name');
$table_value = $this->input->post('table_value');
$table_where = $this->input->post('table_where');
print_r($table_name);
}
因此,如果有人可以帮助我,我将不胜感激。非常感谢。
当然你不能用 $_POST 数组获取数据,因为 angular $http 将请求数据作为字符串流发送,所以你应该使用 :
$data = file_get_content("php//input");
$data = json_decode($data);
$table_column = $data->table_column;
$table_name = $data->table_name;
$table_value = $data->table_value;
$table_where = $data->table_where;
祝你好运
1) 您应该在表单标签中添加 method="POST"
和 action="~/nameController/nameAction"
。
2) 为您的输入命名。
<form method="POST" action="~/nameController/nameAction">
<input type="text" name="exampleInputPassword1"/>
<!--You code here-->
</form>