dongzanxun2790 2018-05-13 18:45
浏览 102
已采纳

如何通过HTML表单和下拉菜单动态更改dygraphs的URL

I would really appreciate if someone could help me out. I did try hard to find a solution but to no avail due to my limited skills. Here is the scenario: I have got a python script that outputs temperature, pressure and humidity from a sensor connected to my Raspberry Pi. The same script also saves the readings into a .CSV file which has the date as file name so that I have every day a unique file with a full day's worth of data in it. I have implemented within a PHP page an open source JavaScript charting library in order to plot the readings:

link to dygraphs.com library

Also within the same PHP page I can select from a drop down menu the .CSV files mentioned above. To achieve that I have used the foreach construct.

Anyway please see below my PHP code:

<html>
<center>
   <iframe width="800" height="600"  src="http://SERVER_IP_ADDRESS/graphs.php" frameborder="0" allowfullscreen="" scrolling="no$
</center>
<head>
<script type="text/javascript"
<script src="//cdnjs.cloudflare.com/ajax/libs/dygraph/2.1.0/dygraph.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/dygraph/2.1.0/dygraph.css" />
</head>
<body>
<div id="graphdiv2"
  style="width:700px; height:500px;"></div>
<script type="text/javascript">
  g2 = new Dygraph(
    document.getElementById("graphdiv2"),
     "20180508.csv", // path to CSV file
    {rollPeriod: 7,
      showRoller: true}                   // options
  );
</script>
</body>
</html>
</head>

<body>
<?php
$filename = 'CSV_index.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>
<form method="post" action="">
        <select name="menu">
            <option selected value="base">Please Select</option>
           <?php foreach($eachlines as $lines){ //add php code here
                echo "<option value='".$lines."'>$lines</option>";
            }?>
        </select>

 <button type="submit" name="submit" >Submit</button>

 </form>

<?php
if(isset($_POST["submit"]))
{

$selected_value=$_POST["menu"];

?>

Now this is what I would love to achieve: once I select the .CSV file from the drop down menu, I would like it to appear automatically in the line below:

"20180508.csv", // path to CSV file

This is where the library finds the path to the .CSV file to plot that graph. So by selecting a specific .CSV I can then plot the relevant graph for a specific day.

I have tried the following but of course both didn't work:

"$selected_value", // path to CSV file

"'$selected_value'", // path to CSV file

Many thanks in advance for your kind kelp.

  • 写回答

1条回答 默认 最新

  • douli6605 2018-05-24 10:49
    关注

    SOLVED! After a lot of readings I have managed to solve my issue. The code below did the trick:

    <?php
    'echo $selected_value;'
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决