我是ajax的新手,我想创建一个ajax来返回值作为PHP变量,我试图从ajax获取值。我有以下表格(视图):
<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>
显示:
总计字段应按公式自动填写: sulfur_vnaoh * aVariable * 32.03 * 1000 / sulfur_ws
aVariable 是ajax的值(每个日期分析的值不同)。这是我的代码:
模型功能:
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();
}
}
控制器功能:
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;
}
并查看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 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>
如果我运行模型查询,它将给出如下图像的结果值:
我希望该值成为变量 aVariable 以完成公式。
非常感谢任何帮助,谢谢。