Html form to PHP my data gets lost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
noble
Forum Newbie
Posts: 4
Joined: Tue Apr 06, 2004 4:54 am

Html form to PHP my data gets lost

Post by noble »

Hello all:
I'm new to PHP. I setup a html form say "form.html" It prompts the user to fill in the fields: first name, email, & comments. Method is set to post. Then when they hit submit it goes to the php form "form.php" In the php form it is supposed to check to make sure all fields were filled in. If a field wasn't filled in that field would be displayed with a comment prompting the user to fill it in.

Anyway, after they fill in that field and hit submit I get the email but the information from the "Comment" section is missing. If they wrote a sentence I would get 1 word. If everything was filled out properly on the html page I would get everything. What can I do to resolve this? I know the info gets from the html to the php page b/c I did a simple echo check for the comment right before the rest of the php code and it all comes out there. After it gets validated its gone =(. Here is all the php code from my php form.

Code: Select all

<?php
<?php 
if (($FName == "") || ($Email == "") || ($Comments == "")) 
{ 
echo "<form name=form method=post>"; 
echo "<p class=bodymd align=center>Please make sure all required fields are filled out.<br>"; 
echo "Fill in the ones you missed, they are listed below.<br></p>"; 
echo "<p class=bodymd align=center>If you do not want to use a Email address type: <b>NA</b></p>"; 
} 
if ($FName == "") 
{ 
echo "<p class=bodymd>First Name<br><input type=text name=FName size=36></p>"; 
} 
else 
{ 
echo "<input type=hidden name=FName value=$FName>"; 
} 
if ($Email == "") 
{ 
echo "<p class=bodymd>Email (username@domain.com)<br><input type=text name=Email size=36></p>"; 
} 
else 
{ 
echo "<input type=hidden name=Email value=$Email>"; 
} 
if ($Comments == "") 
{ 
echo "<p class=bodymd>Comments or Questions<br><textarea name=Comments rows=10 cols=50></textarea></p>"; 
} 
else 
{ 
echo "<input type=hidden name=Comments value=$Comments>"; 
} 
if (($FName == "") || ($Email == "") || ($Comments == "")) 
{ 
echo "<input type=submit name=Submit value=Submit>"; 
echo "<input type=reset name=Reset value=Clear Form>"; 
echo "</form>"; 
} 
else 
{ 
$message = "Name: $FName\nEmail: $Email\nComments: $Comments\n"; 
$extra = "From: $FName\r\nReply-To: $Email\r\n"; 
mail ("someuser@adomain.com", "Subject something here", $message, $extra); 
echo "<br>"; 
echo "<p class=bodymd align=center>Thanks for your inguiry, $FName.<br>"; 
echo "A response will be sent to $Email as soon as possible.</p>"; 
} 
?>
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

i have a feeling that if you change this line

Code: Select all

echo "<input type=hidden name=Comments value=$Comments>";
to...

Code: Select all

echo "<input type=hidden name=Comments value=".$Comments.">";
it should work.

On a side not. You really should construct your form elements like this

Code: Select all

&lt;input type="hidden" name="Email" value="$Email"&gt;
Also, you should call your variables like this $_POST['Comments']

Mark
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Your data isn't lost, you're just not trying to access it correctly - the method you have is true for older versions of PHP but is deprecated in versions 4.1 and up:
Passing Variables through forms (POST) and URLS (GET)
Following on from what Bech has said, see the following code with comments:

Code: Select all

<?php

// * first you need to remove any leading or trailing spaces
//   from the posted data (otherwise a value of space will be
//   seen as a good entry)
// * at the same time you can set the values of the variables
//   you will need later on by using 'variable variable'
//   notation
foreach ($_POST as $key => $value) {
	// note that the double dollar sign is intentional
	$$key = trim($value);
}

// instead of just testing for an empty string, use the empty()
// function to make it even clearer as to what you are doing
// (IMHO)
if (empty($FName) || empty($Email) || empty($Comments)) {
	// the following is called heredoc format and makes it 
	// easier to write HTML in PHP, the important thing to
	// remember is that there should be no spaces after
	// <<<END and no spaces before or after END;
	// you definitely need double quotes around your HTML
	// attributes, you're going to find you have problems
	// retrieving any data with spaces otherwise
	echo <<<END
	
<form name="form" method="post">
	<p class="bodymd" align="center">Please make sure all required fields are filled out.<br />
	Fill in the ones you missed, they are listed below.</p>
	<p class="bodymd" align="center">If you do not want to use a Email address type: <b>NA</b></p>
END;
}

if (empty($FName)) {

	echo <<<END
	
	<p class="bodymd">First Name<br /><input type="text" name="FName" size="36" /></p>
END;
} else {

	echo <<<END
	
	<input type="hidden" name="FName" value="$FName" />
END;
}

if (empty($Email)) {

	echo <<<END
	
	<p class="bodymd">Email (username@domain.com)<br /><input type="text" name="Email" size="36" /></p>
END;
} else {

	echo <<<END

	<input type="hidden" name="Email" value="$Email" />
END;
}

if (empty($Comments)) {

	echo <<<END

	<p class="bodymd">Comments or Questions<br /><textarea name="Comments" rows="10" cols="50"></textarea></p>
END;
} else {

	echo <<<END

	<input type="hidden" name="Comments" value="$Comments" />
END;
}

if (empty($FName) || empty($Email) || empty($Comments)) {

	echo <<<END

	<input type="submit" name="Submit" value="Submit">
	<input type="reset" name="Reset" value="Clear Form">
</form>
END;

} else {

	// you need to include a test here so that you don't try
	// and send an e-mail to NA
	
	if ($Email != 'NA') {
		$message = "Name: $FName\nEmail: $Email\nComments: $Comments\n";
		$extra = "From: $FName\r\nReply-To: $Email\r\n";

		mail('someuser@adomain.com', 'Subject something here', $message, $extra);

		echo <<<END

<p class="bodymd" align="center">Thanks for your enquiry, $FName.<br />
A response will be sent to $Email as soon as possible.</p>
END;
	} else {
		echo <<<END
		
<p class="bodymd" align="center">Thanks for your enquiry, $FName.</p>
END;
	}

}
?>
Mac
noble
Forum Newbie
Posts: 4
Joined: Tue Apr 06, 2004 4:54 am

Post by noble »

Thanks Bech100 for trying...

Twigletmac it worked like a charm Thank you very much. Except for the very last part b/c after the "else" the message goes no where. I have an autoresponse setup on my server for emails but maybe I can configure it so if an invalid email like NA comes in it will be ignored. Anyway, I just wanted to thank you for taking the time to help me. I'm slowly starting to understand how to code and hopefully I'll become as good as you. :D

Code: Select all

<?php
A response will be sent to $Email as soon as possible.</p> 
END; 
   } else { 
      echo <<<END 
       
<p class="bodymd" align="center">Thanks for your enquiry, $FName.</p> 
END; 
   } 

} 
?> 
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Oops, I think that was me misreading the code - for some reason my brain decided that you were trying to send an e-mail to the enquirer. I'd delete that if...else statement to make it work as it should do :oops: so that this:

Code: Select all

if ($Email != 'NA') {
      $message = "Name: $FName\nEmail: $Email\nComments: $Comments\n";
      $extra = "From: $FName\r\nReply-To: $Email\r\n";

      mail('someuser@adomain.com', 'Subject something here', $message, $extra);

      echo <<<END

<p class="bodymd" align="center">Thanks for your enquiry, $FName.<br />
A response will be sent to $Email as soon as possible.</p>
END;
   } else {
      echo <<<END
      
<p class="bodymd" align="center">Thanks for your enquiry, $FName.</p>
END;
   }
becomes this:

Code: Select all

$message = "Name: $FName\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $FName\r\nReply-To: $Email\r\n";

mail('someuser@adomain.com', 'Subject something here', $message, $extra);

echo <<<END

<p class="bodymd" align="center">Thanks for your enquiry, $FName.<br />
A response will be sent to $Email as soon as possible.</p>
END;
Mac
noble
Forum Newbie
Posts: 4
Joined: Tue Apr 06, 2004 4:54 am

Post by noble »

What I think I'll do to make it easy is use that else statement you have there and just redirect it to another email account so it won't get an autoreply from my server.

So this

Code: Select all

<?php
if ($Email != 'NA') { 
      $message = "Name: $FName\nEmail: $Email\nComments: $Comments\n"; 
      $extra = "From: $FName\r\nReply-To: $Email\r\n"; 

      mail('someuser@adomain.com', 'Subject something here', $message, $extra); 

      echo <<<END 

<p class="bodymd" align="center">Thanks for your enquiry, $FName.<br /> 
A response will be sent to $Email as soon as possible.</p> 
END; 
   } else { 
      echo <<<END 
      
<p class="bodymd" align="center">Thanks for your enquiry, $FName.</p> 
END; 
   } 
?>
Will turn into this:

Code: Select all

<?php
if ($Email != 'NA') { 
      $message = "Name: $FName\nEmail: $Email\nComments: $Comments\n"; 
      $extra = "From: $FName\r\nReply-To: $Email\r\n"; 

      mail('someuser@adomain.com', 'Subject something here', $message, $extra); 

      echo <<<END 

<p class="bodymd" align="center">Thanks for your enquiry, $FName.<br /> 
A response will be sent to $Email as soon as possible.</p> 
END; 
   } else {
$message = "Name: $FName\nEmail: $Email\nComments: $Comments\n"; 
      $extra = "From: $FName\r\nReply-To: $Email\r\n"; 

      mail('DIFFERENT_ADD@adomain.com', 'Subject something here', $message, $extra); 
 
      echo <<<END 
      
<p class="bodymd" align="center">Thanks for your enquiry, $FName.</p> 
END; 
   } 
?>
mail('DIFFERENT_ADD@adomain.com', ..... should do the trick. This way when the message is sent without an email it is OK b/c \Different_Add@adomain.com won't be setup with an autoresponse message. It didn't come to me until I read what you wrote... I gotta remember Ease of Administration is almost always a good thing :) Thanks Again!!!
Post Reply