E-mail contact form - adding a field to the PHP

Hi,

I don’t really know PHP so I experiment my way through, but can’t make this work.

The goal is to add a hidden form field called “article.” I got it close, but it never sent the field data.

In the script, fields are defined by:

$name     = $_POST['name'];
$email    = $_POST['email'];
$comments = $_POST['comments'];
$verify   = $_POST['verify'];
$article  = $_POST['article'];

But I get into trouble trying to display the content of the field in the e-mail body. Here’s the existing code w/o any mods:

$e_body = "You have been contacted by $name, the message is:" . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via $email";

Can someone provide some guidance on how to add the article variable to the $e_content= line so that it displays the field content?

Thanks guys!

there is no hidden form element in the code you posted. you do not use $article.

The question is how to use $article - nothing I tried worked.

the question is: how does your form look like? and is $article what you expeted? and: what did you try?

				<form method="post" action="contact.php" name="contactform" id="contactform" class="form c-form">
					<fieldset>
						<input name="name" type="text" id="name" placeholder="Your Name" />
						<input name="email" type="text" id="email" placeholder="Your E-mail" />
						<input name="verify" type="text" id="verify" size="4" value="" placeholder="3 + 2 =" />
						<textarea name="comments" id="comments" placeholder="Type your message here"></textarea>
						<div class="text-center">
                        <input type="hidden" name="article" value="English">
							<input type="submit" class="submit btn color" id="submit" value="Send Message" />
						</div>
					</fieldset>
				</form>

I’m not sure what I tried anymore - it was a lot of things, that didn’t crash the script, but didn’t print results.

Just to be clear. The email is getting sent, only that “article” isn’t in it?

Yes - that’s correct.

What happens to $e_body, $e_content and $e_reply after you have set them?

You will need to add “article” to the contact.php file somewhere. Presumably appending it to $e_content

$e_body = "You have been contacted by $name, the message is:" . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . "\"$article\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via $email";

Thanks Mittineague.

I tried your code and all I received from the form for $article was “”

I had the same results when I hacked around, except I never had the precise syntax you have.

Any further thoughts?

What if you change that to

$e_content = $comments . " " . $article . PHP_EOL . PHP_EOL;

?

The HTML form code looks OK to me.
If $article is an empty string, (or null) at that point, you’ll need to find out where it’s getting set to empty.

Easy fix would be if there’s a typo somewhere, else follow the value back from the empty or down from the $_POST value.

Thanks but no. The content of comments no longer has “” around it, but nothing else came through :frowning:

The HTML form field is:

<input type="hidden" name="article" value="English">

What do you mean exactly?

I mean that somewhere between the form being submitted and the email send the value isn’t getting passed along correctly.

Maybe your validation / sanitation code is stopping it?

Thanks. There is validation code, but from my novice knowledge, I don’t see an issue:

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

$name     = $_POST['name'];
$email    = $_POST['email'];
$comments = $_POST['comments'];
$verify   = $_POST['verify'];

if(trim($name) == '') {
	echo '<div class="error_message">Hello! Entering your name is required.</div>';
	exit();
} else if(trim($email) == '') {
	echo '<div class="error_message">Looks empty! Please enter a valid email address.</div>';
	exit();
} else if(!isEmail($email)) {
	echo '<div class="error_message">Oops! It looks like you\'ve entered an invalid e-mail address, please try again.</div>';
	exit();
}

if(trim($comments) == '') {
	echo '<div class="error_message">Forget something? Please enter your message.</div>';
	exit();
} else if(!isset($verify) || trim($verify) == '') {
	echo '<div class="error_message">Dang math! Please enter the verification number.</div>';
	exit();
} else if(trim($verify) != '8') {
	echo '<div class="error_message">Sorry. The verification number you entered is incorrect.</div>';
	exit();
}

if(get_magic_quotes_gpc()) {
	$comments = stripslashes($comments);
}


$address = "contactform@xyz.com";




// Example, $e_subject = '$name . ' has contacted you via Your Website.';

$e_subject = 'You\'ve been contacted by ' . $name . '.';



$e_body = "A comment is awaiting approval:" . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via $email";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

if(mail($address, $e_subject, $msg, $headers)) {

	// Email has sent successfully, echo a success page.

	echo "<fieldset>";
	echo "<div id='success_page'>";
	echo "<h3>Comment submitted successfully</h3>";
	echo "<p>Thank you $name. Your comment is pending.</p>";
	echo "</div>";
	echo "</fieldset>";

} else {

	echo 'ERROR!';

}

?>

you are not even using $article anywhere in your code.

just write var_dump($_POST); on top of your script and have a look at what it displayes after form submission. also post the output here.

You also seem to be missing this line in the latest version of your code:

$article  = $_POST['article'];

This is the code that takes the input from the form field called “article”, and creates a local PHP variable called $article containing the value of the form field. That they are the same name is not relevant, so your PHP variable does not need to match the form field name. Given the content of it, you could use

$language = $_POST['article'];

and then use $language throughout the code.

I’ve been trying to make heads or tails of this but still not getting it.

I put $article = $_POST['article']; back in where it was missing, but still get nothing from the field.

What did you mean by: [quote=“droopsnoot, post:18, topic:254269”]
$language = $_POST[‘article’];

and then use $language throughout the code.
[/quote]

??

Thanks.

As @chorn said, the code you posted doesn’t contain any reference to $article anywhere in it. If you’ve put that line back in at the top, have you also modified the email contents as @Gandalf noted? You need to do two things in the code - first, take the $_POST field and assign it to a variable name, and then do something with that variable to get its value into your email.

I was just trying to illustrate that the name of the variable in your PHP processing code does not need to be (but can be) the same as the name of the field in the form that it came from.