如何在JS函数上插入数据库值

时间:2018-09-17 11:03:28

标签: javascript php html

我想将PHP查询数据库中的值插入.js echart页面中,如何传输这些数据,以便可以绘制图形?

我要计算等待的请求,然后传输到负责创建图的Javascript

我是一名编程初学者,我喜欢学习,我不是专业人士,也没有在学校学习。 感谢您的所有帮助和理解。

phpquery.php:

<?php
\\query get data from bd

$path = $_SERVER['DOCUMENT_ROOT'] . '\dist\includes\\';
$file = $path . 'db.connect.php';
include($file);
$tec=$_SESSION['nome'];
$permi = $_SESSION['permisson'];
if($permi == "Técnico"){ $sql = "SELECT * FROM npedido WHERE estado = 'Novo' and (tec1 ='$tec' or tec2 ='$tec')";}else if($permi == "Administrador"){ $sql = "SELECT * FROM npedido WHERE estado = 'Novo'";}
    $stmt = sqlsrv_query( $conn, $sql);
	 $contador_pedidos_novos = 0;
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) {
       $contador_pedidos_novos++;
    }

     
    
    ?>
我想将结果放入值内:“ {value:**<--mydata-->**, name:'New'}

dataload.js

/*****E-Charts function start*****/
var echartsConfig = function() { 
	

	if( $('#e_chart_3').length > 0 ){
		var eChart_3 = echarts.init(document.getElementById('e_chart_3'));
		var option3 = {
			tooltip : {
				trigger: 'item',
				formatter: "{a} <br/>{b} : {c} ({d}%)",
				backgroundColor: 'rgba(33,33,33,1)',
				borderRadius:0,
				padding:10,
				textStyle: {
					color: '#fff',
					fontStyle: 'normal',
					fontWeight: 'normal',
					fontFamily: "'Roboto', sans-serif",
					fontSize: 12
				}	
			},
			legend: {
				show:true
			},
			toolbox: {
				show : true,
			},
			calculable : true,
			itemStyle: {
				 normal: {
					 shadowBlur: 5,
					 shadowColor: 'rgba(0, 0, 0, 0.5)'
				 }
			},
			series : [
				{
					name:'Advertising',
					type:'pie',
					radius : '60%',
					center : ['50%', '50%'],
					roseType : 'radius',
					color: ['#119dd2', '#d36ee8', '#667add'],
					label: {
						normal: {
							fontFamily: "'Roboto', sans-serif",
							fontSize: 12
						}
					},
					data:[
						{value:25, name:'New'},
						{value:25, name:'Close'},
						{value:50, name:'Hold'},
					].sort(function (a, b) { return a.value - b.value; }),
				},
			],
			animationType: 'scale',
			animationEasing: 'elasticOut',
			animationDelay: function (idx) {
				return Math.random() * 1000;
			}	
		};
		eChart_3.setOption(option3);
		eChart_3.resize();
	}
}
/*****E-Charts function end*****/

1 个答案:

答案 0 :(得分:0)

答案是在php文件中创建变量,然后从查询中获取数据内容,然后添加此行。

<script type="text/javascript">var teste = "<?= $teste ?>";</script>

在.js文件上,只需将变量名称添加到数据字段中即可:

<--other code-->
data:[
      {value:25, name:teste},
      <--other code-->