嗨,伙计们,我是一个新手,我有一个问题是如何将var从输入表单获取到请求url 中
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>中国武夷 000797 - 历史大单</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var options = {
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
},
title: {
text: '中国武夷 000797 历史大单 from 2016-03-14'
},
tooltip: {
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Highcharts.numberFormat(this.percentage,2) +' %';
}
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ Highcharts.numberFormat(this.percentage,0) +' %';
}
}
}
},
series: [{
type: 'pie',
name: 'BigVolume share',
colors: ['#DD5044','#17A05D','#FFCE43'],
data: []
}]
}
$jsonurl="000797vol400piedata.php?df=";
$.getJSON($jsonurl, function(json) {
options.series[0].data = json;
chart = new Highcharts.Chart(options);
});
$('#button').click(function() {
$jsonurl="000797vol400piedata.php?df=";
$.getJSON($jsonurl2, function(json) {
options.series[0].data = json;
chart = new Highcharts.Chart(options).redraw();
});
});
// window.setInterval(function(){
// $.getJSON("000797datapievol400.php", function(json) {
// options.series[0].data = json;
// chart = new Highcharts.Chart(options);
// });
//}, 3000);
});
</script>
<!-- <script src="http://code.highcharts.com/highcharts.js"></script> -->
<script src="../../stock/js/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
<div id="container" style="min-width: 280px; height: 230px; margin: 0 auto"></div>
<form name="form" action="<?php $jsonurl2 = $jsonurl.$_GET['df'];?>" method="GET">
<input type="text" name="df" value="2016-4-1" />
<input type="button" id="button" value="Start Date" />
</form>
</body>
</html>
000797vol400piedata.php代码是
<?php
$con = mysql_connect("localhost","tushare","tusharetushare");
$df = htmlspecialchars($_GET["df"]) ;
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("stockhistory", $con);
$resultx = mysql_query("select type,sum(volume) from `vol400-000797` where type='买盘' and date BETWEEN '$df' AND current_date()");
$rowsx = array();
while($r = mysql_fetch_array($resultx)) {
$row[0] = $r[0];
$row[1] = $r[1];
array_push($rowsx,$row);
}
$resulty = mysql_query("select type,sum(volume) from `vol400-000797` where type='卖盘' and date BETWEEN '$df' AND current_date()");
$rowsy = array();
while($r = mysql_fetch_array($resulty)) {
$row[0] = $r[0];
$row[1] = $r[1];
array_push($rowsy,$row);
}
$resultz = mysql_query("select type,sum(volume) from `vol400-000797` where type='中性盘' and date BETWEEN '$df' AND current_date()");
$rowsz = array();
while($r = mysql_fetch_array($resultz)) {
$row[0] = $r[0];
$row[1] = $r[1];
array_push($rowsz,$row);
}
$rows=array_merge($rowsx,$rowsy,$rowsz);
print json_encode($rows, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
我认为表单代码有问题当我点击按钮时,$jsonurl2无法获得值
您的表单操作必须是000797vol400piedata.php,所以您的表单看起来像:
<form name="form" action="000797vol400piedata.php" method="GET">
<input type="text" name="df" id="df" value="2016-4-1" />
<input type="submit" name="submit" value="Start Date" />
</form>
在000797vol400piedata.php 中
使用$_get[df']:获得df值
$df = $_GET['df'];
在您的案例中,为了在javascript中使用PHP变量,您应该考虑使用ajax。所以你的代码必须是:
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();//prevent default submit
var df = $('#df').val();
//other codes
$.getJSON('000797vol400piedata.php', {
df: df//Here we have our submitted data
}, function(json) {
options.series[0].data = JSON.parse(json);
chart = new Highcharts.Chart(options);
//other codes
});
});
});
停止使用mysql扩展,使用PDO或mysqli。
请看一下代码的这一行
<form name="form" action="<?php $jsonurl2 = $jsonurl.$_GET['df'];?>" method="GET">
在形式的动作属性中
action="<?php $jsonurl2 = $jsonurl.$_GET['df'];?>"
用代替
action="000797vol400piedata.php"
也在表单内部,在这行
<input type="button" id="button" value="Start Date" />
在中更改
<input type="submit" id="button" value="Start Date" />
你的表格必须是这样的,
<form name="form" action="000797vol400piedata.php" method="GET">
<input type="text" name="df" value="2016-4-1" />
<input type="submit" id="button" value="Start Date" />
</form>
在你的000797vol400piedata.php
中,你可以这样做来获得你传递的var的值。这只是一个如何接收表单发送的值的例子。用这个例子来编写代码。
$value = $_GET['df'];
echo "$df";