1
我必須做出拳擊的產品矩陣中基於上述假設的不同的盒裝:Json_encode陣列功能錯誤
我使用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>
而且我的控制器代碼:
<?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();
}
}
?>
但它在文本框中返回錯誤的結果。
謝謝@Praveen P K它適合我。 –
不用客氣:-) @AnilKumarSahu –
'encode:true'的目的是什麼? – Musa