我正在使用CodeIgniter,我正在尝试使用AJAX将表单数据发送到控制器。 问题是,当我尝试使用$ this-> input-> get_post(“sku_qty”)来获取控制器中的数据时,它的内容为NULL。 ajax也出错了。 这是代码:
container_view_2.php
<?php
/*
* 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>
<link href="<?php echo base_url('assets/css/bootstrap.min.css'); ?>" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<div class="col-sm-12 text-primary">
<center>
<h2>Container calculator</h2>
</center>
</div>
</div>
<div class="container">
<form method="post" action="">
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12 col-md-offset-2 col-lg-offset-2 col-sm-offset-2">
<div class="col-sm-12 form-group">
<label>Partner Name</label>
<select id="partners" name="partnername" class="form-control selectpicker">
<option value="Amazon">Amazon.com</option>
<option value="Homedepot">Homedepot.com</option>
<option value="Build">Build.com</option>
</select>
</div>
</div>
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12 col-md-offset-2 col-lg-offset-2 col-sm-offset-2">
<div class="col-sm-6 col-lg-6 col-md-6 col-xs-12 form-group">
<label>SKU</label>
<select id="sku" name="sku" class="form-control selectpicker">
<option value="Build">Build</option>
<option value="Homedepot">Homedepot</option>
<option value="Amazon">Amazon</option>
</select>
</div>
<div class="col-sm-6 col-lg-6 col-md-6 col-xs-12 form-group">
<label>Qty</label>
<input type="number" name="sku_qty" id="sku_qty" placeholder="Enter the qty" class="form-control">
</div>
<div class="col-sm-6 col-lg-6 col-md-6 col-xs-12 form-group">
<label>Start Port Name</label>
<input type="text" name="spname" id="spname" placeholder="start port name" class="form-control">
</div>
<div class="col-sm-6 col-lg-6 col-md-6 col-xs-12 form-group">
<label>End Port Name</label>
<input type="text" name="epname" id="epname" placeholder="end port name" class="form-control">
</div>
<div class="col-sm-6 col-lg-6 col-md-6 col-md-offset-7 col-lg-offset-7 col-sm-offset-7 form-group">
<div class="col-sm-3 col-md-3 col-lg-3 col-md-offset-1 col-lg-offset-1 col-sm-offset-1"><button type="submit" onclick="calculateContainerdata()" class="btn btn-primary">CALCULATE</button></div>
<div class="col-sm-3 col-md-3 col-lg-3 col-md-offset-2 col-lg-offset-2 col-sm-offset-2"></div>
</div>
</div>
</form>
</div>
<div class="container" style="margin-top: 1em">
<div class="col-sm-6">
<div class="panel panel-info">
<div class="panel-heading">Nested Box Container</div>
<div class="panel-body">
<?php
//echo $partners;
?>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="panel panel-info">
<div class="panel-heading">Boxed Container Info</div>
<div class="panel-body">
<table class="table table-responsive text-center">
<thead>
<tr>
<th scope="col">Container Size</th>
<th scope="col">SKU Qty</th>
<th scope="col">Cost</th>
<th scope="col">container qty</th>
</tr>
</thead>
<tbody>
<tr>
<td>40"</td>
<td>500</td>
<td>1500</td>
<td>1</td>
</tr>
<tr>
<td>20"</td>
<td>500</td>
<td>1600</td>
<td>2</td>
</tr>
<tr>
<td>18"</td>
<td>500</td>
<td>1800</td>
<td>3</td>
</tr>
<tr>
<td>10"</td>
<td>500</td>
<td>1900</td>
<td>5</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="<?php echo base_url('assets/js/bootstrap.min.js'); ?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/js/jquery-1.10.2.min.js'); ?>"></script>
<script>
function calculateContainerdata() {
var partners = $('#partners :selected').val();
var sku = $('#sku :selected').val();
var sku_qty = $('#sku_qty').val();
var spname = $('#spname').val();
var epname = $('#epname').val();
alert(partners);
var url = "<?php echo site_url('container/calcajax'); ?>";
alert(url);
$.ajax({
url: url,
data: {
partners: partners,
sku: sku,
sku_qty: sku_qty,
spname: spname,
epname: epname,
url: url
},
dataType: "json",
type: "POST",
success: function(rdata) {
alert(rdata);
//window.location.href = '<?php //echo site_url("index"); ?>';
},
error: function(xhr, status, error) {
alert("error" + xhr.responseText);
}
});
}
</script>
</body>
</html>
我的控制器是: Container.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
* container calculator view
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
class Container extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
public function index() {
$this->load->view('container_view_2');
// $this->load->view('container_view');
//echo 'pramod';
}
public function calcajax() {
$partners = $this->input->post('sku_qty');
echo json_encode($partners);
exit();
}
}
答案 0 :(得分:0)
回复的形式错误。试试这个
public function calcajax()
{
$partners = $this->input->post('sku_qty');
if(isset($partners)) //not null
{
$out = ['sku_qty' => $partners];
}
else
{
$out = ['sku_qty' => 0];
}
echo json_encode($out);
//exit(); Just let CodeIgniter do its thing
}
除了使用正确的数据结构进行响应外,它还确保返回的值很有用。
如果您仍然收到错误回复,则最有可能因为ajax网址错误。使用浏览器的开发工具查看实际发布的数据和位置。
答案 1 :(得分:-1)
尝试从ajax中删除datatype
标记
$.ajax({
url: url,
type: "POST",
data: {
partners: partners,
sku: sku,
sku_qty: sku_qty,
spname: spname,
epname: epname,
url: url
},
success: function(rdata) {
alert(rdata);
},
error: function(xhr, status, error) {
alert("error" + xhr.responseText);
}
});
并从控制器功能中删除json_encode()