2017-06-02 74 views
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(); 
    } 
} 
?> 

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

回答

2

用途:

dataType : 'json', 
encode : true 

后:

data: {no_of_post:no_of_post}, 

因为你是从CONTROLER返回JSON编码数据。

+1

谢谢@Praveen P K它适合我。 –

+0

不用客气:-) @AnilKumarSahu –

+0

'encode:true'的目的是什么? – Musa