php to .dat file
Posted: Wed Oct 07, 2009 4:36 pm
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))
?>
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))
?>