Partially blank email from form

Hi,

I have a form that sometimes sends incomplete info, some do come in complete.

The missing part of the incomplete ones are the info after this:

$body .= "Dear " . $name . " " . $lastname . " thank you for your registration which will be finalized once payment of the fees has been confirmed (see fees & terms). \

The following are the information you have provided. \

Please kindly review them and in case of mistakes please contact this email. Do NOT register again." . "\
 \
";

By looking at the error in the log file I get this:
PHP Warning: Invalid argument supplied for foreach() in /path/to/file/registrazione.php on line 63

Line 63 is this:

foreach ($clean_post as $key => $value) {
   $body .= $key . ': ' . $value . "\
";
}

Anyhow, since I believe that might be caused by something else I added to the code I post the whole code for you to kindly review:

<?php
//MAIL HEADER INFORMATION
$EmailFrom = "site name";
$EmailTo = "email to send form to";
$Subject = "registration";

// CLEANUP ANY NON-PRINTABLE OR UNWANTED CHARACTERS
function clean_text_string($string) { // FORCE IT TO ACCEPTABLE ESCAPABLE CHARACTERS ONLY
        $new    = trim(ereg_replace('[^\\' a-zA-Z0-9&!#$%()"+:?/@,_\\.\\-]', '&iquest;', stripslashes($string)));
        $new    = ereg_replace(' +', ' ', $new);
return ( $new );
}

// ITERATE OVER POST DATA
foreach ($_POST as $key => $value) {
   $clean_key   = strtolower(clean_text_string($key));
   if (substr($clean_key,0,1) == '_') continue; // SKIP FIELD NAMES THAT START WITH UNDERSCORE
   $clean_value = clean_text_string($value);
   if ($clean_value == '') continue; // SKIP EMPTY FIELDS
   $clean_post["$clean_key"] = $clean_value;
}

// NOW TEST FOR FIELDS THAT ARE REQUIRED
$required = array(); // ADD YOUR FIELDS AS NEEDED
$all_okay = TRUE;
foreach ($required as $key) {
   if ($clean_post["$key"] == '') {
      echo "<br/>$key is a required field\
";
      $all_okay = FALSE;
   }
}

// TEST FOR MISSING INPUT
if (!$all_okay) {
   $referer = $_SERVER['HTTP_REFERER'];
   echo "<br /><strong><a href=\\"$referer\\">Please click to go back, and fill the required fields!</a></strong>\
";
   die();
}

// PREPARE THE DATA
$name = Trim(stripslashes($_POST['name']));
//all the fields of the form here like the lines above, and below
$comment = Trim(stripslashes($_POST['comment']));

// PREPARE EMAIL BODY TEXT
$body = '';
$body .= "Dear " . $name . " " . $lastname . " thank you for your registration which will be finalized once payment of the fees has been confirmed (see fees & terms). \

The following are the information you have provided. \

Please kindly review them and in case of mistakes please contact this email. Do NOT register again." . "\
 \
"; // THIS IS TO HAVE PERSONALIZED MESSAGE
foreach ($clean_post as $key => $value) {
   $body .= $key . ': ' . $value . "\
";
}


$submitter = $_POST["email"];
$noreply = "no-reply@site.com";

// send email
$success = mail($EmailTo, $Subject, $body, "From: <$EmailFrom>");
		   mail($submitter, $Subject, $body, "From: <$noreply>");

// redirect to success page
if ($success){
header( "Location: path/to/thankyou_reg.php" );
}

else
{print "There has been a technical problem, please resend, thank you."; }
?>

Also, if I’m not asking too much is there a way to get the address of the sender, regardless of him/her filling the email field?
And is it possible to put html in the body message:

$body .= "Dear " . $name . " " . $lastname . " thank you for your registration which will be finalized once payment of the fees has been confirmed (see fees & terms). \

The following are the information you have provided. \

Please kindly review them and in case of mistakes please contact this email. Do NOT register again." . "\
 \
";

Thank you

Did I ask something I shouldn’t have? :wink:

Don’t quote your keys if they are variables.

$clean_post["$clean_key"] = $clean_value; 

Should be

$clean_post[$clean_key] = $clean_value; 

… and

$clean_post["$key"]

Should be

$clean_post[$key]

And you might want to wrap is_array() around the foreach section in question to make sure you have an array.

if(is_array($clean_post)){
	foreach ($clean_post as $key => $value) {
	   $body .= $key . ': ' . $value . "\
";
	}
}

Other than that, add exit after a header(“location:”);

header( "Location: path/to/thankyou_reg.php" );
exit;

I noticed you have ereg_replace which is depreciated. I’m no expert at preg_replace but made a replacement. I also don’t see $lastname defined so I added that on this copy as well. No guarantees.

<?php
//MAIL HEADER INFORMATION
$EmailFrom = "site name";
$EmailTo = "email to send form to";
$Subject = "registration";

// CLEANUP ANY NON-PRINTABLE OR UNWANTED CHARACTERS
function clean_text_string($string){ // FORCE IT TO ACCEPTABLE ESCAPABLE CHARACTERS ONLY
	$new = trim(preg_replace('~[^ a-zA-Z0-9&!#$%().,]+~', '&iquest;', stripslashes($string)));
	return $new;
}

// ITERATE OVER POST DATA
foreach ($_POST as $key => $value) {
   $clean_key   = strtolower(clean_text_string($key));
   if (substr($clean_key,0,1) == '_') continue; // SKIP FIELD NAMES THAT START WITH UNDERSCORE
   $clean_value = clean_text_string($value);
   if ($clean_value == '') continue; // SKIP EMPTY FIELDS
   $clean_post[$clean_key] = $clean_value;
}

// NOW TEST FOR FIELDS THAT ARE REQUIRED
$required = array(); // ADD YOUR FIELDS AS NEEDED
$all_okay = TRUE;
foreach ($required as $key) {
   if ($clean_post[$key] == '') {
      echo "<br/>$key is a required field\
";
      $all_okay = FALSE;
   }
}

// TEST FOR MISSING INPUT
if (!$all_okay) {
   $referer = $_SERVER['HTTP_REFERER'];
   echo "<br /><strong><a href=\\"$referer\\">Please click to go back, and fill the required fields!</a></strong>\
";
   die();
}

// PREPARE THE DATA
$name = Trim(stripslashes($_POST['name']));
$lastname = Trim(stripslashes($_POST['lastname']));
//all the fields of the form here like the lines above, and below
$comment = Trim(stripslashes($_POST['comment']));

// PREPARE EMAIL BODY TEXT
$body = '';
$body .= "Dear " . $name . " " . $lastname . " thank you for your registration which will be finalized once payment of the fees has been confirmed (see fees & terms). \

The following are the information you have provided. \

Please kindly review them and in case of mistakes please contact this email. Do NOT register again." . "\
 \
"; // THIS IS TO HAVE PERSONALIZED MESSAGE
if(is_array($clean_post)){
    foreach ($clean_post as $key => $value) {
       $body .= $key . ': ' . $value . "\
";
    }
}


$submitter = $_POST["email"];
$noreply = "no-reply@site.com";

// send email
$success = mail($EmailTo, $Subject, $body, "From: <$EmailFrom>");
           mail($submitter, $Subject, $body, "From: <$noreply>");

// redirect to success page
if ($success){
	header( "Location: path/to/thankyou_reg.php" );
	exit;
}

else
{print "There has been a technical problem, please resend, thank you."; }
?>

Thank you I’ll try it out.

$lastname was not there as I had cut it off with a bunch of other fields (long form), I thought the comment

//all the fields of the form here like the lines above, and below

would help, but obviously I failed in delivering the message. :wink:

That’s cool. Just don’t want to have any php warnings of missing index or variables that could cause mail() to fail.