2016-04-12 40 views
-2

嗨,大家好我是一个新的我有一个关于如何从输入表格VAR到请求的URL问题MySQL的highcharts馅饼PHP获得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当我点击按钮不能获得价值

+1

$ jsonurl2的输出是怎样的?你不能像普通的js变量那样在js中使用php变量。 –

+0

jsonurl返回我的json fomat数据 – dev

+0

我的问题是如何获得输入表单var并将该var放入$ jsonurl2 – dev

回答

1

您的表单动作必须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。

+0

我已经测试了代码,但是当我单击提交按钮 – dev

+0

时,它不起作用,那么错误是什么? –

1

请看看在表单的action属性这行代码

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