Filling a form dynamically depending on link...

I’m working on a “Job Request Form”. I have a plain HTML form, and when submitted, its contents are passed to a confirmation page, which contains the following script: (this script creates and fills a new text file)

<?php
// Set some variables
$filename = 'data/' . $_POST['projName'] . '.txt';
$newevent = $_POST['projName'] . ' | ' . $_POST['stockNum'] . ' | ' . $_POST['dueDate'] . ' | ' ... ;

// Create text file and write job data to file
$handle = fopen($filename, 'w');
$text = $newevent;
fwrite($handle, $text);
fclose($handle);
header('Refresh: 3; url=index.php');
?>

As you can probably see from the last line in the preceding script, there is a redirect to the index.php page, which contains the following script: (this script creates and displays a link for each job that has been created)

<?php
if ($handle = opendir('data/')) {
   while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
         echo '<p><a href="job_web_filled.php">' . $file . '</a></p>';
      }
    }
    closedir($handle);
}
?>

All of the form fields in the “job_web_filled.php” form contain variables which allow the form to be filled from the text files. The very top bit of script on this form looks like this:

<?php
$dir = "data/";

if ($dh = opendir($dir)) {
   while (($file = readdir($dh)) !== false) {
      $data = file_get_contents('data/' . $file);
      list($projName, $stockNum, $dueDate, $jobSheetStarted, $toEditing, $toDesign, $layoutStarted, $contactPerson, $layoutBy, $contactPhone, ... $addtlNotes) = explode(" | ", $data);
   }
   closedir($dh);
}
?>

The problem I’m having is that the form is only being filled by the last job in the list. So, each time you click on a link, doesn’t matter which one, the information that fills the form belongs to the last job/text file in the list.

I can’t figure out how to fix this and would really appreciate any help at all.

Thanks in advance!

  • Jenn

You said that you have plain HTML form - it is not clear how it is filled at all? Can you post here html (or php?) code of your form?
Also, if you want your form filled depending on link - these links at least need to be different, i.e. have file name passed as parameter. Like this:
[php]
echo ‘

’ . $file . ‘

’;
[/php]

And then, in your file job_web_filled.php you need to open just this file, no need to read all the files in the data/ directory (eliminate the while loop):
[php]
$file = urldecode($_GET[‘file’]);
$data = file_get_contents(‘data/’ . $file);
list($projName, $stockNum, $dueDate, $jobSheetStarted, $toEditing, $toDesign, $layoutStarted, $contactPerson, $layoutBy, $contactPhone, … $addtlNotes) = explode(" | ", $data);
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service