0

Trying to create a contact form on a website, the code for the form is shown below:

        <form name="contact_form" method="POST" action="send_email.php">
            <ul id="contact_form">
                <li>
                    <input required type="text" name="name" class="field_style field_split" placeholder="Your Name">
                    <input required type="email" name="email" class="field_style field_split" placeholder="Your Email">
                </li>
                <li>
                    <select required name="subject" class="field_style field_full">
                        <option value="General Enquiry">General Enquiry</option>
                        <option value="Coaching Enquiry">Coaching Enquiry</option>
                        <option value="Other">Other</option>
                    </select>
                </li>
                <li>
                    <textarea required name="message" class="field_style" placeholder="Message"></textarea>
                </li>
                <li>
                    <input id="submit_contact" name="submit" type="submit" value="Send Email">
                </li>
            </ul>
        </form>

This form should send an email with the information from the form fields, but it is failing to get the data from the fields when the user presses Submit.

Here is the send_email.php (with the recipient email replaced):

<?php
$to = "name@domain.com";

if(isset($_POST["from"])){ $from = $_POST["from"];}else{echo "from not set.";}
if(isset($_POST["name"])){ $name = $_POST["name"];}else{echo "name not set.";}
if(isset($_POST["subject"])){ $subject = $_POST["subject"];}else{echo "subject not set.";}
if(isset($_POST["message"])){ $message = $_POST["message"];}else{echo "message not set.";}

if(mail($to, $subject, $message))
{
    header("Location: #");
}else{
    echo "Error: ";
    print_r(error_get_last());
}
?>

And it returns the following message:

from not set.
name not set.
subject not set.
message not set.
Error: Array ( [type] => 8 [message] => Undefined variable: message [file] => /home/u884620714/public_html/send_email.php [line] => 14 )

Anyone have any ideas, because I'm new to PHP and don't understand why the data is not fetched from the form.

Thanks for the help in advance.

Cameron Wood
  • 154
  • 2
  • 12

1 Answers1

0

From the html, you are sending the POST vars: name, email, message, subject . But in the send_email.php file, you are expecting: from, name, subject, message. As you can see, you have not provided from.

Next, variable scoping. Regardless of whether the parameters in $_POST are set or not, $message and $subject all are out of scope and not defined at:

if(mail($to, $subject, $message))

Following should work:

index.html:

<form name="contact_form" method="POST" action="send_email.php">
    <ul id="contact_form">
        <li>
            <input required type="text" name="name" class="field_style field_split" placeholder="Your Name">
            <input required type="email" name="from" class="field_style field_split" placeholder="Your Email">
        </li>
        <li>
            <select required name="subject" class="field_style field_full">
                <option value="General Enquiry">General Enquiry</option>
                <option value="Coaching Enquiry">Coaching Enquiry</option>
                <option value="Other">Other</option>
            </select>
        </li>
        <li>
            <textarea required name="message" class="field_style" placeholder="Message"></textarea>
        </li>
        <li>
            <input id="submit_contact" name="submit" type="submit" value="Send Email">
        </li>
    </ul>
</form>

send_email.php:

<?php
$to = "name@domain.com";
print_r($_POST);

$required_vars = array("from", "name", "subject", "message");
$flag = false;
foreach($required_vars as $req_v) {
    if (!isset($_POST[$req_v])) {
        echo "$req_v is not set!\n";
        $flag = true;
    }
}
if ($flag) {
    exit();
}
$ret = mail($to, $_POST["subject"], $_POST["message"]);
if ($ret) {
    print_r($ret);
    //header("Location: #");
} else {
    echo "Error: ";
    print_r(error_get_last());
}

?>

Read more about variable scope here: Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?

Community
  • 1
  • 1
Nehal J Wani
  • 13,977
  • 2
  • 54
  • 77
  • Mostly worked: returned the following: "Array ( [name] => Foo [from] => Foo@bar.com [subject] => General Enquiry [message] => Test [submit] => Send Email ) 1" – Cameron Wood Aug 14 '16 at 12:26
  • @CameronWood You need to uncomment `header("Location: #")` from my answer and also, you need to read this: http://stackoverflow.com/a/23857883/1005215 – Nehal J Wani Aug 14 '16 at 13:22
  • The problem is that the variables are still not set, it returns as it did originally: "from is not set! name is not set! subject is not set! message is not set!" – Cameron Wood Aug 14 '16 at 13:31