1

Last programming project for me was in GWBasic and Turbo Pascal... so please bear with me. I have been struggling to write the result of a drop down list selection to a text file. The code is able to write to the file, but whatever I try not the selected value. Thank you in advance.

<html lang = "nl">
  <head>
    <title>Thermostaat P7</title>
    <meta charset = "UTF-8" />
  </head>
  <body>
    <h1>Living room</h1>
    <form name="thermostat" method="POST">
       <fieldset>
          <legend>Temperature measurement</legend>
          <p>
             <?php
             $file = file_get_contents('/usr/local/bin/python/Templog', true);
             echo "The current temperature is $file °C.";
             ?>
         </p>
       </fieldset>
      <p> 
       <fieldset>
          <legend>Temperature setting</legend>
          <p>
             <label for="settemp" class="inline">Desired temperature</label>
             <select name="settemp" id="comboA" onChange="getComboA(this)">
               <option value=null SELECTED><?php $curSet = file_get_contents('/usr/local/bin/python/Tempset'); echo $curSet; ?> °C </option>
               <option value = "12">12 °C</option>
               <option value = "16">16 °C</option>
               <option value = "19">19 °C</option>
               <option value = "19.5">19,5 °C</option>
               <option value = "20">20 °C</option>
               <option value = "20.5">20,5 °C</option>
               <option value = "21">21 °C</option>
               <option value = "21.5">21,5 °C</option>
             </select>
          </p>
       </fieldset>
    </form>
  </body>
</html>

<script type="text/javascript">
function getComboA(sel) {
    var value = sel.value;
    <?php
      file_put_contents('/usr/local/bin/python/Tempset', value)
    ?>
}
ROB3RT
  • 11
  • 2
  • 2
    You can't mix javascript and php like that you need to use ajax. – jcubic May 10 '15 at 08:14
  • This SO question might help you understand better: [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming). – Yogi May 10 '15 at 08:33

1 Answers1

0

Here is how to do this action with AJAX.

First of all, change:

<p>
    <?php
         $file = file_get_contents('/usr/local/bin/python/Templog', true);
         echo "The current temperature is $file °C.";
    ?>
</p>

to:

<p id="Temp"></p>

That allows you to use the paragraph for the display of the temperature. Then for your Javascript, here is how to display the temperature.

<script type="text/javascript">
    function getComboA(sel) {
        var value = sel.value;
        var xmlhttp = new XMLHttpRequest();
        if(window.XMLHttpRequest) { // Check for browser acceptance
            xmlhttp=new XMLHttpRequest();
        } else {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("Temp").innerHTML = "The current temperature is " + value + "°C"
            }
        }

Here is your AJAX to write the temperature to WriteToTempset.php

        xmlhttp.open("GET", "WriteToTempset.php?temperature=" + value);
        xmlhttp.send();
    }
</script>

Here is WriteToTempset.php to write to the file:

<?php
    if(!empty($_GET['temperature'])) {
        $file = fopen("/usr/local/bin/python/Tempset", "w");
        fwrite($file, $_GET['temperature']);
        fclose($file);
    }
?>