当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Ajax get column table value to become a variable in javascript

问题描述:

i want to get column table value to become a variable in javascript to complete a formula: 10 * vHasil_m * 32.03* 1000 / 5.

I have a query like this:

select round(avg(cast(t_dtl.hasil_m as decimal(6,4))),4) as hasil_m from t_hdr JOIN t_dtl ON t_hdr.headerid = t_dtl.headerid where id_solvent ='NaOH' AND status='ok' AND concentrate='0.0100' AND date_start <='2016-05-01' AND date_finish >='2016-05-01'

The query result will be like:

| hasil_m |

| ----------- |

| 0.0100 |

I want above column value to become a variable for the formula, vHasil_m.

To run that query, i've created a model:

function getdata_naoh($datetest){

$result = $this->db1->query("select round(avg(cast(t_dtl.hasil_m as decimal(6,4))),4) as hasil_m from t_hdr JOIN t_dtl ON t_hdr.headerid = t_dtl.headerid where id_solvent ='NaOH' AND status='ok' AND concentrate='0.0100' AND date_start <='$datetest' AND date_finish >='$datetest'");

if($result->num_rows()>0){

return $result->result_array();

}else{

return array();

}

}

and a controller to executed it:

function get_NaOH(){

$date_analysis = trim($this->input->post('date_analysis'));

$datetest = trim($this->input->post('date_analysis'));

$datetest = substr($datetest,6,4).'-'.substr($datetest,3,2).'-'.substr($datetest,0,2);

$dtnaoh = $this->M_tambahan->getdata_naoh($datetest);

foreach($dtnaoh as $row){

$data1 .= $row[hasil_m];

}

$data = $data1;

echo $data;

}

Then i create an ajax like this:

<script type="text/javascript">

$(document).ready(function(){

$(document).on('change','.no_picno', function() {

var that = $(this);

var dt_no = that.val();

console.log(that.next());

var col_date_analysis = that.parent().prev().find('.date_analysis');

var date_analysis = col_date_analysis.val();

$.ajax({

type :"post",

url : "<?php echo base_url();?>index.php/additionals/file/C_utils/get_NaOH",

data : { date_analysis: date_analysis, no_picno: dt_no },

success: function(data){

// code here

}

});

});

});

</script>

The ajax already work without error, then i create a javascript to run the formula:

<script type="text/javascript">

function getTotal() {

var obj = document.getElementsByTagName('input');

for(var i=0; i < obj.length; i++) {

if (obj[i].name == "sulfur_vnaoh[]") {

var dt_sulfur_vnaoh = obj[i].value;

};

if (obj[i].name == "sulfur_ws[]") {

var dt_sulfur_ws = obj[i].value

};

if (obj[i].name == "sulfur_hasil[]") {

if ((dt_sulfur_ws != '')&&(dt_sulfur_vnaoh != '')) {

var totalSulfur = 10*vHasil_m*32.03*1000/5;

obj[i].value = totalSulfur;

} else {

obj[i].value = '';

};

};

}

};

</script>

How to declare or initializing the ajax result into variable in javascript to complete that formula ?

Thanks.

网友答案:

in your ajax code you need to create a global variable to store your data:

<script type="text/javascript">
$(document).ready(function(){  
    $(document).on('change','.no_picno', function() {
        var that = $(this);
        var dt_no = that.val();
        console.log(that.next());
        var col_date_analysis =  that.parent().prev().find('.date_analysis');
        var date_analysis = col_date_analysis.val();

        $.ajax({
                  type :"post",
                  url : "<?php echo base_url();?>index.php/additionals/file/C_utils/get_NaOH",
                  data : { date_analysis: date_analysis, no_picno: dt_no },
                  success: function(data){
                      window.data=data;
                  }
        });
    });
});
</script>

and then after your ajax call, fire your getTotal() function, then instead of using vHasil_m in your formula use window.data:

<script type="text/javascript">
    function getTotal() {
        var obj = document.getElementsByTagName('input');  

        for(var i=0; i < obj.length; i++) {
          if (obj[i].name == "sulfur_vnaoh[]") { 
              var dt_sulfur_vnaoh = obj[i].value;
          };
          if (obj[i].name == "sulfur_ws[]") { 
              var dt_sulfur_ws = obj[i].value
          };
          if (obj[i].name == "sulfur_hasil[]") {
            if ((dt_sulfur_ws != '')&&(dt_sulfur_vnaoh != '')) {


              var totalSulfur = 10*window.data*32.03*1000/5;
              obj[i].value = totalSulfur;
            } else {
                obj[i].value = '';
            };
          };
        }
    };
</script>
分享给朋友:
您可能感兴趣的文章:
随机阅读: