Jump to content

Adding check box to send to an email address


jimi71

Recommended Posts

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;
   
}

?>

Link to comment
Share on other sites

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

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

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:

[code]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
}[/code]

after you've settled all the validation.
Link to comment
Share on other sites

[quote]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"[/quote]
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.
Link to comment
Share on other sites

Adding on to raza.shahzad...

Something like...:

[code=php:0]
<?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;
   
}

?>
[/code]
Link to comment
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.