0

I realized that not only was i trying to make things more complicated for myself I was also working in an external sheet. So when I defined, action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" The form was looking on the page for the script. Therefore could not locate the variables. Additionally, some of you pointed out case sensitive errors, which I have also addressed. The complete script now works like a charm, so i thank you again for your help. :)

PHP Solution

<?php
//*--/ variables /--*//
$emailSubject = 'Email Title';
$webMaster = 'php.test@outlook.com';

//*--/ define variables and set to empty values /--*//
$first_nameErr = $last_nameErr = $email_fieldErr = $tel_fieldErr = $select_optionErr = $enquiry_areaErr = "";
$first_name = $last_name = $email_field = $tel_field = $select_option = $enquiry_area = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
 $first_nameErr = "First name is a required field.";
} else {
 $first_name = test_input($_POST["firstname"]);
}

if (empty($_POST["lastname"])) {
 $last_nameErr = "Last name is a required field.";
} else {
 $last_name = test_input($_POST["lastname"]);
}

if (empty($_POST["email"])) {
 $email_fieldErr = "E-mail is a required field.";
} else {
 $email_field = test_input($_POST["email"]);
}

if (empty($_POST["tel"])) {
 $tel_fieldErr = "Tel No. is a required field.";
} else {
 $tel_field = test_input($_POST["tel"]);
}

if (empty($_POST["select"])) {
 $select_option= "";
} else {
 $select_option = test_input($_POST["select"]);
}

if (empty($_POST["enquiry"])) {
 $enquiry_areaErr = "Enquiry is a required field.";
} else {
 $enquiry_area = test_input($_POST["enquiry"]);
}
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

$body = <<<EOD
<br><hr><br>
First Name: $first_name <br>
Last Name: $last_name <br>
Email: $email_field <br>
Tel: $tel_field <br>
I heard about you from; $select_option <br>
Enquiry: $enquiry_area <br> 
EOD;

$headers = "From: $email_field\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster,$emailSubject, $body, $headers);
?>

HTML Solution " method="POST">

    <!--/ form left /-->
    <div id="form-left">
        <input type="text" name="first_name" placeholder="First Name" maxlength="64"><span class="error">* <?php echo $first_nameErr; ?></span><br>
        <input type="text" name="last_name" placeholder="Last Name" maxlength="64"><span class="error">* <?php echo $last_nameErr; ?></span><br>
        <input type="text" name="email" placeholder="JohnDoe@example.com" maxlength="128"><span class="error">* <?php echo $email_fieldErr; ?></span><br>
        <input type="text" name="tel" placeholder="Tel No." maxlength="16"><span class="error">* <?php echo $tel_fieldErr; ?></span><br>
        <input type="reset">
    </div>

    <!--/ form right /-->
    <div id="form-right">
        <select>
            <option value="where" name="select" selected>How did you find us? ▼</option>
            <option value="facebook">Facbook</option>
            <option value="twitter">Twitter</option>
            <option value="event">An Event</option>
            <option value="friend">A Friend</option>
            <option value="partner">A Partner</option>
            <option value="other">Other</option>
        </select> <br>
        <textarea name="enquiry" placeholder="Have an enquiry?" maxlength="750"></textarea><span class="error">* <?php echo $enquiry_areaErr; ?></span>
    </div>
    <div id="form-submit">
    <input type="submit" onClick="alert('Thank you, your enquiry has been recieved.')">
    </div>
    </div>
    </form>
MacMac
  • 34,294
  • 55
  • 151
  • 222
Beaniie
  • 52
  • 1
  • 8

3 Answers3

1

Non of your error variables are defined. If you check your PHP, only when there was a POST action, the variables would get a value.

Just define every variable at the top of your PHP page and it should work.

0

in your html code, you are using php variables, they are not defined when html form is loaded. so you are getting errors saying undefined variables.

if you dont want to see those errors simply use this

ini_set('display_errors', 0);
Developer
  • 3,857
  • 4
  • 37
  • 47
0

You don't use the good variables at your PHP code. You have affected value of variables from your html form. ----codes error $body = <<



First Name: $first_Name
Last Name: $last_Name
Email: $email_Field
Tel: $tel_Field
I heard about you from; $select_Option
Enquiry: $enquiry_Field
EOD;

----replace by $body = <<



First Name: $firstField
Last Name: $lastField
Email: $emailField
Tel: $telField
I heard about you from; $selectField
Enquiry: $enquiryField
EOD;
babafall
  • 1
  • 2