treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Send me a copy - PHP form doesn't send copy

  • So I've got the following form:

    <form action="sendemail.php" method="post">
    <p><label for="from">From:</label><input id="from" name="from" type="text"/></p>
    <p><label for="to">To:</label><input id="to" name="to" type="text"/></p>
    <p><label for="subject">Subject:</label><input id="subject" name="subject" type="text"/></p>
    <p><label for="message">Message:</label><textarea id="message" name="message"></textarea></p>
    <p><label for="sendcopy">Send me a copy</label><input id="sendcopy" name="sendcopy" value="on" type="checkbox"/></p>
    <input type="submit" value="Send E-mail" name="submit"/>
    </form>


    Then, the following PHP to process the form:

    &lt;?php
    // Receiving variables
    @$from = addslashes($_POST&#91;'from'&#93;);
    @$to = addslashes($_POST&#91;'to'&#93;);
    @$subject = addslashes($_POST&#91;'subject'&#93;);
    @$message = addslashes($_POST&#91;'message'&#93;);
    @$sendcopy = addslashes($_POST&#91;'sendcopy'&#93;);

    //Sending Email to form owner
    # Email to Owner
    $headers = &quot;From&#58; $from&quot;;
    if ($sendcopy == &quot;on&quot;) {
    mail($from, $subject, $message, $headers);
    }
    mail($to, $subject, $message, $headers);
    header(&quot;Location&#58; thanks&#46;php&quot;);

    ?&gt;


    It sends the form to the "to" address, but when I have the checkbox checked, it doesn't send a copy of the email to the "from" address. This is so simple... why isn't it working?
  • Hi, I haven't used check boxes before so i might be wrong but check boxes pass their data to php in an array. So where you declare your variable to the post data it actually turns into an array with only one value (if the box was ticked, otherwise it will be empty).
    So when you test tp send the array, just check if the first value of the array is equal to on.

    if ($sendcopy&#91;0&#93; == &quot;on&quot;) {

    You might even just be able to see if the variable exists as if they don't check the box nothing should get passed through. So this might even work,

    if ($sendcopy) {

    Again, im still learning with php so someone else will have to check this but there is no harm in testing it out, please let us know if it does work.

    Also while im on it, i don't think you need to use the strip slashes on the checkbox post as the user can't alter the value.
  • Ok... I think my code works because I just tried some different email addresses and it sends correctly, it's just the emails aren't being delivered to my domain email address.

    Is it possible that it ignores emails that are sent to and from the same address?
  • On the checkboxes - forms only send array'ed data if you tell them to... for example:

    <input type="checkbox" name="data[value1][checkbox]" value="1" />

    would output the value - in this case "1" if you did:

    $data = $_POST['data'];

    echo $data[value1][checkbox];

    Other than that it would just be a normal value stored within the global post or get array.

    And the next part - if it works with another email address, are you SURE you have put the email address in right? Yea I know, sorry, but it's normally the silly things that I miss... :)