0
votes

I have looked everywhere and searched high and low. I have seen many issues with the PHP code I'm about to post, but nothing pertaining to my specific issue I am seeing.

Here is my issue - The two lines ($values/$required) where I have 'name','email',subject','message'

I would like to add 'tel' to it. (i.e. 'name','email','tel','subject','message')

If I add it to one of the two lines (doesn't matter which one), I can still submit the form but it doesn't give me that line (telephone) in the email I receive.

If I add the 'tel' to BOTH lines ($values/$required) then I get a "Please fill in all required fields, marked with *" when I hit submit (with all the lines filled out).

Can someone tell me what I am missing?

Also - 'subject' is actually a vin/body # but if I change the label in the html to 'vin' instead of 'subject' then it gives me the "Please fill in all required fields, marked with *" error so I just left it as 'subject' even though that is not what is really associated with that line item in the form.

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}
if($errors==1) {
    echo $error;
} else {
    $values = array('name','email','subject','message',);
    $required = array('name','email','subject','message',);

    $your_email = "[email protected]";
    $email_subject = "VIN / Body - ".$_POST['subject'];
    $email_content = "PART INQUIRY INFORMATION BELOW \n";

    foreach ($values as $key => $value) {
        if (in_array($value,$required)) {
            if ($key != 'subject' && $key != 'company') {
                if (empty($_POST[$value])) {
                    echo 'Please fill in all required fields, marked with *';
                    exit;
                }
            }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
    }

    if(@mail($your_email,$email_subject,$email_content)) {
        echo 'Message sent!';
    } else {
        echo 'ERROR!';
    }
}
?>
1
Fix you indentation, your code is a terrible read right now. - kapa
Please can you show.your form markup - Gavin
The question seems a bit confusing. I've tested your code adding the field 'tel' to both $values and $required arrays and it works (i.e. in $email_content the value of the 'tel' posted data appears). Are you sure that your starting form contains a field named 'tel' and that this field is posted to this page? With such a message seems that in $_POST array there is no 'tel' data. Furthermore, in $email_subject you are adding the value of $_POST['subject'], if you change the name of the field 'subject', this line must be corrected. I suggest you to rewrite step by step your form and this page. - Paolo Gibellini
yes 'tel' is in my form. I have also tried it with 'part' & 'description'. I don't get how it can be working for you and not for me? - user2958205
what do you mean posted to this page? "Are you sure that your starting form contains a field named 'tel' and that this field is posted to this page" - user2958205

1 Answers

0
votes

Updated answer:

Well, there's good news and bad news. The good news is, I think your code is actually operating properly. I think that your form is not actually posting the 'tel' field.

I tested by replacing the $_POST array with:

$_POST = array('name' => 'Joel', 'email' => '[email protected]', 'subject' => 'Guy','message' =>'Thingo','tel' => 'Some number');

I then added 'tel' to the 'values' array and your code worked as expected.

Do a print_r($_POST) before you process the data and look at the structure of the array. If the tel field isn't there, your form isn't submitting it.