Jump to content

Contact form help : adding a textfield to email.


renegade44

Recommended Posts

hey guys, here is my contact page: www.glenhealy.com/contact.php

 

I downloaded a contact form and got it set up on my site and working properly. Basically what I am trying to do is add a new field to the form. The new field is "current Web site". I was able to do all of the front end stuff and I basically messed around in the PHP trying to get it to work. I have gotten as far as having the field show up in the email but whatever is put into the actual text field doesn't show up.

 

Can someone help me out and tell me where I need to add more php code so that the form sends whatever is in that field?

 

Here is the PHP code:

 

<?php
//If the form is submitted
if(isset($_POST['submit'])) {

//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
	$hasError = true;
} else {
	$name = trim($_POST['contactname']);
}

//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
	$hasError = true;
} else {
	$subject = trim($_POST['subject']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
	$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
	$hasError = true;
} else {
	$email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
	$hasError = true;
} else {
	if(function_exists('stripslashes')) {
		$comments = stripslashes(trim($_POST['message']));
	} else {
		$comments = trim($_POST['message']);
	}
}

//If there is no error, send the email
if(!isset($hasError)) {
	$emailTo = '[email protected]'; //Put your own email address here
			$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nSite: $site \n\nComments:\n $comments";
			$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;


	mail($emailTo, $subject, $body, $headers);
	$emailSent = true;
}
}
?>                              

 

 

Here is my form:

 

<div id="contact-wrapper">
							<?php if(isset($hasError)) { //If errors are found ?>
									<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
								<?php } ?>

								<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
									<p><strong>Email Successfully Sent!</strong></p>
									<p>Thank you <span><?php echo $name;?></span> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>
								<?php } ?>  
						    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
								<div>
								    <label for="name"><strong>Name:</strong></label>
									<input type="text" size="50" name="contactname" id="contactname" value="" class="required" />
								</div>

								<div>
									<label for="email"><strong>Email:</strong></label>
									<input type="text" size="50" name="email" id="email" value="" class="required email" />
								</div> 
								<div>
									<label for="email"><strong>Current Website (if any)</strong></label>
									<input type="text" size="50" name="site" id="site" value="" class="" />
								</div>

								<div>
									<label for="subject"><strong>Subject:</strong></label>
									<input type="text" size="50" name="subject" id="subject" value="" class="required" />
								</div>

								<div>
									<label for="message"><strong>Message:</strong></label>
									<textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
								</div>
							    <input type="submit" value="Send Message" name="submit" id="button"/>
							</form>

						 </div>                                                        

 

 

You'll notice in your provided code that each form field is handled and then set into a variable, for example:

 

$name = trim($_POST['contactname']);
...
$subject = trim($_POST['subject']);

 

You have used the variable $site in the $body variable to send as part of your e-mail. However, you never set the $site variable to any value.

 

You should put this code somewhere in your PHP code. I have duplicated the coding standard used in your PHP source code below.

 

//Check to make sure that the site field is not empty
if(trim($_POST['site']) == '') {
    $hasError = true;
} else {
    $site = trim($_POST['site']);
}

 

One critique I have of the code is that when an error is found in the form submission I don't see any way that the error is handled... perhaps I just missed it on my quick glance of your code.

 

Have a good day,

Scot

 

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.