gpt4 book ai didi

php - Json_encode数组函数错误

转载 作者:行者123 更新时间:2023-12-01 08:43:36 26 4
gpt4 key购买 nike

我根据该假设制作了一个矩阵,将产品装箱到不同的盒子中:

我使用 PHP 作为服务器端语言,并使用 jQuery Ajax 来传输数据。

前端代码:<input id="no_of_post" name="no_of_post" type="text" class="form-control" onChange="return test()">

<div class="col-sm-3">
<label for="lastname">Weight Box</label><br>
<input id="weight_box" name="weight_box" type="text" class="form-control">
</div>

<div class="col-sm-3">
<label for="lastname">Plate Box</label><br>
<input id="plate_box" name="plate_box" type="text" class="form-control">
</div>

<div class="col-sm-3">
<label for="lastname">Two Pipe Box</label><br>
<input id="two_pipe_box" name="two_pipe_box" type="text" class="form-control">
</div>

<div class="col-sm-3">
<label for="lastname">Four Pipe Box</label><br>
<input id="four_pipe_box" name="four_pipe_box" type="text" class="form-control" onChange="return test()">
</div>

<div class="col-sm-3">
<label for="lastname">No. Of Box</label><br>
<input id="no_of_box" name="no_of_box" type="text" class="form-control">
</div>


<script type="text/javascript">
function test(){

var no_of_post=$('#no_of_post').val();
$.ajax({
type: "POST",
url: '<?php echo base_url()?>Test/test1',
data: {no_of_post:no_of_post},
//data: $('#enqCapture').serialize(),
success: function(resp) {

// toastr.success('Success - Dispatch Detail Prepared');
$("#weight_box").val(resp[1]);
$("#plate_box").val(resp[3]);
$("#two_pipe_box").val(resp[5]);
$("#four_pipe_box").val(resp[7]);
$("#no_of_box").val(resp[9]);

console.log(resp);
},
error: function (jqXHR, exception) {
toastr.error('Error - Something wrong try again.');
}
});

return false;
}
</script>

我的 Controller 代码是:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends MY_Controller{
function __construct() {
parent::__construct();

$this->load->helper('url');
$this->load->model('Enquiry_model');
$this->load->model('Quotation_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('User');
$this->load->model('Main_model');
$this->load->model('Master_model');
$this->is_logged_in();
}
public function index(){
$data['user_data']=$this->is_logged_in();
$this->load->view('admin1/quotation/test',$data);
}
public function test1(){
$no_of_post=$this->input->post('no_of_post');
//$no_of_post."<br>";

$weight_box=0;
$plate_box=0;
$two_pipe_box=0;
$four_pipe_box=0;

if($no_of_post==1){
$weight_box=1;
$two_pipe_box=1;

$total_box=floor($weight_box+$two_pipe_box);

}else{
$weight_box=round($no_of_post/2);
$plate_box=ceil(($no_of_post/5));
$four_pipe_box=$no_of_post/4;

if (strpos($four_pipe_box,'.') !== false) {
$four_pipe_box1=floor($four_pipe_box);
$fraction=$four_pipe_box-$four_pipe_box1;
if($fraction>=.60){
$four_pipe_box=ceil($no_of_post/4);
$total_box=floor($weight_box+$plate_box+$four_pipe_box);

}else{
$four_pipe_box=floor($no_of_post/4);
$two_pipe_box=1;
$total_box=floor($weight_box+$plate_box+$four_pipe_box+$two_pipe_box);
}

}else{
$four_pipe_box=ceil($no_of_post/4);
$total_box=floor($weight_box+$plate_box+$four_pipe_box);
}
}

$arr= array($weight_box,$plate_box,$two_pipe_box,$four_pipe_box,$total_box);

/*$arr[0]=$weight_box;
$arr[1]=$plate_box;
$arr[2]=$two_pipe_box;
$arr[3]=$four_pipe_box;
$arr[4]=$total_box;*/

//$arr[0] = "Mark Reed";
//$arr[1] = "34";
//$arr[2] = "Australia";

//echo json_encode($arr);
echo json_encode(array_values($arr));
//exit();
}
}
?>

但是在文本框中返回错误的结果。

最佳答案

用途:

dataType : 'json',
encode : true

之后:

data: {no_of_post:no_of_post},

因为您正在从 Controller 返回 json 编码数据。

关于php - Json_encode数组函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44327133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com