Jump to content

Recommended Posts

Hey,

 

I'm a web designer that recently decided to go freelance, but I have little understanding of PHP and require help with some PHP code I use for my comment forms.

 

I have two forms on one of my pages and require them to go to different e-mail addresses.

 

Here is the PHP code before my DOCTYPE and HTML:

 

<?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 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 = 'info@mydomain.com'; 
	$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
	$headers = 'From: Contact Form <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

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

 

This is the first form, I want this to go to info@mydomain.com (Domain hasn't been set up yet but it works with another e-mail address)

 

<?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>Comment Successfully Sent!</strong></p>
	<p>Thank you <strong><?php echo $name;?></strong> your comment has been placed in que for admin review and will be posted shortly.</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="30" name="contactname" id="contactname" value="" class="required" />
	</div>

	<div>
		<label for="email"><strong>*Email:</strong></label>
		<input type="text" size="30" name="email" id="email" value="" class="required email" />
            <p>This field is kept private and will not be shown publicly.</p>
	</div>

	<div>
		<label for="message"><strong>*Comment:</strong></label>
		<textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
	</div>
    <p><input type="submit" value="Post" name="submit" /></p>
</form>

 

Then the second form I want to go to brad@mydomain.com, I used the exact same code as the first form, and was hoping there was a possibility to have them send to different emails without having to link to an external PHP file.

 

thanks,

Brad

 

Link to comment
https://forums.phpfreaks.com/topic/195238-web-developer-newbie-needs-help/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.