Anil Kumar Sahu
Anil Kumar Sahu

Reputation: 577

Json_encode array function Error

I have make an matrix for boxing products in to different boxed based on that assumption:

I am using PHP as an server side language and jQuery Ajax for transmitting the data.

Front end code: <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>

And My Controller code is:

<?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();
  }
}
?>

But it is return wrong result in text box.

Upvotes: 1

Views: 73

Answers (1)

Praveen P K
Praveen P K

Reputation: 198

Use:

dataType : 'json',
encode   : true

After:

data: {no_of_post:no_of_post},

Because you are returning json encoded data from the controler.

Upvotes: 2

Related Questions