duanshan5259 2016-05-24 02:04
浏览 27

Ajax返回值作为PHP变量

I'm new in ajax, and i want to create ajax to return value as a PHP variable, i'm stuck trying to get the value from an ajax. I have the following form (view):

<td>
    <input type="text" name="date_analysis[]" id="date_analysis" class="date_analysis input-group date" data-date="" data-date-format="dd-mm-yyyy" size="12" value="<?php $a=set_value('date_analysis[0]'); echo $a; ?>"/></td>
<td>
    <input type="text" name="sulfur_ws[]" class="sulfur_ws" id="sulfur_ws" size="10" onkeyup="getValues()" value="<?php $a=set_value('sulfur_ws[0]'); echo $a; ?>"/></td>
<td>
    <input type="text" name="sulfur_vnaoh[]" class="sulfur_vnaoh" id="sulfur_vnaoh" size="10" onkeyup="getValues()" value="<?php $a=set_value('sulfur_vnaoh[0]'); echo $a; ?>"/></td>    
<td>
    <input type="text" name="sulfur_total[]" class="sulfur_total" id="sulfur_total" size="10" onkeyup="getValues()" value="<?php $a=set_value('sulfur_total[0]'); echo $a; ?>"/></td>

Display:

enter image description here

Total field should be fillin automatically by formula: sulfur_vnaoh * aVariable * 32.03* 1000 / sulfur_ws

aVariable is a value from ajax (the value is different for each date analys). Here is my code :

Model Function:

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

Controller Function:

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;
}

and View javascript:

<script type="text/javascript">
    function getValues() {
        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 = ((eval(dt_sulfur_vnaoh))*aVariable*32.03*1000/(eval(dt_sulfur_ws))).toFixed(2);
              obj[i].value = totalSulfur;
            } else {
                obj[i].value = '';
            };
          };
        }
    };
</script>

// ajax script

<script type="text/javascript">
     $(document).ready(function(){         

        $(document).on('change', function() {
            var that = $(this);
            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 },
                      success: function(data){
                          // code here
                      }
                  });
          });
        });

</script>

If i run the model query, it will give result value like below image:

enter image description here

I want that value to become a variable aVariable to complete the formula.

Any help would be appreciated greatly, thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题