2016-04-12 80 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";