PHP Mail Form, Some info not being mailed

I’m a complete noob to php and trying to fix a contact form for work. This page needs two contact forms: http://moschettalaw.com/fda-study-links-actos%C2%AE-to-bladder-cancer/,
I’m dealing with the bottom one currently. Any help is tremendously appreciated. I’m getting my contact emails back from tests, but several of the fields are empty. Site is in Wordpress.

HTML:

<h2>Actos® Bladder Cancer Contact Form</h2>
<form id="bladder-cancer-form" action="/forms/mail2.php" enctype="multipart/form-data" method="post"><label>Name:</label>&nbsp;

<input id="formname" name="formname" type="text" /> <label>
Phone:</label>&nbsp;

<input id="formphone" name="formphone" type="text" /></form> <form action="/forms/mail2.php" enctype="multipart/form-data" method="post"><label>Email:</label>&nbsp;

<input id="formemail" name="formemail" type="email" /></form> <form action="/forms/mail2.php" enctype="multipart/form-data" method="post"><label>How Long Were You Taking Actos?®</label>&nbsp;

<input id="formhowlong" name="formhowlong" type="text" /></form> <form action="/forms/mail2.php" enctype="multipart/form-data" method="post"><label>Where you Diagnosed with Bladder Cancer?</label>&nbsp;

Yes:
<input name="formdiagnosed" type="radio" value="Yes" />

No:
<input name="formdiagnosed" type="radio" value="No" />

<label>Date of Diagnosis</label>

<input id="formdate" name="formdate" type="text" /> <label></label> <label> </label>&nbsp;

<label>Questions:</label>
<textarea id="formquestions" style="width: 310px; height: 152px; margin: 2px;" name="formquestions"></textarea>
For immediate assistance please call us at: 1-877-472-1578 Toll Free 724-225-3060 Local in PA

<input class="submit" name="submit" type="submit" value="Submit" /> </form>

PHP:

<?

if(isset($_POST['submit'])) {
	    $to         = "michael@piconsulting.com";
		$subject    = "Bladder Cancer Form";
		$headers    = "From: $formemail";
		
}


		$formname		= $_POST['formname'];
		$formphone		= $_POST['formphone'];
		$formemail		= $_POST['formemail'];
		$formhowlong	= $_POST['formhowlong'];
		$formdiagnosed	= $_POST['formdiagnosed'];
		$formdate	    = $_POST['formdate'];
		$formquestions	= $_POST['formquestions'];
		
		$referer    = $_SERVER[HTTP_REFERER];
		$ip         = $_SERVER[REMOTE_ADDR];

		

		$msg       .= "Name: $formname\
";
		$msg       .= "Phone: $formphone\
";
		$msg       .= "Email: $formemail\
";
		$msg       .= "Response: \
";
		$msg       .= "Took Actos How Long: $formhowlong\
";
		$msg	   .= "Diagnosed?: $formdiagnosed\
";
		$msg       .= "Date of Diagnosis: $formdate\
";
		$msg       .= "Question: $formquestions\
\
";
		
		$msg       .= "Referer: $referer \
";
		$msg       .= "IP Address: $ip \
";

		mail($to, $subject, $msg, $headers);
		
		header ("Location:/thank-you/");
	

?>

Welcome to the SP forums.

It would help if you could tell us which fields are empty?

Sure, thank you.

Empty Fields in email:

-Email Subject
-From Email Field (Sender)
-Name
-Phone
-Email
-Took Actos How Long (formhowlong)

A bit confused how I’m getting some data but not all when it looks like code is same for each.

You have the closing bracket of the if after three lines. I think you have to move it after the header (in other words at the end of the php code you posted).

And right now this line

$headers    = "From: $formemail"; 

comes before

$formemail        = $_POST['formemail']; 

so $formemail has no value yet.

Like this? It resulted in same semi-empty email.

<?

if(isset($_POST['submit'])) {
	    $to         = "michael@piconsulting.com";
		$subject    = "Bladder Cancer Form";
		$headers    = "From: $formemail";
		



		$formname		= $_POST['formname'];
		$formphone		= $_POST['formphone'];
		$formemail		= $_POST['formemail'];
		$formhowlong	= $_POST['formhowlong'];
		$formdiagnosed	= $_POST['formdiagnosed'];
		$formdate	    = $_POST['formdate'];
		$formquestions	= $_POST['formquestions'];
		
		$referer    = $_SERVER[HTTP_REFERER];
		$ip         = $_SERVER[REMOTE_ADDR];

		

		$msg       .= "Name: $formname\
";
		$msg       .= "Phone: $formphone\
";
		$msg       .= "Email: $formemail\
";
		$msg       .= "Response: \
";
		$msg       .= "Took Actos How Long: $formhowlong\
";
		$msg	   .= "Diagnosed?: $formdiagnosed\
";
		$msg       .= "Date of Diagnosis: $formdate\
";
		$msg       .= "Question: $formquestions\
\
";
		
		$msg       .= "Referer: $referer \
";
		$msg       .= "IP Address: $ip \
";

		mail($to, $subject, $msg, $headers);
		
		header ("Location:/thank-you/");
	
}
?>

I would change the order of things a bit. And to find the cause of the empty fields, try doing a var_dump of $_POST to see what it contains (this will cause a “headers already sent” error, but that’s no problem, once the debugging is done you can delete the var_dump).


<?

if (isset($_POST['submit'])) {

  // var_dump, can be deleted when debugging is finished
  var_dump($_POST);

        $formname        = $_POST['formname'];
        $formphone        = $_POST['formphone'];
        $formemail        = $_POST['formemail'];
        $formhowlong    = $_POST['formhowlong'];
        $formdiagnosed    = $_POST['formdiagnosed'];
        $formdate        = $_POST['formdate'];
        $formquestions    = $_POST['formquestions'];

        $to         = "michael@piconsulting.com";
        $subject    = "Bladder Cancer Form";
        $headers    = "From: $formemail";
                
        $referer    = $_SERVER[HTTP_REFERER];
        $ip         = $_SERVER[REMOTE_ADDR];

        $msg       .= "Name: $formname\
";
        $msg       .= "Phone: $formphone\
";
        $msg       .= "Email: $formemail\
";
        $msg       .= "Response: \
";
        $msg       .= "Took Actos How Long: $formhowlong\
";  
        $msg       .= "Diagnosed?: $formdiagnosed\
"; 
        $msg       .= "Date of Diagnosis: $formdate\
"; 
        $msg       .= "Question: $formquestions\
\
"; 
        
        $msg       .= "Referer: $referer \
";
        $msg       .= "IP Address: $ip \
";

        mail($to, $subject, $msg, $headers); 
        
        header ("Location:/thank-you/");
    
}
?> 

guido2004,

this is what i get:

array(4) { ["formdiagnosed"]=> string(3) "Yes" ["formdate"]=> string(8) "1/5/2011" ["formquestions"]=> string(14) "ahkldajshfksaj" ["submit"]=> string(6) "Submit" }
Warning: Cannot modify header information - headers already sent by (output started at /home/moschett/public_html/forms/mail2.php:6) in /home/moschett/public_html/forms/mail2.php on line 37

Ok, so the problem is the form. It’s not sending all the fields you expect. Take a close look at your form again. You are closing and opening a new form for about each single form field. That means the send button will send only the fields contained in the form that contains the button as well. Get rid of all “intermediate” form opening and closing tags. Leave only the ones at the beginning and the end of the entire form.

I saw that, must have made a copy/pate error, as that’s not in my form code on Dreamweaver. Anywho, I tested form and I’m now getting all fields back. Thanks so much for the help! To remove var_dump, I just delete the code?

array(8) { ["formname"]=> string(7) "michael" ["formphone"]=> string(12) "xxx-xxx-xxxx" ["formemail"]=> string(24) "michael@piconsulting.com" ["formhowlong"]=> string(7) "3 years" ["formdiagnosed"]=> string(2) "No" ["formdate"]=> string(8) "1/5/2012" ["formquestions"]=> string(12) "testing form" ["submit"]=> string(6) "Submit" }
Warning: Cannot modify header information - headers already sent by (output started at /home/moschett/public_html/forms/mail2.php:6) in /home/moschett/public_html/forms/mail2.php on line 37

Yes, just delete the line with the var_dump.

Got it. Thanks so much for the help. Tremendously appreciated!

You’re welcome