Jump to content

Do Not Send $_post Data For Submit Button


son

Recommended Posts

From my online form I do not want to send the $_POST data for submit button. I tried: if (empty($errors)) { $S = ''; foreach($_POST as $key=>$value){ while ($key != $_POST['submitted']) { $S.= $key ." : ". $value."\n"; } }which does not work. How do you normally do that?Any hints appreciated.Son

Link to comment
Share on other sites

From my online form I do not want to send the $_POST data for submit button. I tried: if (empty($errors)) { $S = ''; foreach($_POST as $key=>$value){ while ($key != $_POST['submitted']) { $S.= $key ." : ". $value."\n"; } }which does not work. How do you normally do that?Any hints appreciated.Son
I have now made it work by assigning the values individually to the relevant variables. There are two more issues left, with one leaving me completely in the dark. The csv file arrives with all values in separate columns and the lot, but the first line in file is always empty. Does this have anything to do with: $headers .= "X-Mailer: Drupal\n"; $headers .= 'MIME-Version: 1.0' . "\n"; $headers .= 'Content-type: application/octet-stream; charset=iso-8859-1' . "\r\n"; $headers .= 'Content-Disposition: attachment; filename=catalogueRequest.csv' . "\r\n";And if yes, where do I go wrong?Thanks,Sona
Link to comment
Share on other sites

It's not that code, it probably has to do with the actual CSV file. A line break in CSV data will create an empty line.
I cannot see any line break in my code (or at least I do not see it). The first variable to asign is:$Title = ucwords($_POST['Title']);with the content ($S)for the csv file being put together as:$S = "Title,Firstname,Surname\n";$S.= $Title.",";$S.= $Firstname.",";$S.= $Surname;Do you see anything wrong with this?Son
Link to comment
Share on other sites

Looks fine to me. You might consider using array2csv instead though, it will properly escape everything. Also, in the headers above, the first two headers need to end with \r\n also, not just \n.

Link to comment
Share on other sites

Looks fine to me. You might consider using array2csv instead though, it will properly escape everything. Also, in the headers above, the first two headers need to end with \r\n also, not just \n.
Do you mean fputcsv? I could not find anything on array2csv. Why is it so important to escape everything?Son
Link to comment
Share on other sites

Ah, it looks like that was a custom function.

function array2csv ($fields, $delimiter = ',', $enclosure = '"', $mysql_null = false) {  $delimiter_esc = preg_quote($delimiter, '/');  $enclosure_esc = preg_quote($enclosure, '/');  $output = array();  foreach ($fields as $field)   {	if ($field === null && $mysql_null) 	{	  $output[] = 'NULL';	  continue;	}	$output[] = preg_match('/(?:'.$delimiter_esc.'|'.$enclosure_esc.'|\s)/', $field) ? (	$enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure	) : $field;  }  return join($delimiter, $output);}

You can put your data in an array and then use that to output it. You can send it options if you want to use something other than a comma or a quote to separate or enclose fields. You need to do that in case your data includes quotes or commas, you don't want the CSV structure to get messed up. So instead of this:$S = "Title,Firstname,Surname\n";$S.= $Title.",";$S.= $Firstname.",";$S.= $Surname;you could do this:$S = array2csv(array('Title', 'Firstname', 'Surname')) . "\n";$S .= array2csv(array($Title, $Firstname, $Surname)) . "\n";

Link to comment
Share on other sites

Do you mean fputcsv? I could not find anything on array2csv. Why is it so important to escape everything?Son
Used your custom function, but csv is exactly the same. First line is empty. Also, just echoed all my $_POST data (|Sa|SA|SA|). As I only entered 'sa' in fields (used | as a delimeter) there is no added space or something. Also, my form processing data (php) is in middle of HTML as it also contains message which should go nicely in the relevant containers. This or the host, could this be a point to look for mistakes?Son
Link to comment
Share on other sites

Used your custom function, but csv is exactly the same. First line is empty.
Then the extra line break is not in the CSV data. You might want to post the rest of the code, there's an extra line break there but I can't guess where it is. It's after the headers, and before the data.
Link to comment
Share on other sites

Then the extra line break is not in the CSV data. You might want to post the rest of the code, there's an extra line break there but I can't guess where it is. It's after the headers, and before the data.
Thanks for having a look. The code is:
if (isset($_POST['submitted'])){// store errors in an array	$errors = array();	//check for Firstname to be filled out	if (!isset($_POST['Title']) OR empty($_POST['Title'])) {		$errors['Title'] = 'Title';	}	//check for Firstname to be filled out	if (!isset($_POST['Firstname']) OR empty($_POST['Firstname'])) {		$errors['Firstname'] = 'Firstname';	}		//check for Firstname to be filled out	if (!isset($_POST['Surname']) OR empty($_POST['Surname'])) {		$errors['Surname'] = 'Surname';	}		if (empty($errors))	{		$Title = ucwords($_POST['Title']);		$Firstname = ucwords($_POST['Firstname']);		$Surname = ucwords($_POST['Surname']);function array2csv ($fields, $delimiter = ',', $enclosure = '"', $mysql_null = false){  $delimiter_esc = preg_quote($delimiter, '/');  $enclosure_esc = preg_quote($enclosure, '/');  $output = array();  foreach ($fields as $field)  {    if ($field === null && $mysql_null)    {      $output[] = 'NULL';      continue;    }    $output[] = preg_match('/(?:'.$delimiter_esc.'|'.$enclosure_esc.'|\s)/', $field) ? (    $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure    ) : $field;  }  return join($delimiter, $output);}$S = array2csv(array('Title', 'Firstname', 'Surname')) . "\n";$S .= array2csv(array($Title, $Firstname, $Surname)) . "\n";    	$headers .= "X-Mailer: Drupal\r\n";    	$headers .= 'MIME-Version: 1.0' . "\r\n";    	$headers .= 'Content-type: application/octet-stream; charset=iso-8859-1' . "\r\n";    	$headers .= 'Content-Disposition: attachment; filename=webVote.csv' . "\r\n";		mail('info@domain.co.uk','From Web Form',$S,$headers);	echo '<h2>Dear ' . $Firstname . '</h2>';	echo '<p>Thank you for your opinion.</p>';	 echo "</div>";echo "</div>";echo "</div>";echo "</body>";echo "</html>";exit;} else {	echo '<p><strong>Looks at the problems with fields below</strong></p>';	}} else {  echo "<p>You might want to look at my other web pages";  echo '<p>Fill out this form and I will get back to you....</p> ';}function check_error ($field, $text) {global $errors;if (empty($errors[$field])) {	echo $text;	} else {	echo "<strong>" . $errors[$field] . "</strong>";	}}?>  <form action="form.php" method="post">

Let me know if you spot anything...Son

Link to comment
Share on other sites

$headers .= 'Content-Disposition: attachment; filename=webVote.csv' . "\r\n";Remove that last \r\n, the last header doesn't need that on the end, it will add it automatically.
That's great! Removing the return and newline characters sorted my issue and there is not empty first row in csv file. Really appreciate your help. Can I take it that you never need to have those characters, even for an email with all form fields in 'normal' email text (without attachment)?Many thanks,Son
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...