Jump to content


Photo

Adding check box to send to an email address


  • Please log in to reply
4 replies to this topic

#1 jimi71

jimi71
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 August 2006 - 02:13 AM

Hey guys!

I was wondering ???:

Can I add a checkbox, that if checked, sends the email to an adress I specify?

As I have it, the person writes in the text area and sends that info to an email address that they input.

I would like to add a checkbox right before the SEND button that says, "if you want to send me a copy, please check this box"

Any Ideas?

Thanks!

-----

Heres the code I am using:

<?php

// your own email address
$your_email = ($_POST['txtEmail']);

// email subject line
$subject = "Message via your contact form";

// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>";

// This is displayed when the email has been sent
$thankyou_message = "<p>Thankyou. Your message has been sent.</p>";

// do not need to edit these lines

$name = stripslashes($_POST['txtName']);
$email = stripslashes($_POST['txtEmail']);
$message = stripslashes($_POST['txtMessage']);

if (!isset($_POST['txtName'])) {

?>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <p><label for="txtName">Name:</label><br />
    <input type="text" title="Enter your name" name="txtName" /></p>

    <p><label for="txtEmail">Email:</label><br />
    <input type="text" title="Enter your email address" name="txtEmail" /></p>

    <p><label for="txtMessage">Your message:</label><br />
    <textarea title="Enter your message" name="txtMessage"></textarea></p>

    <p><label title="Send your message">
    <input type="submit" value="Send" /></label></p>

</form>

<?php

}

elseif (empty($name) || empty($email) || empty($message)) {

    echo $empty_fields_message;

}

else {

    // Stop the form being used from an external URL
    // Get the referring URL
    $referer = $_SERVER['HTTP_REFERER'];
    // Get the URL of this page
    $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    // If the referring URL and the URL of this page don't match then
    // display a message and don't send the email.
    if ($referer != $this_url) {
        echo "You do not have permission to use this script from another URL.";
        exit;
    }

    // The URLs matched so send the email
    mail($your_email, $subject, $message, "From: $name <$email>");

    // Display the thankyou message
    echo $thankyou_message;
   
}

?>



#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 12 August 2006 - 02:21 AM

assuming you know how to write a checkbox form input, then it's a simple matter of checking whether it's set.  for example:

<input type="checkbox" name="mail_me" value="1" />

the resulting $_POST['mail_me'] will ONLY be set if it was checked.  if not, $_POST['mail_me'] will not exist upon for submission.  that being said, it's merely a matter of doing the following:

if (isset($_POST['mail_me']))
{
  // add yourself to the list of recipients, CCs, BCCs, or simply run an identical mail() command to the one you're currently running, using your own e-mail rather than the one specified by the user
}

after you've settled all the validation.

#3 raza.shahzad

raza.shahzad
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 12 August 2006 - 02:25 AM

I would like to add a checkbox right before the SEND button that says, "if you want to send me a copy, please check this box"

i suppose that if the check box is clicked(checked) and the send button clicked then the reciever as well as the person whom you refer as 'me' both will recieve the same msg.
if above is correct then you can place an if condition for processing the form which checks whether the checkbox is clicked or not. if the condition returns 1(checked) then email is sent to multiple recievers else it is sent to only one person.
hope you get my point.

#4 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 12 August 2006 - 02:27 AM

Adding on to raza.shahzad...

Something like...:

<?php

// your own email address
$your_email = ($_POST['txtEmail']);

// email subject line
$subject = "Message via your contact form";

// This is displayed if all the fields are not filled in
$empty_fields_message = "<p>Please go back and complete all the fields in the form.</p>";

// This is displayed when the email has been sent
$thankyou_message = "<p>Thankyou. Your message has been sent.</p>";

// do not need to edit these lines

$name = stripslashes($_POST['txtName']);
$email = stripslashes($_POST['txtEmail']);
$message = stripslashes($_POST['txtMessage']);

if (!isset($_POST['txtName'])) {

?>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

    <p><label for="txtName">Name:</label>

    <input type="text" title="Enter your name" name="txtName" /></p>

    <p><label for="txtEmail">Email:</label>

    <input type="text" title="Enter your email address" name="txtEmail" /></p>

    <p><label for="txtMessage">Your message:</label>

    <textarea title="Enter your message" name="txtMessage"></textarea></p>
		<label for="send_copy">Send me a copy:</label>
	<input type="checkbox" Title="Send Me a Copy" name="send_copy" />

    <p><label title="Send your message">
    <input type="submit" value="Send" /></label></p>

</form>

<?php

}

elseif (empty($name) || empty($email) || empty($message)) {

    echo $empty_fields_message;

}

else {

    // Stop the form being used from an external URL
    // Get the referring URL
    $referer = $_SERVER['HTTP_REFERER'];
    // Get the URL of this page
    $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    // If the referring URL and the URL of this page don't match then
    // display a message and don't send the email.
    if ($referer != $this_url) {
        echo "You do not have permission to use this script from another URL.";
        exit;
    }

    // The URLs matched so send the email
    mail($your_email, $subject, $message, "From: $name <$email>");
	if($_POST['send_copy'])
	{
	$email = "you@you.com";
	mail($email, $subject, $message, "From: $name <$email>");
	}

    // Display the thankyou message
    echo $thankyou_message;
    
}

?>

Why doesn't anyone ever say hi, hey, or whad up world?

#5 jimi71

jimi71
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 12 August 2006 - 06:10 PM

akitchin, raza.shahzad, corbin,

you guys rock....I see it now! And I got it!

I am new to php but I am starting to realize that as long as you follow the bouncing ball it will all make sense in the end. :)

Thank you!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users