Robert Ettinger
Robert Ettinger

Reputation: 329

How fill form data from Dropdown?

I have a form on a page where users can save information, and a dropdown that pulls those saved files from the directory they saved them to. What I would like is when they go to that page, is that when they select the filename from the dropdown box, it places the name of the file in the input box "CodeDescription" field and the information on the file in the textarea "Code", however I am unsure how to go about parsing that. Below is my current code.

<input type="hidden" name="Action" value="EDIT" />
<input type="hidden" name="Selection"  id="Selection" value="-1">    
<div>Below is the list of your saved codes. To edit your codes, select it from the list.</div>
<select> 
<?php 
  $directory = $directory = 'users/' . $_SESSION['username'];
  $files = scandir( $directory ) ;
  foreach( $files as $file ) {
    if ( ! is_dir( $file ) ) {
      echo "<option>" . $file . "</option>";
    }
  }
  ?>
</select>
<h3>Saved Codes</h3>
<form method="post" action="/evo/avsaveprocess.php">
  <input type="hidden" name="Action" value="SAVE" />
  <input type="hidden" name="CodeId" id="CodeId" value="0" />
  <table width="100%" border="0">
    <tr>
      <td>Description:</td>
      <td><input type="text" name="CodeDescription" size="40" maxlength="50" id="CodeName" value="" /></td>
    </tr>
    <tr>
      <td valign="top">Code:</td>
      <td>
        <textarea rows="10" style="width:99%" name="Code" id="CodeValue"></textarea>
      </td>
    </tr>
  </table>
  <input type="submit" value="Save" />
</form>

Or, conversely, I wouldn't mind if on select of the right file in the dropdown menu it simply displayed the output beneath the form fields.

edited to add javascript I was trying:

<script>

function CodeChange() {                            
        var filesContentJS = "$filesContents";
        if (index > 0) {
            document.getElementById("Selection").value = filesContentJS;
          } else {
            document.getElementById("Selection").value = "-1";
            document.getElementById("CodeId").value = "0";
            document.getElementById("CodeName").value = "";
            document.getElementById("CodeValue").value = "";
        }
    }

</script>

updated javascript This does not output any errors, however when I inspect element in Chrome it returns:

Warning: file_get_contents(users/AddictionAddiction.txt) [function.file-get-contents]: failed to open stream: No such file or directory in /home/revo/public_html/evo/codesaveindex.php on line 141

which I'm not sure where to correct because there should be a / between AddictionAddiction.txt

Upvotes: 2

Views: 1038

Answers (1)

cyadvert
cyadvert

Reputation: 875

Create an array of files contents:

<?php 
$directory = $directory = 'users/' . $_SESSION['username'];
$filesContents = Array();
$files = scandir( $directory ) ;

foreach( $files as $file )
{
if ( ! is_dir( $file ) )
{
$filesContents[$file] = file_get_contents($directory . $file);
echo "<option>" . $file . "</option>";
}
}
?>

Then make it Javascript array or JSON

<script>
var filesContentJS = <?=json_encode($filesContents) ?>;
</script>

An in the select use onChange="" function: get selected file from dropdown, scroll through filesContentJS to find its content and fill in the form with it


on another note: keep in mind, that if your files are large (have lots of code/text) - you might need to do this with AJAX... When dropdown changes, do AJAX call to retrieve its info.

Upvotes: 1

Related Questions