title: Passing variable from select element to JSON update function - PHP
body: <p>I'm trying to update individually loaded <code>.json</code> files according to whichever option in the <code>&lt;select&gt;</code> is chosen. It loads the <code>.json</code> file and then updates an individual file fine as long as I define <code>$myFile</code> as a specific <code>.json</code> file such as <code>$myFile = "data/data.json";</code></p>
  
  <p>But when I attempt to pass the select option as the filename, nothing saves. Where am I messing this up?</p>
  
  <p>Here is the HTML select:</p>
  
  <pre><code>&lt;form action="/server/php/data/process.php" method="post"&gt;
      &lt;select class="ui mini fluid search dropdown" id="templateSelection" type="text" name="selectTemplate" onchange="loadTemplate()"&gt;
          &lt;option value="" selected="selected"&gt;Select Template&lt;/option&gt;
          &lt;?php
              foreach(glob(dirname(__FILE__) . '/server/php/data/*') as $filename){
                  $filename = basename($filename);
                  echo "&lt;option value='" . $filename . "'&gt;".$filename."&lt;/option&gt;";
              }
          ?&gt;
     &lt;/select&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Here are the options that get populated:</p>
  
  <pre><code>&lt;option value="data.json"&gt;data.json&lt;/option&gt;
  &lt;option value="data2.json"&gt;data2.json&lt;/option&gt;
  &lt;option value="data3.json"&gt;data3.json&lt;/option&gt;
  </code></pre>
  
  <p>And here is process.php:</p>
  
  <pre><code>&lt;?php
  
  //$myFile = "data/data.json";
  $filename = $_POST['selectTemplate'];
  $myFile = "data/" . $filename;
  $arr_data = array(); // create empty array
  try
  {
      //Get form data
      $formdata = array(
          'ID'=&gt; $_POST['ID'],
          'attributeName'=&gt; $_POST['attributeName'],
          'valueX'=&gt; $_POST['valueX']
      );
      //Get data from existing json file
      $jsondata = file_get_contents($myFile);
      // converts json data into array
      $arr_data = json_decode($jsondata, true);
      $updateKey = null;
      foreach ($arr_data as $k =&gt; $v) {
          if ($v['ID'] == $formdata['ID']) {
              $updateKey = $k;
          }
      }
      if ($updateKey === null) {
          array_push($arr_data,$formdata);
      } else {
          $arr_data[$updateKey] = $formdata;
      }
  
      $jsondata = json_encode($arr_data);
  
      //write json data into data.json file
      if(file_put_contents($myFile, $jsondata)) {
          echo 'Data successfully saved';
      }
      else
          echo "error";
  }
  catch (Exception $e) {
      echo 'Caught exception: ',  $e-&gt;getMessage(), "\n";
  }
  ?&gt;
  </code></pre>
  
  <p>EDIT: I'm submitting it (with a submit button) via another form on the same page with the same action. This other form allows me to change some of the values of the json that is loaded when an option in the first form is selected. Here is what this second form looks like:</p>
  
  <p><code>&lt;form class="ui form" action="server/php/process.php" method="POST" target="frame"&gt;
  ....
  &lt;input type="submit" value="Save"&gt;
  &lt;/form&gt;
  </code></p>
  
  <p>Perhaps that is where the issue is coming from? I figured a submit on this 2nd form would pick up the selected option in the first form and store the option in a variable.</p>