Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing Null values.

Status
Not open for further replies.
May 31, 2007
31
AE
Hi all,

I am new to progrmming and php, so apologies for the basic question.

i am writing form fields to a text file. however, i do wish to write the key names /values even if they are empty. the code i have is below, what do i change to get the form to write the field names and values?

<?php


foreach($_POST as $key => $data) {
if($key != 'required') {
if($data !='') {
$stringData .= $key.": ".$data .",";
}
}
}

$myFile = "testFile.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");

$stringData = $stringData."\n";

fwrite($fh, $stringData);
fclose($fh);


?>

Thanks in advance
 
your code will write a blank string for form controls that are empty: provided that the browser actuall submits the control. for example, browsers will not submit unchecked check boxes.

your code will also write the values of the submit button etc. is this desired?

I always prefer profiling the form first, which deals with both issues and makes displaying the form easier. I tend to use my own variant of quickForm (from pear) as the profiler but you can do it pretty easily with an array

Code:
$output = array();
$form = array('field1', 'field2');
if (isset($_POST['submit'])){
 foreach ($form as $control){
  $cValue = (isset($_POST[$control])) ? trim($_POST[$control] : NULL;
  $output[] = addslashes("$control:$value";
 }
 $myFile = "testFile.txt";
 $fh = fopen($myFile, 'a+') or die("can't open file");
 fwrite($fh, implode(',', $output) . "\n");
 fclose($fh);
}
 
hi jpadie,

thanks for the reply. i dont need the submit buttons written to the text file. i just need the field names and values.

also I have changed the code to the one you posted, and this is the error displayed.

Parse error: syntax error, unexpected ':' in /home/nbbquxxs/public_html/survey/test1.php on line 9

regards
 
there is a closing round bracket missing after the trim function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top