Page 1 of 1

php to .dat file

Posted: Wed Oct 07, 2009 4:36 pm
by span
Hi I'm very to new to php, but here is what we're trying to do. We have a webpage with an html table that can be filled out. Once the table has been completed and submitted, We want to submit the information to a csv .dat file using php. Below is first an example from the html table and second the php code that we are trying. The problem is that it is creating the .dat file in the directory, but it is not putting any of the data into that file. Any suggestions would be much appreciated. Thank you!


1.
<table>
<tr bgcolor="#AACCFF">
<td valign="center" colspan="1" height="75" width="400">
<p style="margin-top: 0; margin-bottom: 0" align="left">
1. Age (must to 21 to 50)
</p>
</td>
<td valign="center" colspan="3" height="75" width="400">
<p style="margin-top: 0; margin-bottom: 0" align="left">
<input type="text" size="6" name="DEMO1"> years old
</p>
</td>
</tr>

2.

<?php
if (array_key_exists('send', $_POST)) {

// 1. Add field names to the $expected array //
$expected = array();

//2. Add field names to $required array !!!!!!!!!!!!!!!!//
$required = array('date','DEMO1','DEMO2','DEMO2a','DEMO3','DEMO4','DEMO5','DEMO6','DEMO7','MAST1','MAST2','MAST3','MAST4','MAST5','MAST6','MAST7','MAST8','MAST9','MAST10','MAST11','MAST12','MAST13','Hand1','Hand2','Hand3','Hand4','Hand5','Hand6','Hand7','Hand8','Hand9','Hand10','HandTotal','QUAL1','QUAL2','QUAL3','QUAL3a','QUAL4','QUAL4a','QUAL5','QUAL6'); // #2 is this field !!!

$missing = array(); // create empty array for any missing fields
foreach ($_POST as $key => $value) { // process the $_POST variables
$temp = is_array($value) ? $value : trim($value); // assign to temporary variable and strip whitespace if not an array

if (empty($temp) && in_array($key, $required)) { // if empty and required, add to $missing array
array_push($missing, $key);
} elseif (in_array($key, $expected)) { // otherwise, assign to a variable of the same name as $key
${$key} = $temp;
}
}

if (empty($missing)) { // if no fields are missing

// 3. Write the data to a file //
$csv_file = 'Alcohol_Screen.dat'; // Specify which file we will be writing to
$date=date('Y-m-d');
if (is_writable($csv_file)) { // Check to see if the file is writable
if (!$csv_handle = fopen($csv_file,'a')) { // Attempt to open the file to append data
//Debug: The file cannot be opened, it may not exist or the filename may be incorrect.
//echo "<p>Cannot open file $csv_file</p>";
exit;
}
} else {
// Debug: The file does not have permissions set to enable writing data to the file.
echo "<p>File $csv_file is not writable.</p>";
}
$csv_item = "$date,$DEMO1,$DEMO2,$DEMO2a,$DEMO3,$DEMO4,$DEMO5,$DEMO6,$DEMO7,$MAST1,$MAST2,$MAST3,$MAST4,$MAST5,$MAST6,$MAST7,$MAST8,$MAST9,$MAST10,$MAST11,$MAST12,$MAST13,$Hand1,$Hand2,$Hand3,$Hand4,$Hand5,$Hand6,$Hand7,$Hand8,$Hand9,$Hand10,$HandTotal,$QUAL1,$QUAL2,$QUAL3,$QUAL3a,$QUAL4,$QUAL4a,$QUAL5,$QUAL6";
if (is_writable($csv_file)) {
if (fwrite($csv_handle, $csv_item) === FALSE) { // write the data to the file
//Debug: was unable to write to the file
//echo "Cannot write to file";
$dataLogged = false;
exit;
} else {
// $missing is no longer needed if the data was logged, so unset it
unset($missing);
$dataLogged = true;
}

}
fclose($csv_handle);

}//if (empty($missing)) { // if no fields are missing
} //if (array_key_exists('send', $_POST))
?>

Re: php to .dat file

Posted: Wed Oct 07, 2009 4:45 pm
by JNettles
Have you set permissions for the file if you're not under Windows? I see error checks in your code, are any of those being displayed?

Re: php to .dat file

Posted: Wed Oct 07, 2009 5:34 pm
by span
Thanks for the quick reply. There are no errors being displayed. The data can be filled in and submitted just fine, but nothing appears in the .dat file

Re: php to .dat file

Posted: Thu Oct 08, 2009 9:13 am
by JNettles
Your code looks fine to me so I can't be entirely sure what the problem is - check the chmod on your .dat file and then start going through your code a line at a time - put in an echo "Successful .... process" to try and isolate where the problem is. That or I suggest using either Eclipse or Zend Studio or VS.PHP so that you have a debugger with breakpoints to step through your code line by line.